/
functional.py
402 lines (307 loc) · 10.7 KB
/
functional.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
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
# -*- coding: utf-8 -*-
"""
celery.utils.functional
~~~~~~~~~~~~~~~~~~~~~~~
Utilities for functions.
"""
from __future__ import absolute_import, print_function
import sys
import threading
from collections import OrderedDict
from functools import partial, wraps
try:
from inspect import isfunction, getfullargspec as getargspec
except ImportError: # Py2
from inspect import isfunction, getargspec # noqa
from itertools import chain, islice
from kombu.utils.functional import (
dictfilter, lazy, maybe_evaluate, is_list, maybe_list,
)
from vine import promise
from celery.five import UserDict, UserList, keys, range
__all__ = ['LRUCache', 'is_list', 'maybe_list', 'memoize', 'mlazy', 'noop',
'first', 'firstmethod', 'chunks', 'padlist', 'mattrgetter', 'uniq',
'regen', 'dictfilter', 'lazy', 'maybe_evaluate', 'head_from_fun']
IS_PY3 = sys.version_info[0] == 3
IS_PY2 = sys.version_info[0] == 2
KEYWORD_MARK = object()
FUNHEAD_TEMPLATE = """
def {fun_name}({fun_args}):
return {fun_value}
"""
class DummyContext(object):
def __enter__(self):
return self
def __exit__(self, *exc_info):
pass
class LRUCache(UserDict):
"""LRU Cache implementation using a doubly linked list to track access.
:keyword limit: The maximum number of keys to keep in the cache.
When a new key is inserted and the limit has been exceeded,
the *Least Recently Used* key will be discarded from the
cache.
"""
def __init__(self, limit=None):
self.limit = limit
self.mutex = threading.RLock()
self.data = OrderedDict()
def __getitem__(self, key):
with self.mutex:
value = self[key] = self.data.pop(key)
return value
def update(self, *args, **kwargs):
with self.mutex:
data, limit = self.data, self.limit
data.update(*args, **kwargs)
if limit and len(data) > limit:
# pop additional items in case limit exceeded
for _ in range(len(data) - limit):
data.popitem(last=False)
def popitem(self, last=True):
with self.mutex:
return self.data.popitem(last)
def __setitem__(self, key, value):
# remove least recently used key.
with self.mutex:
if self.limit and len(self.data) >= self.limit:
self.data.pop(next(iter(self.data)))
self.data[key] = value
def __iter__(self):
return iter(self.data)
def _iterate_items(self):
with self.mutex:
for k in self:
try:
yield (k, self.data[k])
except KeyError: # pragma: no cover
pass
iteritems = _iterate_items
def _iterate_values(self):
with self.mutex:
for k in self:
try:
yield self.data[k]
except KeyError: # pragma: no cover
pass
itervalues = _iterate_values
def _iterate_keys(self):
# userdict.keys in py3k calls __getitem__
with self.mutex:
return keys(self.data)
iterkeys = _iterate_keys
def incr(self, key, delta=1):
with self.mutex:
# this acts as memcached does- store as a string, but return a
# integer as long as it exists and we can cast it
newval = int(self.data.pop(key)) + delta
self[key] = str(newval)
return newval
def __getstate__(self):
d = dict(vars(self))
d.pop('mutex')
return d
def __setstate__(self, state):
self.__dict__ = state
self.mutex = threading.RLock()
if sys.version_info[0] == 3: # pragma: no cover
keys = _iterate_keys
values = _iterate_values
items = _iterate_items
else: # noqa
def keys(self):
return list(self._iterate_keys())
def values(self):
return list(self._iterate_values())
def items(self):
return list(self._iterate_items())
def memoize(maxsize=None, keyfun=None, Cache=LRUCache):
def _memoize(fun):
cache = Cache(limit=maxsize)
@wraps(fun)
def _M(*args, **kwargs):
if keyfun:
key = keyfun(args, kwargs)
else:
key = args + (KEYWORD_MARK,) + tuple(sorted(kwargs.items()))
try:
value = cache[key]
except KeyError:
value = fun(*args, **kwargs)
_M.misses += 1
cache[key] = value
else:
_M.hits += 1
return value
def clear():
"""Clear the cache and reset cache statistics."""
cache.clear()
_M.hits = _M.misses = 0
_M.hits = _M.misses = 0
_M.clear = clear
_M.original_func = fun
return _M
return _memoize
class mlazy(lazy):
"""Memoized lazy evaluation.
The function is only evaluated once, every subsequent access
will return the same value.
.. attribute:: evaluated
Set to to :const:`True` after the object has been evaluated.
"""
evaluated = False
_value = None
def evaluate(self):
if not self.evaluated:
self._value = super(mlazy, self).evaluate()
self.evaluated = True
return self._value
def noop(*args, **kwargs):
"""No operation.
Takes any arguments/keyword arguments and does nothing.
"""
pass
def pass1(arg, *args, **kwargs):
return arg
def evaluate_promises(it):
for value in it:
if isinstance(value, promise):
value = value()
yield value
def first(predicate, it):
"""Return the first element in `iterable` that `predicate` Gives a
:const:`True` value for.
If `predicate` is None it will return the first item that is not None.
"""
return next(
(v for v in evaluate_promises(it) if (
predicate(v) if predicate is not None else v is not None)),
None,
)
def firstmethod(method):
"""Return a function that with a list of instances,
finds the first instance that gives a value for the given method.
The list can also contain lazy instances
(:class:`~kombu.utils.functional.lazy`.)
"""
def _matcher(it, *args, **kwargs):
for obj in it:
try:
answer = getattr(maybe_evaluate(obj), method)(*args, **kwargs)
except AttributeError:
pass
else:
if answer is not None:
return answer
return _matcher
def chunks(it, n):
"""Split an iterator into chunks with `n` elements each.
Examples
# n == 2
>>> x = chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 2)
>>> list(x)
[[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10]]
# n == 3
>>> x = chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 3)
>>> list(x)
[[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]]
"""
for first in it:
yield [first] + list(islice(it, n - 1))
def padlist(container, size, default=None):
"""Pad list with default elements.
Examples:
>>> first, last, city = padlist(['George', 'Costanza', 'NYC'], 3)
('George', 'Costanza', 'NYC')
>>> first, last, city = padlist(['George', 'Costanza'], 3)
('George', 'Costanza', None)
>>> first, last, city, planet = padlist(
... ['George', 'Costanza', 'NYC'], 4, default='Earth',
... )
('George', 'Costanza', 'NYC', 'Earth')
"""
return list(container)[:size] + [default] * (size - len(container))
def mattrgetter(*attrs):
"""Like :func:`operator.itemgetter` but return :const:`None` on missing
attributes instead of raising :exc:`AttributeError`."""
return lambda obj: {attr: getattr(obj, attr, None) for attr in attrs}
def uniq(it):
"""Return all unique elements in ``it``, preserving order."""
seen = set()
return (seen.add(obj) or obj for obj in it if obj not in seen)
def regen(it):
"""Regen takes any iterable, and if the object is an
generator it will cache the evaluated list on first access,
so that the generator can be "consumed" multiple times."""
if isinstance(it, (list, tuple)):
return it
return _regen(it)
class _regen(UserList, list):
# must be subclass of list so that json can encode.
def __init__(self, it):
self.__it = it
self.__index = 0
self.__consumed = []
def __reduce__(self):
return list, (self.data,)
def __length_hint__(self):
return self.__it.__length_hint__()
def __iter__(self):
return chain(self.__consumed, self.__it)
def __getitem__(self, index):
if index < 0:
return self.data[index]
try:
return self.__consumed[index]
except IndexError:
try:
for i in range(self.__index, index + 1):
self.__consumed.append(next(self.__it))
except StopIteration:
raise IndexError(index)
else:
return self.__consumed[index]
@property
def data(self):
try:
self.__consumed.extend(list(self.__it))
except StopIteration:
pass
return self.__consumed
def _argsfromspec(spec, replace_defaults=True):
if spec.defaults:
split = len(spec.defaults)
defaults = (list(range(len(spec.defaults))) if replace_defaults
else spec.defaults)
positional = spec.args[:-split]
optional = list(zip(spec.args[-split:], defaults))
else:
positional, optional = spec.args, []
if IS_PY3: # pragma: no cover
keywords = spec.varkw
elif IS_PY2:
keywords = spec.keywords # noqa
return ', '.join(filter(None, [
', '.join(positional),
', '.join('{0}={1}'.format(k, v) for k, v in optional),
'*{0}'.format(spec.varargs) if spec.varargs else None,
'**{0}'.format(keywords) if keywords else None,
]))
def head_from_fun(fun, bound=False, debug=False):
if not isfunction(fun) and hasattr(fun, '__call__'):
name, fun = fun.__class__.__name__, fun.__call__
else:
name = fun.__name__
definition = FUNHEAD_TEMPLATE.format(
fun_name=name,
fun_args=_argsfromspec(getargspec(fun)),
fun_value=1,
)
if debug: # pragma: no cover
print(definition, file=sys.stderr)
namespace = {'__name__': 'headof_{0}'.format(name)}
exec(definition, namespace)
result = namespace[name]
result._source = definition
if bound:
return partial(result, object())
return result