This document is for Kombu's development version, which can be significantly different from previous releases. Get the stable docs here: 5.0.

Functional-style Utilities - kombu.utils.functional

Functional Utilities.

class kombu.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.

incr(key, delta=1)[source]
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[source]

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

update([E, ]**F) None.  Update D from mapping/iterable E and F.[source]

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
kombu.utils.functional.dictfilter(d=None, **kw)[source]

Remove all keys from dict d whose value is None.

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

evaluate()[source]
kombu.utils.functional.maybe_evaluate(value)[source]

Evaluate value only if value is a lazy instance.

kombu.utils.functional.maybe_list(obj, scalars=(<class 'collections.abc.Mapping'>, <class 'str'>))[source]

Return list of one element if l is a scalar.

kombu.utils.functional.memoize(maxsize=None, keyfun=None, Cache=<class 'kombu.utils.functional.LRUCache'>)[source]

Decorator to cache function return value.

kombu.utils.functional.retry_over_time(fun, catch, args=None, kwargs=None, errback=None, max_retries=None, interval_start=2, interval_step=2, interval_max=30, callback=None, timeout=None)[source]

Retry the function over and over until max retries is exceeded.

For each retry we sleep a for a while before we try again, this interval is increased for every retry until the max seconds is reached.

Parameters
  • fun (Callable) – The function to try

  • catch (Tuple[BaseException]) – Exceptions to catch, can be either tuple or a single exception class.

Keyword Arguments
  • args (Tuple) – Positional arguments passed on to the function.

  • kwargs (Dict) – Keyword arguments passed on to the function.

  • errback (Callable) – Callback for when an exception in catch is raised. The callback must take three arguments: exc, interval_range and retries, where exc is the exception instance, interval_range is an iterator which return the time in seconds to sleep next, and retries is the number of previous retries.

  • max_retries (int) – Maximum number of retries before we give up. If neither of this and timeout is set, we will retry forever. If one of this and timeout is reached, stop.

  • interval_start (float) – How long (in seconds) we start sleeping between retries.

  • interval_step (float) – By how much the interval is increased for each retry.

  • interval_max (float) – Maximum number of seconds to sleep between retries.

  • timeout (int) – Maximum seconds waiting before we give up.