-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_decorator.py
More file actions
executable file
·105 lines (80 loc) · 2.17 KB
/
test_decorator.py
File metadata and controls
executable file
·105 lines (80 loc) · 2.17 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
from functools import wraps
# usage of (*args, **kwargs)
def test_func(x, y, *args, **kwargs):
print(x)
print(y)
print(args)
print(kwargs.pop('res', None))
print(kwargs.get('ele', None))
print(kwargs)
input_1 = [90, 43, 56]
input_2 = {'res': 23, 'ele': 70}
test_func(12, 23, *input_1, **input_2)
# decorator function
def printDecorator(func):
@wraps(func)
def wrapped_func(*args, **kwargs):
print(f'wrapped function {func.__name__}')
return func(*args, **kwargs)
return wrapped_func
@printDecorator
def say_hi(name: str):
print('Hi ' + name)
return 'Done'
def get_hi_func():
def hi():
print('hi')
return hi
hi_func = get_hi_func()
print(hi_func, hi_func())
print('==========')
print(say_hi('Jax'))
print(say_hi.__name__)
# decorator class
class logIt(object):
def __init__(self, logfile: str = '/home') -> None:
self._logfile = logfile
def __call__(self, func):
@wraps(func)
def wrapped_func(*args, **kwargs):
log_string = func.__name__ + self._logfile
print('logging into: ' + log_string)
self.notify()
return func(*args, **kwargs)
return wrapped_func
def notify(self):
pass
class EmailLogIt(logIt):
def __init__(self, email='admin@myproject.com', *args, **kwargs) -> None:
super(EmailLogIt, self).__init__(*args, **kwargs)
self._email = email
def notify(self):
print('send email to ' + self._email)
return super().notify()
@logIt()
def fun_o(x, y):
return x + y
@EmailLogIt('todo@todo.com', '/data')
def fun_x(x, y):
return x + y
print('==========')
fun_o(1, 2)
fun_x(1, 2)
def repeat(times):
''' call a function a number of times '''
def decorate(fn):
@wraps(fn)
def wrapper(*args, **kwargs):
for _ in range(times):
result = fn(*args, **kwargs)
return result
return wrapper
return decorate
@repeat(10)
def say(message):
''' print the message
Arguments
message: the message to show
'''
print(message)
say('Hello')