-
Notifications
You must be signed in to change notification settings - Fork 0
/
t2_lazy_decorator.py
189 lines (136 loc) · 5.42 KB
/
t2_lazy_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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
#!/usr/bin/env python
from __future__ import division
from functools import wraps
import unittest
def make_decorator_lazy(decorator, apply_on_each_call = False):
"""
Make decorator lazy, so that it would be applied right before first run of decorated function.
"""
@wraps(decorator)
def new_lazy_decorator(f):
lazy_decorated = []
@wraps(f)
def decorated(*args, **kwargs):
if apply_on_each_call:
return decorator(f)(*args, **kwargs)
else:
if not lazy_decorated:
lazy_decorated.append(decorator(f))
return lazy_decorated[0](*args, **kwargs)
return decorated
return new_lazy_decorator
def make_decorator_returning_function_returning_lazy_decorator(decorator_factory, apply_on_each_call = False):
"""
Make function returning decorators return lazy decorators,
so that each of them would be applied right before first run of decorated function.
"""
@wraps(decorator_factory)
def new_factory(*fargs, **fkwargs):
def new_lazy_decorator(f):
lazy_decorated = []
@wraps(f)
def decorated(*args, **kwargs):
if apply_on_each_call:
return decorator_factory(*fargs, **fkwargs)(f)(*args, **kwargs)
else:
if not lazy_decorated:
lazy_decorated.append(decorator_factory(*fargs, **fkwargs)(f))
return lazy_decorated[0](*args, **kwargs)
return decorated
return new_lazy_decorator
return new_factory
class Tests_decorator(unittest.TestCase):
def setUp(self):
def decorator(f):
self.cumulative += 1
@wraps(f)
def new_decorated_f(*args, **kwargs):
return f(*args, **kwargs)
return new_decorated_f
self.decorator = decorator
def f():
pass
self.f = f
def test_without_lazy_decorator(self):
self.cumulative = 0
decorated = self.decorator(self.f)
self.assertEqual(self.cumulative, 1)
decorated()
self.assertEqual(self.cumulative, 1)
def test_with_lazy_decorator(self):
self.cumulative = 0
decorator_made_lazy = make_decorator_lazy(self.decorator)
self.assertEqual(self.cumulative, 0)
decorated = decorator_made_lazy(self.f)
self.assertEqual(self.cumulative, 0)
decorated()
self.assertEqual(self.cumulative, 1)
def test_with_lazy_decorator_two_calls(self):
self.cumulative = 0
decorator_made_lazy = make_decorator_lazy(self.decorator)
self.assertEqual(self.cumulative, 0)
decorated = decorator_made_lazy(self.f)
self.assertEqual(self.cumulative, 0)
decorated()
self.assertEqual(self.cumulative, 1)
decorated()
self.assertEqual(self.cumulative, 1)
def test_with_lazy_decorator_two_calls_with_application_on_each_call(self):
self.cumulative = 0
decorator_made_lazy = make_decorator_lazy(self.decorator, apply_on_each_call=True)
self.assertEqual(self.cumulative, 0)
decorated = decorator_made_lazy(self.f)
self.assertEqual(self.cumulative, 0)
decorated()
self.assertEqual(self.cumulative, 1)
decorated()
self.assertEqual(self.cumulative, 2)
class Tests_factory(unittest.TestCase):
def setUp(self):
def factory(option):
def decorator(f):
self.cumulative += 1
@wraps(f)
def new_decorated_f(*args, **kwargs):
return f(*args, **kwargs)
return new_decorated_f
return decorator
self.factory = factory
def f():
pass
self.f = f
def test_without_lazy_decorator(self):
self.cumulative = 0
decorated = self.factory(10)(self.f)
self.assertEqual(self.cumulative, 1)
decorated()
self.assertEqual(self.cumulative, 1)
def test_with_lazy_decorator(self):
self.cumulative = 0
decorator_factory_made_lazy = make_decorator_returning_function_returning_lazy_decorator(self.factory)
self.assertEqual(self.cumulative, 0)
decorated = decorator_factory_made_lazy(10)(self.f)
self.assertEqual(self.cumulative, 0)
decorated()
self.assertEqual(self.cumulative, 1)
def test_with_lazy_decorator_two_calls(self):
self.cumulative = 0
decorator_factory_made_lazy = make_decorator_returning_function_returning_lazy_decorator(self.factory)
self.assertEqual(self.cumulative, 0)
decorated = decorator_factory_made_lazy(10)(self.f)
self.assertEqual(self.cumulative, 0)
decorated()
self.assertEqual(self.cumulative, 1)
decorated()
self.assertEqual(self.cumulative, 1)
def test_with_lazy_decorator_two_calls_with_application_on_each_call(self):
self.cumulative = 0
decorator_factory_made_lazy = make_decorator_returning_function_returning_lazy_decorator(self.factory,
apply_on_each_call=True)
self.assertEqual(self.cumulative, 0)
decorated = decorator_factory_made_lazy(10)(self.f)
self.assertEqual(self.cumulative, 0)
decorated()
self.assertEqual(self.cumulative, 1)
decorated()
self.assertEqual(self.cumulative, 2)