-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_decorator.py
175 lines (124 loc) · 4.19 KB
/
test_decorator.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
# def hi(name = "bairui"):
# print("now you are inside the hi() functon")
# # nested function
# def greet():
# return "now you are in the greet() function"
# def welcome():
# return "now your are in the welcome() function"
# print("now you are back in the hi() function")
# if name == "bairui":
# return greet
# else:
# return welcome
# greet()
# a = hi()
# print(a)
# print(a())
# def hi():
# return "hi yasoob!"
# def doSomethingBeforeHi(func):
# print("I am doing some boring work before executing hi()")
# print(func())
# doSomethingBeforeHi(hi)
# Decorators let you execute code before and after a function.
# def a_new_decorator(a_func):
# def wrapTheFunction():
# print("I am doing some boring work before executing a_func()")
# a_func()
# print("I am doing some boring work after executing a_func()")
# return wrapTheFunction
# def a_function_requiring_decoration():
# print("I am the function which needs some decoration to remove my foul smell")
# a_function_requiring_decoration()
# a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)
# a_function_requiring_decoration()
# @a_new_decorator
# def a_function_requiring_decoration():
# print("I am the function which needs some decoration to remove my foul smell")
# a_function_requiring_decoration()
# @a_new_decorator
# def a_function_requiring_decoration():
# print("I am the function which needs some decoration to remove my foul smell")
# # the @a_new_decorator is just a short way of saying
# # a_function_requiring_decoration = a_new_decorator(a_function_requiring_decoration)
# print(a_function_requiring_decoration.__name__)
# # Output: wrapTheFunction
from functools import wraps
def a_new_decorator(a_func):
@wraps(a_func)
def wrapTheFunction():
print("I am doing some boring work before executing a_func()")
a_func()
print("I am doing some boring work after executing a_func()")
return wrapTheFunction
@a_new_decorator
def a_function_requiring_decoration():
print("I am the function which needs some decoration to remove my foul smell")
print(a_function_requiring_decoration.__name__)
from functools import wraps
def decorator_name(f):
@wraps(f)
def decorated(*args, **kwargs):
if not can_run:
return "Function will not run"
return f(*args, **kwargs)
return decorated
@decorator_name
def func():
return("Function is running")
can_run = True
print(func())
# Output: Function is running
can_run = False
print(func())
# Output: Function will not run
#use-cases
# from functools import wraps
# def requires_auth(f):
# @wraps(f)
# def decorated(*args, **kwargs):
# auth = request.authorization
# if not auth or not check_auth(auth.username, auth.password):
# authenticate()
# return f(*args, **kwargs)
# return decorated
# from functools import wraps
# def logit(func):
# @wraps(func)
# def with_logging(*args, **kwargs):
# print(func.__name__ + " was called")
# return func(*args, **kwargs)
# return with_logging
# @logit
# def addition_func(x):
# """Do some math."""
# return x + x
# result = addition_func(4)
# # Output: addition_func was called
from functools import wraps
def logit(logfile='out.log'):
def logging_decorator(func):
@wraps(func)
def wrapped_function(*args, **kwargs):
log_string = func.__name__ + " was called"
print(log_string)
# Open the logfile and append
with open(logfile, 'a') as opened_file:
# Now we log to the specified logfile
opened_file.write(log_string + '\n')
return func(*args, **kwargs)
return wrapped_function
return logging_decorator
@logit()
def myfunc1():
pass
myfunc1 = logit()(myfunc1) # logging_decorator(myfunc1) => wrapped_function
myfunc1()
# Output: myfunc1 was called
# A file called out.log now exists, with the above string
@logit(logfile='func2.log')
def myfunc2():
pass
myfunc2()
# Output: myfunc2 was called
# A file called func2.log now exists, with the above string