-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfunction_decorator.py
70 lines (57 loc) · 1.94 KB
/
function_decorator.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
import functools
import time
# decorator function to add milk in coffee
def milk_coffee(func):
def wrapper(*args, **kwargs):
func(*args, **kwargs)
print('+Milk')
return wrapper
# decorator function to add syrup in coffee. Accepts type of syrup as an argument
def syrup(syrup_type):
def wrapper(func):
def inner_wrapper(*args, **kwargs):
func(*args, **kwargs)
print(f'+{syrup_type} syrup')
return inner_wrapper
return wrapper
# decorator function to add sugar in coffee. Accepts qty of added sugar as an argument
def sugar(how_many):
def wrapper(func):
def inner_wrapper(*args, **kwargs):
func(*args, **kwargs)
print(f'+Sugar {how_many} spoons')
return inner_wrapper
return wrapper
# decorator function to mention type coffee. Accepts type as an argument
def type_of_coffee(name):
def wrapper(func):
def inner_wrapper(*args, **kwargs):
print(f'Make {name}')
func(*args, **kwargs)
return inner_wrapper
return wrapper
# decorator function to calculate function calls
def count_calls(func):
def wrapper(*args, **kwargs):
wrapper.calls_count += 1
func(*args, **kwargs)
wrapper.calls_count = 0
return wrapper
def timing(func):
"""Print the runtime of the decorated function"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
start_time = time.perf_counter() # 1
value = func(*args, **kwargs)
end_time = time.perf_counter() # 2
run_time = end_time - start_time # 3
print(f'Finished {func.__name__} in {run_time} secs')
return value
return wrapper
def debug(func):
"""Print the function arguments and return value"""
@functools.wraps(func)
def wrapper(*args, **kwargs):
print(f'Calling {func.__name__} with arguments {args, kwargs}')
return func(*args, **kwargs)
return wrapper