ahmet / Python-for-Data-Science
Herkese Açık Ahmet Kerem Piscine Final
callLimit.py 947 B
from typing import Any
def callLimit(limit: int):
"""Build a decorator blocking a function above a call limit."""
count = 0
def callLimiter(function):
"""Wrap the function into its limited version."""
def limit_function(*args: Any, **kwds: Any):
"""Call the function while the limit is not reached."""
nonlocal count
if count >= limit:
print(f"Error: {function} call too many times")
return None
count += 1
return function(*args, **kwds)
return limit_function
return callLimiter
def main() -> None:
"""Test the call limit on two functions."""
@callLimit(3)
def f():
"""Print its own name."""
print("f()")
@callLimit(1)
def g():
"""Print its own name."""
print("g()")
for _ in range(3):
f()
g()
if __name__ == "__main__":
main()