-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDecorators.py
More file actions
executable file
·165 lines (125 loc) · 2.88 KB
/
Decorators.py
File metadata and controls
executable file
·165 lines (125 loc) · 2.88 KB
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Apr 27 17:35:40 2021
@author: maherme
"""
#%%
def counter(fn):
count = 0
def inner(*args, **kwargs):
nonlocal count
count += 1
print('Function {0} (id={1}) was called {2} times'.format(fn.__name__, id(fn), count))
return fn(*args, **kwargs)
return inner
def add(a: int, b: int = 0):
"""
adds two values
"""
return a + b
print(id(add))
help(add)
add = counter(add)
print(id(add)) # Notice this add function is not the same than above
help(add)
#%%
ret = add(10, 20)
print(ret)
ret = add(20, 40)
print(ret)
ret = add(10)
print(ret)
#%%
def mult(a: int, b: int, c: int = 1, *, d):
"""
multiplies four values
"""
return a * b * c * d
ret = mult(1, 2, 3, d=4)
print(ret)
ret = mult(1, 2, d=3)
print(ret)
mult = counter(mult) # Decorate mult with counter.
help(mult)
ret = mult(1, 2, 3, d=4)
print(ret)
ret = mult(1, 2, d=3)
print(ret)
#%%
# Using a decorator:
@counter
def my_func(s: str, i: int) -> str:
return s * i
help(my_func)
ret = my_func('a', 10)
print(ret)
print(mult.__name__)
print(mult.__doc__)
#%%
# We can write some docstring for inner:
def counter(fn):
count = 0
def inner(*args, **kwargs):
"""
this is the inner closure
"""
nonlocal count
count += 1
print('Function {0} (id={1}) was called {2} times'.format(fn.__name__, id(fn), count))
return fn(*args, **kwargs)
return inner
def mult(a: int, b: int, c: int = 1, *, d):
"""
multiplies four values
"""
return a * b * c * d
mult = counter(mult)
help(mult)
#%%
# You can fix some documentation issues:
def counter(fn):
count = 0
def inner(*args, **kwargs):
"""
this is the inner closure
"""
nonlocal count
count += 1
print('Function {0} (id={1}) was called {2} times'.format(fn.__name__, id(fn), count))
return fn(*args, **kwargs)
inner.__name__ = fn.__name__
inner.__doc__ = fn.__doc__
return inner
def mult(a: int, b: int, c: int = 1, *, d):
"""
multiplies four values
"""
return a * b * c * d
mult = counter(mult)
help(mult)
#%%
# But the arguments are incorrect, a better way to fix this is using @wraps:
from functools import wraps
def counter(fn):
count = 0
@wraps(fn)
def inner(*args, **kwargs):
"""
this is the inner closure
"""
nonlocal count
count += 1
print('Function {0} (id={1}) was called {2} times'.format(fn.__name__, id(fn), count))
return fn(*args, **kwargs)
# inner = wraps(fn)(inner) # This is another way to do @wraps(fn)
return inner
def mult(a: int, b: int, c: int = 1, *, d):
"""
multiplies four values
"""
return a * b * c * d
help(mult)
mult = counter(mult)
help(mult)
#%%