This document describes Celery 2.4. For development docs, go here.

celery.utils

celery.utils

Utility functions.

copyright:
  1. 2009 - 2011 by Ask Solem.
license:

BSD, see LICENSE for more details.

celery.utils.abbr(S, max, ellipsis='...')
celery.utils.abbrtask(S, max)
celery.utils.chunks(it, n)

Split an iterator into chunks with n elements each.

Examples

# 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.cry()

Return stacktrace of all active threads.

From https://gist.github.com/737056

celery.utils.cwd_in_path(*args, **kwds)
celery.utils.deprecated(description=None, deprecation=None, removal=None, alternative=None)
celery.utils.find_module(module, path=None, imp=None)

Version of imp.find_module() supporting dots.

celery.utils.first(predicate, iterable)

Returns the first element in iterable that predicate returns a True value for.

celery.utils.firstmethod(method)

Returns a functions that with a list of instances, finds the first instance that returns a value for the given method.

The list can also contain promises (promise.)

celery.utils.fun_takes_kwargs(fun, kwlist=[])

With a function, and a list of keyword arguments, returns arguments in the list which the function takes.

If the object has an argspec attribute that is used instead of using the inspect.getargspec() introspection.

Parameters:
  • fun – The function to inspect arguments of.
  • kwlist – The list of keyword arguments.

Examples

>>> def foo(self, x, y, logfile=None, loglevel=None):
...     return x * y
>>> fun_takes_kwargs(foo, ["logfile", "loglevel", "task_id"])
["logfile", "loglevel"]
>>> def foo(self, x, y, **kwargs):
>>> fun_takes_kwargs(foo, ["logfile", "loglevel", "task_id"])
["logfile", "loglevel", "task_id"]
celery.utils.get_cls_by_name(name, aliases={}, imp=None, package=None, **kwargs)

Get class by name.

The name should be the full dot-separated path to the class:

modulename.ClassName

Example:

celery.concurrency.processes.TaskPool
                            ^- class name

If aliases is provided, a dict containing short name/long name mappings, the name is looked up in the aliases first.

Examples:

>>> get_cls_by_name("celery.concurrency.processes.TaskPool")
<class 'celery.concurrency.processes.TaskPool'>
>>> get_cls_by_name("default", {
...     "default": "celery.concurrency.processes.TaskPool"})
<class 'celery.concurrency.processes.TaskPool'>

# Does not try to look up non-string names. >>> from celery.concurrency.processes import TaskPool >>> get_cls_by_name(TaskPool) is TaskPool True

celery.utils.get_full_cls_name(cls)

With a class, get its full module and class name.

celery.utils.get_symbol_by_name(name, aliases={}, imp=None, package=None, **kwargs)

Get class by name.

The name should be the full dot-separated path to the class:

modulename.ClassName

Example:

celery.concurrency.processes.TaskPool
                            ^- class name

If aliases is provided, a dict containing short name/long name mappings, the name is looked up in the aliases first.

Examples:

>>> get_cls_by_name("celery.concurrency.processes.TaskPool")
<class 'celery.concurrency.processes.TaskPool'>
>>> get_cls_by_name("default", {
...     "default": "celery.concurrency.processes.TaskPool"})
<class 'celery.concurrency.processes.TaskPool'>

# Does not try to look up non-string names. >>> from celery.concurrency.processes import TaskPool >>> get_cls_by_name(TaskPool) is TaskPool True

celery.utils.import_from_cwd(module, imp=None, package=None)

Import module, but make sure it finds modules located in the current directory.

Modules located in the current directory has precedence over modules located in sys.path.

celery.utils.instantiate(name, *args, **kwargs)

Instantiate class by name.

See get_cls_by_name().

celery.utils.is_iterable(obj)
celery.utils.isatty(fh)
celery.utils.kwdict(kwargs)

Make sure keyword arguments are not in unicode.

This should be fixed in newer Python versions, see: http://bugs.python.org/issue4978.

celery.utils.lpmerge(L, R)

Left precedent dictionary merge. Keeps values from l, if the value in r is None.

celery.utils.mattrgetter(*attrs)

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

celery.utils.maybe_promise(value)

Evaluates if the value is a promise.

class celery.utils.mpromise(fun, *args, **kwargs)

Memoized promise.

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

evaluated

Set to to True after the promise has been evaluated.

evaluate()
evaluated = False
celery.utils.noop(*args, **kwargs)

No operation.

Takes any arguments/keyword arguments and does nothing.

celery.utils.padlist(container, size, default=None)

Pad list with default elements.

Examples:

>>> 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")
class celery.utils.promise(fun, *args, **kwargs)

A promise.

Evaluated when called or if the evaluate() method is called. The function is evaluated on every access, so the value is not memoized (see mpromise).

Overloaded operations that will evaluate the promise:
__str__(), __repr__(), __cmp__().
evaluate()
celery.utils.reprcall(name, args=(), kwargs=(), sep=', ')
celery.utils.reprkwargs(kwargs, sep=', ', fmt='%s=%s')
celery.utils.textindent(t, indent=0)

Indent text.

celery.utils.truncate_text(text, maxlen=128, suffix='...')

Truncates text to a maximum number of characters.

celery.utils.warn_deprecated(description=None, deprecation=None, removal=None, alternative=None)