Decorators
timer(function)
Print the runtime of the decorated function.
Source code in src/deeponto/utils/decorators.py
21
22
23
24
25
26
27
28
29
30
31
32
33 | def timer(function):
"""Print the runtime of the decorated function."""
@wraps(function)
def wrapper_timer(*args, **kwargs):
start_time = time.perf_counter() # 1
value = function(*args, **kwargs)
end_time = time.perf_counter() # 2
run_time = end_time - start_time # 3
print(f"Finished {function.__name__!r} in {run_time:.4f} secs.")
return value
return wrapper_timer
|
debug(function)
Print the function signature and return value.
Source code in src/deeponto/utils/decorators.py
36
37
38
39
40
41
42
43
44
45
46
47
48
49 | def debug(function):
"""Print the function signature and return value."""
@wraps(function)
def wrapper_debug(*args, **kwargs):
args_repr = [repr(a) for a in args]
kwargs_repr = [f"{k}={v!r}" for k, v in kwargs.items()]
signature = ", ".join(args_repr + kwargs_repr)
print(f"Calling {function.__name__}({signature})")
value = function(*args, **kwargs)
print(f"{function.__name__!r} returned {value!r}.")
return value
return wrapper_debug
|
paper(title, link)
Add paper tagger for methods.
Source code in src/deeponto/utils/decorators.py
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67 | def paper(title: str, link: str):
"""Add paper tagger for methods."""
# Define a new decorator, named "decorator", to return
def decorator(func):
# Ensure the decorated function keeps its metadata
@wraps(func)
def wrapper(*args, **kwargs):
# Call the function being decorated and return the result
return func(*args, **kwargs)
wrapper.paper_title = f'This method is associated with tha paper of title: "{title}".'
wrapper.paper_link = f"This method is associated with the paper with link: {link}."
return wrapper
# Return the new decorator
return decorator
|
Last update:
February 1, 2023
Created:
January 14, 2023