Skip to content

Instantly share code, notes, and snippets.

@aperezlillo
Created February 3, 2024 07:22
Show Gist options
  • Save aperezlillo/ab14edca9901abb027f87d649b9fdcfd to your computer and use it in GitHub Desktop.
Save aperezlillo/ab14edca9901abb027f87d649b9fdcfd to your computer and use it in GitHub Desktop.

Revisions

  1. aperezlillo created this gist Feb 3, 2024.
    29 changes: 29 additions & 0 deletions decorators.py
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,29 @@
    def timing_decorator(func):
    import time
    def wrapper(*args, **kwargs):
    start = time.time()
    result = func(*args, **kwargs)
    end = time.time()
    print(f"{func.__name__} took {end - start} seconds to run.")
    return result
    return wrapper

    def debug_decorator(func):
    def wrapper(*args, **kwargs):
    print(f"Function {func.__name__} called with {args} and {kwargs}")
    result = func(*args, **kwargs)
    print(f"{func.__name__} returned {result}")
    return result
    return wrapper

    def memoize_decorator(func):
    # recursive functions memory
    memo = {}
    def wrapper(*args):
    if args in memo:
    return memo[args]
    result = func(*args)
    memo[args] = result
    return result
    return wrapper