Snippets Collections
import time
from functools import wraps

def rate_limiter(max_calls, period=60):
    def decorator(func):
        timestamps = []

        @wraps(func)
        def wrapper(*args, **kwargs):
            nonlocal timestamps
            current_time = time.time()
            # Remove timestamps outside of the current period
            timestamps = [t for t in timestamps if current_time - t < period]
            if len(timestamps) < max_calls:
                timestamps.append(current_time)
                return func(*args, **kwargs)
            else:
                print(f"Rate limit exceeded. Try again in {int(period - (current_time - timestamps[0]))} seconds.")
        return wrapper
    return decorator

# Example usage:
@rate_limiter(max_calls=5)
def my_function():
    # Your function implementation here
    print("Function is called")

# Test the rate limiter
for i in range(10):
    my_function()
    time.sleep(1)  # Sleep for demonstration purposes
from ipywidgets import interact

@interact
def plot_polynom(a=[0,1,2,3], b=2):
    x = np.arange(-10, 10, 0.1)
    y = a*x**3+ b*x**2    
    plt.plot(x,y); plt.xlim(xmin=-10, xmax=10); plt.ylim(ymin=-100, ymax=100)
from ipywidgets import interact

@interact
def plot_polynom(a=[0,1,2,3], b=2):
    x = np.arange(-10, 10, 0.1)
    y = a*x**3+ b*x**2    
    plt.plot(x,y); plt.xlim(xmin=-10, xmax=10); plt.ylim(ymin=-100, ymax=100)
def my_decorator(func):
    def wrapper():
        func()
		func()

    return wrapper
def my_decorator(func):
    def wrapper():
        print("I'm before the method call")
        func()
        print("I'm after the method call")
    
    return wrapper
star

Sun Apr 07 2024 20:22:01 GMT+0000 (Coordinated Universal Time)

##python #coding #limiter #decorators
star

Sat Nov 26 2022 10:31:08 GMT+0000 (Coordinated Universal Time)

#decorators #jupyter
star

Sat Nov 26 2022 10:31:06 GMT+0000 (Coordinated Universal Time)

#decorators #jupyter
star

Sat Nov 26 2022 10:29:53 GMT+0000 (Coordinated Universal Time)

#python #decorators
star

Sat Nov 26 2022 10:28:52 GMT+0000 (Coordinated Universal Time)

#python #decorators

Save snippets that work with our extensions

Available in the Chrome Web Store Get Firefox Add-on Get VS Code extension