functional.py 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325
  1. # -*- coding: utf-8 -*-
  2. """
  3. celery.utils.functional
  4. ~~~~~~~~~~~~~~~~~~~~~~~
  5. Utilities for functions.
  6. """
  7. from __future__ import absolute_import
  8. import sys
  9. import threading
  10. from functools import wraps
  11. from itertools import islice
  12. from kombu.utils import cached_property
  13. from kombu.utils.functional import lazy, maybe_evaluate, is_list, maybe_list
  14. from kombu.utils.compat import OrderedDict
  15. from celery.five import UserDict, UserList, items, keys, range
  16. __all__ = ['LRUCache', 'is_list', 'maybe_list', 'memoize', 'mlazy', 'noop',
  17. 'first', 'firstmethod', 'chunks', 'padlist', 'mattrgetter', 'uniq',
  18. 'regen', 'dictfilter', 'lazy', 'maybe_evaluate']
  19. IS_PYPY = hasattr(sys, 'pypy_version_info')
  20. KEYWORD_MARK = object()
  21. class DummyContext(object):
  22. def __enter__(self):
  23. return self
  24. def __exit__(self, *exc_info):
  25. pass
  26. class LRUCache(UserDict):
  27. """LRU Cache implementation using a doubly linked list to track access.
  28. :keyword limit: The maximum number of keys to keep in the cache.
  29. When a new key is inserted and the limit has been exceeded,
  30. the *Least Recently Used* key will be discarded from the
  31. cache.
  32. """
  33. def __init__(self, limit=None):
  34. self.limit = limit
  35. self.mutex = threading.RLock()
  36. self.data = OrderedDict()
  37. def __getitem__(self, key):
  38. with self.mutex:
  39. value = self[key] = self.data.pop(key)
  40. return value
  41. def update(self, *args, **kwargs):
  42. with self.mutex:
  43. data, limit = self.data, self.limit
  44. data.update(*args, **kwargs)
  45. if limit and len(data) > limit:
  46. # pop additional items in case limit exceeded
  47. for _ in range(len(data) - limit):
  48. data.popitem(last=False)
  49. def popitem(self, last=True, _needs_lock=IS_PYPY):
  50. if not _needs_lock:
  51. return self.data.popitem(last)
  52. with self.mutex:
  53. return self.data.popitem(last)
  54. def __setitem__(self, key, value):
  55. # remove least recently used key.
  56. with self.mutex:
  57. if self.limit and len(self.data) >= self.limit:
  58. self.data.pop(next(iter(self.data)))
  59. self.data[key] = value
  60. def __iter__(self):
  61. return iter(self.data)
  62. def _iterate_items(self, _need_lock=IS_PYPY):
  63. with self.mutex if _need_lock else DummyContext():
  64. for k in self:
  65. try:
  66. yield (k, self.data[k])
  67. except KeyError: # pragma: no cover
  68. pass
  69. iteritems = _iterate_items
  70. def _iterate_values(self, _need_lock=IS_PYPY):
  71. with self.mutex if _need_lock else DummyContext():
  72. for k in self:
  73. try:
  74. yield self.data[k]
  75. except KeyError: # pragma: no cover
  76. pass
  77. itervalues = _iterate_values
  78. def _iterate_keys(self):
  79. # userdict.keys in py3k calls __getitem__
  80. return keys(self.data)
  81. iterkeys = _iterate_keys
  82. def incr(self, key, delta=1):
  83. with self.mutex:
  84. # this acts as memcached does- store as a string, but return a
  85. # integer as long as it exists and we can cast it
  86. newval = int(self.data.pop(key)) + delta
  87. self[key] = str(newval)
  88. return newval
  89. def __getstate__(self):
  90. d = dict(vars(self))
  91. d.pop('mutex')
  92. return d
  93. def __setstate__(self, state):
  94. self.__dict__ = state
  95. self.mutex = threading.RLock()
  96. if sys.version_info[0] == 3: # pragma: no cover
  97. keys = _iterate_keys
  98. values = _iterate_values
  99. items = _iterate_items
  100. else: # noqa
  101. def keys(self):
  102. return list(self._iterate_keys())
  103. def values(self):
  104. return list(self._iterate_values())
  105. def items(self):
  106. return list(self._iterate_items())
  107. def memoize(maxsize=None, keyfun=None, Cache=LRUCache):
  108. def _memoize(fun):
  109. mutex = threading.Lock()
  110. cache = Cache(limit=maxsize)
  111. @wraps(fun)
  112. def _M(*args, **kwargs):
  113. if keyfun:
  114. key = keyfun(args, kwargs)
  115. else:
  116. key = args + (KEYWORD_MARK, ) + tuple(sorted(kwargs.items()))
  117. try:
  118. with mutex:
  119. value = cache[key]
  120. except KeyError:
  121. value = fun(*args, **kwargs)
  122. _M.misses += 1
  123. with mutex:
  124. cache[key] = value
  125. else:
  126. _M.hits += 1
  127. return value
  128. def clear():
  129. """Clear the cache and reset cache statistics."""
  130. cache.clear()
  131. _M.hits = _M.misses = 0
  132. _M.hits = _M.misses = 0
  133. _M.clear = clear
  134. _M.original_func = fun
  135. return _M
  136. return _memoize
  137. class mlazy(lazy):
  138. """Memoized lazy evaluation.
  139. The function is only evaluated once, every subsequent access
  140. will return the same value.
  141. .. attribute:: evaluated
  142. Set to to :const:`True` after the object has been evaluated.
  143. """
  144. evaluated = False
  145. _value = None
  146. def evaluate(self):
  147. if not self.evaluated:
  148. self._value = super(mlazy, self).evaluate()
  149. self.evaluated = True
  150. return self._value
  151. def noop(*args, **kwargs):
  152. """No operation.
  153. Takes any arguments/keyword arguments and does nothing.
  154. """
  155. pass
  156. def first(predicate, it):
  157. """Return the first element in `iterable` that `predicate` Gives a
  158. :const:`True` value for.
  159. If `predicate` is None it will return the first item that is not None.
  160. """
  161. return next(
  162. (v for v in it if (predicate(v) if predicate else v is not None)),
  163. None,
  164. )
  165. def firstmethod(method):
  166. """Return a function that with a list of instances,
  167. finds the first instance that gives a value for the given method.
  168. The list can also contain lazy instances
  169. (:class:`~kombu.utils.functional.lazy`.)
  170. """
  171. def _matcher(it, *args, **kwargs):
  172. for obj in it:
  173. try:
  174. answer = getattr(maybe_evaluate(obj), method)(*args, **kwargs)
  175. except AttributeError:
  176. pass
  177. else:
  178. if answer is not None:
  179. return answer
  180. return _matcher
  181. def chunks(it, n):
  182. """Split an iterator into chunks with `n` elements each.
  183. Examples
  184. # n == 2
  185. >>> x = chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 2)
  186. >>> list(x)
  187. [[0, 1], [2, 3], [4, 5], [6, 7], [8, 9], [10]]
  188. # n == 3
  189. >>> x = chunks(iter([0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]), 3)
  190. >>> list(x)
  191. [[0, 1, 2], [3, 4, 5], [6, 7, 8], [9, 10]]
  192. """
  193. # XXX This function is not used anymore, at least not by Celery itself.
  194. for first in it:
  195. yield [first] + list(islice(it, n - 1))
  196. def padlist(container, size, default=None):
  197. """Pad list with default elements.
  198. Examples:
  199. >>> first, last, city = padlist(['George', 'Costanza', 'NYC'], 3)
  200. ('George', 'Costanza', 'NYC')
  201. >>> first, last, city = padlist(['George', 'Costanza'], 3)
  202. ('George', 'Costanza', None)
  203. >>> first, last, city, planet = padlist(
  204. ... ['George', 'Costanza', 'NYC'], 4, default='Earth',
  205. ... )
  206. ('George', 'Costanza', 'NYC', 'Earth')
  207. """
  208. return list(container)[:size] + [default] * (size - len(container))
  209. def mattrgetter(*attrs):
  210. """Like :func:`operator.itemgetter` but return :const:`None` on missing
  211. attributes instead of raising :exc:`AttributeError`."""
  212. return lambda obj: dict((attr, getattr(obj, attr, None))
  213. for attr in attrs)
  214. def uniq(it):
  215. """Return all unique elements in ``it``, preserving order."""
  216. seen = set()
  217. return (seen.add(obj) or obj for obj in it if obj not in seen)
  218. def regen(it):
  219. """Regen takes any iterable, and if the object is an
  220. generator it will cache the evaluated list on first access,
  221. so that the generator can be "consumed" multiple times."""
  222. if isinstance(it, (list, tuple)):
  223. return it
  224. return _regen(it)
  225. class _regen(UserList, list):
  226. # must be subclass of list so that json can encode.
  227. def __init__(self, it):
  228. self.__it = it
  229. def __reduce__(self):
  230. return list, (self.data, )
  231. def __length_hint__(self):
  232. return self.__it.__length_hint__()
  233. @cached_property
  234. def data(self):
  235. return list(self.__it)
  236. def dictfilter(d=None, **kw):
  237. """Remove all keys from dict ``d`` whose value is :const:`None`"""
  238. d = kw if d is None else (dict(d, **kw) if kw else d)
  239. return dict((k, v) for k, v in items(d) if v is not None)