functional.py 7.0 KB

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