-
Notifications
You must be signed in to change notification settings - Fork 39
/
functions_and_decorators.py
285 lines (188 loc) · 4.81 KB
/
functions_and_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
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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
'''
Function
a function is a self-contained block of code that encapsulates a specific task or related group of tasks
'''
def my_func():
print("This is my function.")
my_func()
def my_func():
"""
This is a dummy function.
"""
print("This is my function.")
'''
Why do we need them?
- Code Reusability
- Chunking
Breaking a large task into smaller, bite-sized sub-tasks helps make the large task easier to think about and manage.
As programs become more complicated, it becomes increasingly beneficial to modularize them in this way.
- ETL
'''
def after_tax(price):
print(price * 1.1)
def after_tax(price, tax_percent):
print(price * (1 + (tax_percent/100)))
def after_tax(price, tax_percent=10):
print(price * (1 + (tax_percent/100)))
def after_tax(price, tax_percent):
return price * (1 + (tax_percent/100))
#When a parameter name in a Python function definition is preceded by an asterisk (*), it indicates argument tuple packing
*args
def multiply(*args):
print("Parameters passed in", args)
print("Type of args", type(args))
num = 1
for x in args:
num = num * x
return num
**kwargs
def sample_func(**kwargs):
print("Parameters passed in", kwargs)
print("Type of kwargs", type(kwargs))
for k, v in kwargs.items():
print(key, val)
#Python has a similar operator, the double asterisk (**), which can be used with Python function parameters and arguments to specify dictionary packing and unpacking.
def say(msg):
print(msg)
def saytwice(msg):
print(msg*2)
def wrapper_func(func):
msg = "Hello."
func(msg)
wrapper_func(say)
wrapper_func(saytwice)
import logging
logging.basicConfig(filename='multiply.log', level=logging.INFO)
def logger(func):
def log_func(*args):
logging.info(
'Running with arguments {}'.format(args))
func(*args)
return log_func
def multiply(*args):
num = 1
for x in args:
num = num * x
print(num)
multiply_logger = logger(multiply)
multiply_logger(2,3,4)
multiply_logger(1,2,3)
#1 Normal function call
def outer_func():
message = "Hi"
def inner_func():
print(message)
return inner_func()
outer_func()
#2 First class function
def outer_func():
message = "Hi"
def inner_func():
print(message)
return inner_func
my_func = outer_func()
my_func()
#3 Closure / Free variable
def outer_func(msg):
def inner_func():
print(msg)
return inner_func
hi_func = outer_func("Hi")
hi_func()
#4 With arguments
def double():
def inner_func(num):
print(num*2)
return inner_func
twox = double()
twox(2)
#4.1 passing function as arguments
def say(msg):
print(msg)
def saytwice(msg):
print(msg*2)
def outer_func(func):
def inner_func(msg):
func(msg)
return inner_func
hi_func1 = outer_func(say)
hi_func1("Hi")
hi_func2 = outer_func(saytwice)
hi_func2("Hi")
for f in (say, saytwice):
of = outer_func(f)
of("Hi")
#5 *arguments
def multiply():
def inner_func(*args):
num = 1
for x in args:
num = num * x
print(num)
return inner_func
calc = multiply()
calc(2,3,4)
calc(1,2,3)
def decorator_function(original_function):
def wrapper():
print(f"Wrapper ran before {original_function.__name__}")
original_function()
return wrapper
def display():
print("Display function ran.")
display = decorator_function(display)
display()
#8
def decorator_function(original_function):
def wrapper():
print(f"Wrapper ran before {original_function.__name__}")
original_function()
return wrapper
@decorator_function
def display():
print("Display function ran.")
display()
@decorator_function
def display2():
print("Display2 function ran.")
display2()
#9
class decorator_class(object):
def __init__(self, original_function):
self.original_function = original_function
def __call__(self):
print('call method before {}'.format(self.original_function.__name__))
self.original_function()
@decorator_class
def display3():
print("Display3 function ran.")
display3()
#10
# Practical Examples
from functools import wraps
def my_logger(orig_func):
import logging
logging.basicConfig(filename='{}.log'.format(orig_func.__name__), level=logging.INFO)
@wraps(orig_func)
def wrapper(*args, **kwargs):
logging.info(
'Ran with args: {}, and kwargs: {}'.format(args, kwargs))
return orig_func(*args, **kwargs)
return wrapper
def my_timer(orig_func):
import time
@wraps(orig_func)
def wrapper(*args, **kwargs):
t1 = time.time()
result = orig_func(*args, **kwargs)
t2 = time.time() - t1
print('{} ran in: {} sec'.format(orig_func.__name__, t2))
return result
return wrapper
@my_logger
@my_timer
def display2(caller):
import time
time.sleep(1)
print(f"Display function ran by {caller}.")
display2("Bharath")