This document describes the current stable version of Celery (5.0). For development docs, go here.
celery.utils.functional¶
Functional-style utilties.
-
class
celery.utils.functional.LRUCache(limit=None)[source]¶ LRU Cache implementation using a doubly linked list to track access.
- Parameters
limit (int) – 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.
-
iteritems()¶
-
iterkeys()¶
-
itervalues()¶
-
popitem() → (k, v), remove and return some (key, value) pair[source]¶ as a 2-tuple; but raise KeyError if D is empty.
-
celery.utils.functional.chunks(it, n)[source]¶ Split an iterator into chunks with n elements each.
Warning
itmust be an actual iterator, if you pass this a concrete sequence will get you repeating elements.So
chunks(iter(range(1000)), 10)is fine, butchunks(range(1000), 10)is not.Example
# 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]]
-
celery.utils.functional.dictfilter(d=None, **kw)[source]¶ Remove all keys from dict
dwhose value isNone.
-
celery.utils.functional.first(predicate, it)[source]¶ Return the first element in
itthatpredicateaccepts.If
predicateis None it will return the first item that’s notNone.
-
celery.utils.functional.firstmethod(method, on_call=None)[source]¶ Multiple dispatch.
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 (
lazy.)
-
celery.utils.functional.fun_accepts_kwargs(fun)[source]¶ Return true if function accepts arbitrary keyword arguments.
-
celery.utils.functional.head_from_fun(fun, bound=False, debug=False)[source]¶ Generate signature function from actual function.
-
celery.utils.functional.is_list(obj, scalars=(<class 'collections.abc.Mapping'>, <class 'str'>), iters=(<class 'collections.abc.Iterable'>, ))[source]¶ Return true if the object is iterable.
Note
Returns false if object is a mapping or string.
-
class
celery.utils.functional.lazy(fun, *args, **kwargs)[source]¶ Holds lazy evaluation.
Evaluated when called or if the
evaluate()method is called. The function is re-evaluated on every call.- Overloaded operations that will evaluate the promise:
__str__(),__repr__(),__cmp__().
-
celery.utils.functional.mattrgetter(*attrs)[source]¶ Get attributes, ignoring attribute errors.
Like
operator.itemgetter()but returnNoneon missing attributes instead of raisingAttributeError.
-
celery.utils.functional.maybe_evaluate(value)[source]¶ Evaluate value only if value is a
lazyinstance.
-
celery.utils.functional.maybe_list(obj, scalars=(<class 'collections.abc.Mapping'>, <class 'str'>))[source]¶ Return list of one element if
lis a scalar.
-
celery.utils.functional.memoize(maxsize=None, keyfun=None, Cache=<class 'kombu.utils.functional.LRUCache'>)[source]¶ Decorator to cache function return value.
-
class
celery.utils.functional.mlazy(fun, *args, **kwargs)[source]¶ Memoized lazy evaluation.
The function is only evaluated once, every subsequent access will return the same value.
-
evaluated= False¶ Set to
Trueafter the object has been evaluated.
-
-
celery.utils.functional.noop(*args, **kwargs)[source]¶ No operation.
Takes any arguments/keyword arguments and does nothing.
-
celery.utils.functional.padlist(container, size, default=None)[source]¶ Pad list with default elements.
Example
>>> 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')