Repository navigation
Expand file tree
/
Copy pathday19.py
More file actions
71 lines (71 loc) · 1.56 KB
/
Copy pathday19.py
File metadata and controls
71 lines (71 loc) · 1.56 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
import functools
def my_decorator(func):
def wrapper():
print("before function runs")
func()
print("after function runs")
return wrapper
@my_decorator
def greet():
print("hello!")
greet()
import time
def timer(func):
def wrapper(*args,**kwargs):
start=time.time()
result=func(*args,**kwargs)
end=time.time()
print(f"{func.__name__} took {end-start:.4f} seconds")
return result
return wrapper
@timer
def slow_function():
time.sleep(1)
print("done!")
#@timer
#def fibonacci(n):
# if n<=1:
# return n
# return fibonacci(n-1)+fibonacci(n-2)
@timer
def fibonacci_fast(n,memo={}):
if n in memo:
return n
if n<=1:
return n
memo[n]=fibonacci_fast(n-1)+fibonacci_fast(n-2)
return memo[n]
#print(fibonacci_fast(30))
slow_function()
print(fibonacci_fast(30))
def logger(func):
@functools.wraps(func)
def wrapper(*args,**kwargs):
print(f"calling {func.__name__} with args {args} ")
result=func(*args,**kwargs)
return result
return wrapper
def validator(func):
@functools.wraps(func)
def wrapper(*args,**kwargs):
for i in args:
if i<0:
print("invalid input!")
return None
for j in kwargs.values():
if j<0:
return None
return func(*args,**kwargs)
return wrapper
@logger
@validator
def add(a,b):
result=a+b
return result
print(add(9,6))
@logger
@validator
def multiply(n,x):
result=n*x
return result
print(multiply(6,5))