-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecorators.py
74 lines (53 loc) · 1.54 KB
/
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
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
71
72
73
74
# Decorators adds functionality to a function
def mydecorator(func):
def wrapper(*args, **kwargs: object):
return_value = func(*args, **kwargs)
print("Adding a decorator to your function...")
return return_value
return wrapper
@mydecorator
def hello_world(person):
print(f"Hello {person}")
return f"Hello {person}!"
# mydecorator(hello_world)() # Not a standard way of using a decorator
print(hello_world("John"))
# Practical Example #1 - Logging
def logged(func):
def wrapper(*args, **kwargs):
value = func(*args, **kwargs)
with open('logfile.txt', 'a+') as file:
fname = func.__name__
file.write(f"{fname} function returned a value - {value}\n")
print(f"{fname} function returned a value - {value}\n")
return value
return wrapper
@logged
def add(x, y):
return x + y
@logged
def multiply(x, y):
return x * y
@logged
def divide(x, y):
return x / y
add(10, 20)
multiply(10, 20)
divide(10, 20)
# Practical Example #2 - Timing
import time
def timer(func):
def wrapper(*args, **kwargs):
time_before = time.time()
value = func(*args, **kwargs)
time_after = time.time()
fname = func.__name__
print(f"{fname} function execution time - {time_after - time_before}")
return value
return wrapper
@timer
def number_count():
sum = 0
for i in range(10000000):
sum += i
return sum
number_count()