-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathtutorial_38.py
113 lines (78 loc) · 2.73 KB
/
tutorial_38.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -------------------------------------------------------------------------------------------
def time_this(func):
import time
def wrapper(*args, **kwargs):
start = time.time()
r_value = func(*args, **kwargs)
stop = time.time()
runtime = stop - start
print(f'{func.__name__} took {runtime} seconds to exeucte.')
return r_value
wrapper.__name__ = func.__name__ + '_timed'
return wrapper
@ time_this
def sum_to_1(stop):
total = 0
for n in range(1, stop + 1):
total += n
return total
# decorator @time_this is the same as
sum_to_1 = time_this(sum_to_1)
result_1 = sum_to_1(30)
print(result_1)
# -------------------------------------------------------------------------------------------
def make_time_this(allowed=10):
def time_this(func):
import time
def wrapper(*args, **kwargs):
start = time.time()
r_value = func(*args, **kwargs)
stop = time.time()
runtime = stop - start
if runtime > allowed:
print(f'{func.__name__} took {runtime} seconds to exeucte. You might want to optimize it.')
return r_value
wrapper.__name__ = func.__name__ + '_timed'
return wrapper
return time_this
@ make_time_this(1)
def sum_to_2(stop):
total = 0
for n in range(1, stop + 1):
total += n
return total
# decorator @make_time_this(1) is the same as
sum_to_2 = make_time_this(1)(sum_to_2)
# probably won't tell the time it took to execute (depends on your computer speed)
result_2 = sum_to_2(30)
print(result_2)
# probably will tell how long it took to exeucte (depends on your computer speed)
print(sum_to_2(20_000_000))
# -------------------------------------------------------------------------------------------
# recap on regular factory function
def make_greet_1():
def greet(name):
print('hello there', name)
return greet
test_1 = make_greet_1()
test_1('jeremy')
# some simpler examples that don't make sense but is allowed
# -------------------------------------------------------------------------------------------
# use decorator which inputs the current function, despite not using the inputed function
def make_greet_2(function):
def greet(name):
print('hello there', name)
return greet
@ make_greet_2
def test_2():
pass
test_2('jeremy')
# -------------------------------------------------------------------------------------------
# another case of using a decorator which inputs the current function, despite not using it
def change_func_to_int(function):
return 5
@ change_func_to_int
def test_3():
pass
# no longer a function. just prints out 5
print(test_3)