functional.py 8.0 KB

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