celery.utils.functional

Functional-style utilities.

class celery.utils.functional.LRUCache(limit=None)[исходный код]

LRU Cache implementation using a doubly linked list to track access.

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.

incr(key, delta=1)[исходный код]
items() a set-like object providing a view on D's items
iteritems()
iterkeys()
itervalues()
keys() a set-like object providing a view on D's keys
popitem() (k, v), remove and return some (key, value) pair[исходный код]

as a 2-tuple; but raise KeyError if D is empty.

update([E, ]**F) None.  Update D from mapping/iterable E and F.[исходный код]

If E present and has a .keys() method, does: for k in E: D[k] = E[k] If E present and lacks .keys() method, does: for (k, v) in E: D[k] = v In either case, this is followed by: for k, v in F.items(): D[k] = v

values() an object providing a view on D's values
celery.utils.functional.chunks(it, n)[исходный код]

Split an iterator into chunks with n elements each.

Предупреждение

it must be an actual iterator, if you pass this a concrete sequence will get you repeating elements.

So chunks(iter(range(1000)), 10) is fine, but chunks(range(1000), 10) is not.

Пример

# 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)[исходный код]

Remove all keys from dict d whose value is None.

celery.utils.functional.first(predicate, it)[исходный код]

Return the first element in it that predicate accepts.

If predicate is None it will return the first item that’s not None.

celery.utils.functional.firstmethod(method, on_call=None)[исходный код]

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)[исходный код]

Return true if function accepts arbitrary keyword arguments.

celery.utils.functional.head_from_fun(fun: Callable[[...], Any], bound: bool = False) str[исходный код]

Generate signature function from actual function.

celery.utils.functional.is_list(obj, scalars=(<class 'collections.abc.Mapping'>, <class 'str'>), iters=(<class 'collections.abc.Iterable'>, ))[исходный код]

Return true if the object is iterable.

Returns false if object is a mapping or string.

class celery.utils.functional.lazy(fun, *args, **kwargs)[исходный код]

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__().

evaluate()[исходный код]
celery.utils.functional.mattrgetter(*attrs)[исходный код]

Get attributes, ignoring attribute errors.

Like operator.itemgetter() but return None on missing attributes instead of raising AttributeError.

celery.utils.functional.maybe(typ, val)[исходный код]

Call typ on value if val is defined.

celery.utils.functional.maybe_evaluate(value)[исходный код]

Evaluate value only if value is a lazy instance.

celery.utils.functional.maybe_list(obj, scalars=(<class 'collections.abc.Mapping'>, <class 'str'>))[исходный код]

Return list of one element if l is a scalar.

celery.utils.functional.memoize(maxsize=None, keyfun=None, Cache=<class 'kombu.utils.functional.LRUCache'>)[исходный код]

Decorator to cache function return value.

class celery.utils.functional.mlazy(fun, *args, **kwargs)[исходный код]

Memoized lazy evaluation.

The function is only evaluated once, every subsequent access will return the same value.

evaluate()[исходный код]
evaluated = False

Set to True after the object has been evaluated.

celery.utils.functional.noop(*args, **kwargs)[исходный код]

No operation.

Takes any arguments/keyword arguments and does nothing.

celery.utils.functional.padlist(container, size, default=None)[исходный код]

Pad list with default elements.

Пример

>>> 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')
celery.utils.functional.regen(it)[исходный код]

Convert iterator to an object that can be consumed multiple times.

Regen takes any iterable, and if the object is an generator it will cache the evaluated list on first access, so that the generator can be «consumed» multiple times.

celery.utils.functional.uniq(it)[исходный код]

Return all unique elements in it, preserving order.

Back to Top