-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathp14.py
494 lines (271 loc) · 12.3 KB
/
p14.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
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
# Decorators
# you have to have complete understanding of functions,
# first class function/closures
# then finally we will learn about decorators
def square(a):
return a**2
print(square(4)) # o/p: 16
s=square
print(s(7)) # o/p: 49
print(s.__name__) # o/p: square
print(square.__name__) # o/p: square
print(s) # o/p: <function square at 0x02C2F2B0>
print(square) # o/p: <function square at 0x02C2F2B0>
# pass function as argument
l=[1,2,3,4]
print(list(map(square,l))) # o/p: [1, 4, 9, 16]
print(list(map(lambda a:a**2,l))) # o/p: [1, 4, 9, 16]
def my_map(func, l):
new_list=[]
for i in l:
new_list.append(func(i))
return new_list
print(my_map(square,l)) # o/p: [1, 4, 9, 16]
print(my_map(lambda a:a**2, l)) # o/p: [1, 4, 9, 16]
def my_map2(func,l):
return[func(i) for i in l]
print(my_map2(lambda a:a**2,l)) # o/p: [1, 4, 9, 16]
# Function returning function ---or--- Closures ---or--- first class function
def outer_func():
def inner_func():
print("inside inner func")
return inner_func
print(outer_func()) # o/p: <function outer_func.<locals>.inner_func at 0x0115F3D0>
var=outer_func()
var() # o/p: inside inner func
def outer_func1(msg):
def inner_func1():
print(f"message is : {msg}")
return inner_func1
var1 = outer_func1("Hi there !")
var1() # o/p: message is : Hi there !
# Function returning function ---or--- Closures ---or--- first class function practice
def to_power(x):
def cal_power(n):
return n**x
return cal_power
squ=to_power(2) # o/p: enter number you want to square:
n=int(input("enter number you want to square:\n")) # 4
print(squ(n)) # 16
cube=to_power(3) # o/p: enter number you want to square:
n1=int(input("enter number you want to cube:\n")) # 2
print(cube(n1)) # 8
# Decorators------> enhance the functionality of other functions
# decorator is function which enhance the functionality of other functions
# i want to print "this is awesome" when i call func1 or func2 and i don't want to change the code of func1 and func2
def decorator_function(any_function):
def wrapper_function():
print("this is awesome")
any_function()
return wrapper_function
def func1():
print("this is function 1")
def func2():
print("this is function 2")
func1() # o/p: this is function 1
func2() # o/p: this is function 2
# here we can write func1 instead of var2 and var2()
var2=decorator_function(func1)
var2() # o/p: this is awesome
# this is function 1
func1=decorator_function(func1)
func1() # o/p: this is awesome
# this is function 1
# here we can write func2 instead of var3 and var3()
var3=decorator_function(func2) # o/p: this is awesome
var3() # this is function 2
func2=decorator_function(func2) # o/p: this is awesome
func2() # this is function 2
# @ -----> use for decorator
def decorator_function1(any_function):
def wrapper_function():
print("this is awesome")
any_function()
return wrapper_function
@decorator_function1 # shortcut
def func11():
print("this is function 11")
@decorator_function1
def func22():
print("this is function 22")
func11() # o/p: this is awesome
# this is function 11
func22() # o/p: this is awesome
# this is function 22
# def decorator_function2(any_function):
# def wrapper_function():
# print("this is awesome")
# any_function() ## in this function i will get error because func3(4) will call wrapper function which has no parameters, as a result i have to face type error.
# return wrapper_function ## we can solve this proble using *args and **kwarg
# @decorator_function2
# def func3(a):
# print(f"this is function {a}")
# func3(4) # o/p: type error
def decorator_function3(any_function):
def wrapper_function(*arg,**kwarg):
print("this is awesome")
any_function(*arg,**kwarg)
return wrapper_function
@decorator_function3 # shortcut
def func3(a):
print(f"this is function {a}")
func3(7) # o/p: this is awesome
# this is function 7
@decorator_function3
def add(a,b):
return a+b
add(3,4) # o/p: this is awesome
print(add(3,4)) # o/p: None ------ here, we are getting none as output because in decorator_function3 we simply call any_function(*args,**kwarg), so we have to return any_function to get real output(sum of a and b).
# I think this is complete decorator function
def decorator_function4(any_function):
def wrapper_function(*arg,**kwarg):
print("this is awesome")
return any_function(*arg,**kwarg)
return wrapper_function
@decorator_function4
def add1(a,b):
return a+b
add1(3,4) # o/p: this is awesome
print(add1(3,4)) # o/p: this is awesome
# 7
@decorator_function4
def func4(a):
print(f"this is function {a}")
func4(10) # o/p: this is awesome
# this is function 10
def decorator_function5(any_function):
def wrapper_function(*arg,**kwarg):
""" This is wrapper function """
print("this is awesome")
return any_function(*arg,**kwarg)
return wrapper_function
@decorator_function5
def add2(a,b):
''' this is add function '''
return a+b
print(add2.__doc__) # o/p: This is wrapper function
print(add2.__name__) # o/p: wrapper_function
# here we called add2 function, even though we are getting output of wrapper function
# we can overcome this problem by importing wraps module
# complete decorator function
from functools import wraps
def decorator_function6(any_function):
@wraps(any_function)
def wrapper_function(*arg,**kwarg):
""" This is wrapper function """
print("this is awesome")
return any_function(*arg,**kwarg)
return wrapper_function
@decorator_function6
def add3(a,b):
''' this is add function '''
return a+b
print(add3.__doc__) # o/p: This is add function
print(add3.__name__) # o/p: add3
# Decorators practice
from functools import wraps
def print_function_data(function):
@wraps(function)
def wrapper_function(*args,**kwarg):
print(f"you are calling {function.__name__} function")
print(f"{function.__doc__}")
return function(*args,**kwarg)
return wrapper_function
@print_function_data
def add4(a,b):
''' This function takes two numbers as argument and return their sum '''
return a+b
print(add4(4,5)) # o/p: you are calling add4 function
# This function takes two numbers as argument and return their sum
# 9
# How to find time which has been taken by executing function
import time
t1=time.time()
print("this is line 1") # o/p: this is line 1
x=5
if x==5:
print("x is equal to 5") # o/p: x is equal to 5
print("this is line 2") # o/p: this is line 2
print("this is line 2") # o/p: this is line 2
print("this is line 2") # o/p: this is line 2
print("this is line 2") # o/p: this is line 2
print("this is line 2") # o/p: this is line 2
print("this is line 2") # o/p: this is line 2
print("this is line 2") # o/p: this is line 2
t2=time.time()
print(t2-t1) # o/p: 0.004003047943115234
# Excercise 1
import time
from functools import wraps
def calculate_time(function1):
@wraps(function1)
def wrapper_function1(*args,**kwarg):
print(f"executing.....{function1.__name__}")
t11=time.time()
returned_value=function1(*args,**kwarg)
t22=time.time()
ta=t22-t11
print (f"this function took {ta} to run")
return returned_value
return wrapper_function1
@calculate_time
def funct():
print(f"this is {funct.__name__} function")
funct() # o/p: executing.....funct
# this is funct function
# this function took 0.003991842269897461 to run
@calculate_time
def check_square1(n1):
return [i**2 for i in range(1,n1+1)]
check_square1(1000) # o/p: executing.....check_square
# this function took 0.003998756408691406 to run
# Decorator practice 2
def add_all(*args):
total=0
for i in args:
total+=i
return total
print(add_all(1,2,3,4,5)) # o/p: 15
# print(add_all(1,2,3,4,5,[1,2,3])) # o/p: Error -------> we can dismiss this error by using decorator
from functools import wraps
def only_int_allow(funct1):
@wraps(funct1)
def inner_funct(*args,**kwarg):
# data_list=[]
# for i in args:
# data_list.append(type(i)==int)
# if all(data_list):
# return (funct1(*args,**kwarg))
# else:
# return "Invalid arguments"
if all([type(i)==int for i in args]):
return (funct1(*args,**kwarg))
return "Invalid arguments"
return inner_funct
@only_int_allow
def add_all1(*args):
total=0
for i in args:
total+=i
return total
print(add_all1(1,2,3,4,5)) # o/p: 15
print(add_all1(1,2,3,4,5,[1,2,3])) # o/p: Invalid arguments
# Decorator with arguments
from functools import wraps
def only_data_type_allow(data_type):
def decorator1(function11):
@wraps(function11)
def wrapper1(*args,**kwarg):
if all([type(i)==data_type for i in args]):
return function11(*args,**kwarg)
return "Invalid input"
return wrapper1
return decorator1
@only_data_type_allow(str)
def string_join(*args):
s=""
for i in args:
s+=i
return s
print(string_join('Ravin ','Rakholiya')) # o/p: Ravin Rakholiya
print(string_join('Ravin ','Rakholiya',8)) # o/p: Invalid input