forked from NetManAIOps/LogClass
-
Notifications
You must be signed in to change notification settings - Fork 0
/
decorators.py
26 lines (22 loc) · 929 Bytes
/
decorators.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
import functools
# Borrowed from https://realpython.com/primer-on-python-decorators/
def debug(func):
"""Print the function signature and return value"""
@functools.wraps(func)
def wrapper_debug(*args, **kwargs):
args_repr = [repr(a) for a in args] # 1
kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()] # 2
signature = ", ".join(args_repr + kwargs_repr) # 3
print(f"Calling {func.__name__}({signature})")
value = func(*args, **kwargs)
print(f"{func.__name__!r} returned {value!r}") # 4
return value
return wrapper_debug
def print_step(func):
"""Print the function signature and return value"""
@functools.wraps(func)
def wrapper_print_name(*args, **kwargs):
print(f"Calling {func.__qualname__}")
value = func(*args, **kwargs)
return value
return wrapper_print_name