-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecorator_chaining_example.py
More file actions
93 lines (68 loc) · 2.57 KB
/
Copy pathdecorator_chaining_example.py
File metadata and controls
93 lines (68 loc) · 2.57 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
def register(*decorators):
"""
This decorator is for chaining multiple decorators.
:param decorators:args(the decorators as arguments)
:return: callable object
"""
def register_wrapper(func):
for deco in decorators[::-1]:
func = deco(func)
func._decorators = decorators
return func
return register_wrapper
def deco1(f):
def wrapper(*args, **kwds):
print('-' * 100)
fn = f(*args, **kwds)
print('-' * 100)
return fn
return wrapper
def deco2(f):
def wrapper(*args, **kwds):
print('*' * 100)
fn = f(*args, **kwds)
print('*' * 100)
return fn
return wrapper
def deco3(f):
def wrapper(*args, **kwds):
print('#' * 100)
fn = f(*args, **kwds)
print('#' * 100)
return fn
return wrapper
class Foo(object):
@deco1
@deco2
@deco3
def bar(self):
print('I am bar')
class AnotherFoo(object):
@register(deco1, deco2, deco3)
def bar(self):
print('I am bar')
foo = Foo()
foo.bar()
print('\n\n~~~~ Alternate Way to Annotate ~~~~\n\n')
another_foo = AnotherFoo()
another_foo.bar()
print(another_foo.bar._decorators)
"""
output:
----------------------------------------------------------------------------------------------------
****************************************************************************************************
####################################################################################################
I am bar
####################################################################################################
****************************************************************************************************
----------------------------------------------------------------------------------------------------
~~~~ Alternate Way to Annotate ~~~~
----------------------------------------------------------------------------------------------------
****************************************************************************************************
####################################################################################################
I am bar
####################################################################################################
****************************************************************************************************
----------------------------------------------------------------------------------------------------
(<function deco1 at 0x7f50c7e6c940>, <function deco2 at 0x7f50c7e6c9d0>, <function deco3 at 0x7f50c7e6ca60>)
"""