functional.py 7.0 KB

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