Skip to content

Instantly share code, notes, and snippets.

@z00sts

z00sts/call_limit.py

Last active Jan 22, 2020
Embed
What would you like to do?
call_limit.py
class CallLimitException(Exception):
pass
def call_limit(limit=10):
def deco(wrapped):
nonlocal limit
i = 0
def wrapper(*args, **kwargs):
nonlocal i
i += 1
if i > limit:
raise CallLimitException(i)
return wrapped(*args, **kwargs)
return wrapper
return deco
@call_limit(2)
def f(x):
print(f'f({x})')
return x
for j in range(1, 20):
print(f'{j}: {f(j)}')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.