Skip to content

Instantly share code, notes, and snippets.

@z00sts
Last active January 22, 2020 08:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save z00sts/6531125a698d9d5de9d48933d8a7b08e to your computer and use it in GitHub Desktop.
Save z00sts/6531125a698d9d5de9d48933d8a7b08e to your computer and use it in GitHub Desktop.
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