-
Notifications
You must be signed in to change notification settings - Fork 0
/
util.py
248 lines (185 loc) · 7.1 KB
/
util.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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
import collections
from functools import partial
from time import time
from isoweb_time import clock
from twisted.internet import task, reactor
_class_cache = {}
def load_class(classpath):
mod, cls = classpath.rsplit('.', 1)
val = getattr(__import__(mod), cls)
_class_cache[classpath] = val
return val
def get_class(classpath):
return _class_cache.get(classpath, load_class(classpath))
class OrderedDefaultdict(collections.OrderedDict):
def __init__(self, *args, **kwargs):
if not args:
self.default_factory = None
else:
if not (args[0] is None or callable(args[0])):
raise TypeError('first argument must be callable or None')
self.default_factory = args[0]
args = args[1:]
super(OrderedDefaultdict, self).__init__(*args, **kwargs)
def __missing__ (self, key):
if self.default_factory is None:
raise KeyError(key)
self[key] = default = self.default_factory()
return default
def __reduce__(self): # optional, for pickle support
args = (self.default_factory,) if self.default_factory else ()
return self.__class__, args, None, None, self.items()
def memodict(f):
""" (FAST) Memoization decorator for a function taking a single argument """
class memodict(dict):
def __missing__(self, key):
ret = self[key] = f(key)
return ret
return memodict().__getitem__
class memoize:
"""cache the return value of a method
This class is meant to be used as a decorator of methods. The return value
from a given method invocation will be cached on the instance whose method
was invoked. All arguments passed to a method decorated with memoize must
be hashable.
If a memoized method is invoked directly on its class the result will not
be cached. Instead the method will be invoked like a static method:
class Obj:
@memoize
def add_to(self, arg):
return self + arg
Obj.add_to(1) # not enough arguments
Obj.add_to(1, 2) # returns 3, result is not cached
"""
def __init__(self, func):
self.func = func
def __get__(self, obj, objtype=None):
if obj is None:
return self.func
return partial(self, obj)
def __call__(self, *args, **kw):
obj = args[0]
try:
cache = obj._memo_cache
except AttributeError:
cache = obj._memo_cache = {}
key = (self.func, args[1:], frozenset(kw.items()))
try:
res = cache[key]
except KeyError:
res = cache[key] = self.func(*args, **kw)
return res
class AttributeDict(dict):
def __getattribute__(self, item):
return dict.get(self, item)
def __setattr__(self, key, value):
self[key] = value
def freeze_dict(d):
def _filter(val):
if isinstance(val, dict):
return freeze_dict(val)
return val
return frozenset(((k, _filter(v)) for k, v in d.items()))
def refreeze(obj):
def _filter(x):
if isinstance(x, (list, tuple, set)):
return refreeze(x)
return x
return frozenset(_filter(x) for x in obj)
def noop():
pass
def sleep(seconds, callback=noop):
return task.deferLater(reactor, seconds, callback)
def to_bytes(data):
if isinstance(data, list):
return [x.encode('utf8') if isinstance(x, str) else x for x in data]
elif isinstance(data, str):
return data.encode('utf8')
else:
return data
class TrackedDictionary(dict):
"""
Tracks a timestamp for every item.
Provides method `get_modified_after(timestamp)` to return a sub-dictionary
containing only values that have changed after `timestamp`.
"""
_tracked_parent = None
DELETED = object()
INVALID = object()
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._tracking = {k: -1 for k in self.keys()}
self.modified = clock()
def __setitem__(self, key, value):
if isinstance(value, TrackedDictionary) and (not value._tracked_parent or value._tracked_parent[1] != self):
value._tracked_parent = (key, self)
elif isinstance(value, dict):
# we are borg
value = TrackedDictionary(value)
value._tracked_parent = (key, self)
if value != self.get(key, TrackedDictionary.INVALID):
self.touch(key)
super().__setitem__(key, value)
def __delitem__(self, key):
self.touch(key)
super().__delitem__(key)
def erase(self, key):
"""
delete a key completely, including tracking information
"""
super().__delitem__(key)
del self._tracking[key]
def touch(self, key):
self._tracking[key] = self.modified = clock()
try:
_k, _p = self._tracked_parent
_p.touch(_k)
except (TypeError, ):
pass
def get_modified_after(self, when):
def _unpack(v):
return v.get_modified_after(when) if isinstance(v, TrackedDictionary) else v
return {k: _unpack(self.get(k, TrackedDictionary.DELETED)) for k, d in self._tracking.items() if d > when}
class TrackAttributes:
tracked_attributes = frozenset()
on_tracked_attribute_changed = None
def __init__(self, *args, **kwargs):
object.__setattr__(self, '_tracking', TrackedDictionary())
[self.__setattr__(k, getattr(self, k)) for k in self.tracked_attributes]
super().__init__()
def __setattr__(self, key, value):
changed = value != getattr(self, key, TrackedDictionary.INVALID)
super().__setattr__(key, value)
if key in self.tracked_attributes:
if value == self.get_original_value(key):
# delete and save memory!
try:
self._tracking.erase(key)
except KeyError:
return
else:
self._tracking[key] = value
if changed and self.on_tracked_attribute_changed:
self.on_tracked_attribute_changed(key)
def get_tracked_attributes(self, after=-1):
if after == -1:
return {k: getattr(self, k) for k in self.tracked_attributes}
def filter_deleted(k, v):
return k, v if v != TrackedDictionary.DELETED else getattr(self, k)
return dict(
(filter_deleted(k, v)
for k, v in self._tracking.get_modified_after(after).items())
)
def get_persists(self, after=-1):
if after == -1:
return {k: getattr(self, k) for k in self.persists}
return {k: v for k, v in self.get_tracked_attributes(after).items() if k in self.persists}
def get_exports(self, after=-1):
if after == -1:
return {k: getattr(self, k) for k in self.exports}
return {k: v for k, v in self.get_tracked_attributes(after).items() if k in self.exports}
clock_time_start = time() - clock()
def clock_to_time(clock_value):
return clock_time_start + clock_value
def time_to_clock(time_value):
return time_value - clock_time_start