Welcome to jaraco.functools documentation!

class jaraco.functools.Throttler(func, max_rate=inf)

Bases: object

Rate-limit a function (or other callable)

reset()
jaraco.functools.apply(transform)

Decorate a function with a transform function that is invoked on results returned from the decorated function.

>>> @apply(reversed)
... def get_numbers(start):
...     return range(start, start+3)
>>> list(get_numbers(4))
[6, 5, 4]
jaraco.functools.assign_params(func, namespace)

Assign parameters from namespace where func solicits.

>>> def func(x, y=3):
...     print(x, y)
>>> assigned = assign_params(func, dict(x=2, z=4))
>>> assigned()
2 3
jaraco.functools.call_aside(f, *args, **kwargs)

Call a function for its side effect after initialization.

>>> @call_aside
... def func(): print("called")
called
>>> func()
called

Use functools.partial to pass parameters to the initial call

>>> @functools.partial(call_aside, name='bingo')
... def func(name): print("called with", name)
called with bingo
jaraco.functools.compose(*funcs)

Compose any number of unary functions into a single unary function.

>>> import textwrap
>>> from six import text_type
>>> text_type.strip(textwrap.dedent(compose.__doc__)) == compose(text_type.strip, textwrap.dedent)(compose.__doc__)
True

Compose also allows the innermost function to take arbitrary arguments.

>>> round_three = lambda x: round(x, ndigits=3)
>>> f = compose(round_three, int.__truediv__)
>>> [f(3*x, x+1) for x in range(1,10)]
[1.5, 2.0, 2.25, 2.4, 2.5, 2.571, 2.625, 2.667, 2.7]
jaraco.functools.first_invoke(func1, func2)

Return a function that when invoked will invoke func1 without any parameters (for its side-effect) and then invoke func2 with whatever parameters were passed, returning its result.

jaraco.functools.method_cache(method, cache_wrapper=None)

Wrap lru_cache to support storing the cache data in the object instances.

Abstracts the common paradigm where the method explicitly saves an underscore-prefixed protected property on first call and returns that subsequently.

>>> class MyClass:
...     calls = 0
...
...     @method_cache
...     def method(self, value):
...         self.calls += 1
...         return value
>>> a = MyClass()
>>> a.method(3)
3
>>> for x in range(75):
...     res = a.method(x)
>>> a.calls
75

Note that the apparent behavior will be exactly like that of lru_cache except that the cache is stored on each instance, so values in one instance will not flush values from another, and when an instance is deleted, so are the cached values for that instance.

>>> b = MyClass()
>>> for x in range(35):
...     res = b.method(x)
>>> b.calls
35
>>> a.method(0)
0
>>> a.calls
75

Note that if method had been decorated with functools.lru_cache(), a.calls would have been 76 (due to the cached value of 0 having been flushed by the ‘b’ instance).

Clear the cache with .cache_clear()

>>> a.method.cache_clear()

Another cache wrapper may be supplied:

>>> cache = lru_cache(maxsize=2)
>>> MyClass.method2 = method_cache(lambda self: 3, cache_wrapper=cache)
>>> a = MyClass()
>>> a.method2()
3

Caution - do not subsequently wrap the method with another decorator, such as @property, which changes the semantics of the function.

See also http://code.activestate.com/recipes/577452-a-memoize-decorator-for-instance-methods/ for another implementation and additional justification.

jaraco.functools.method_caller(method_name, *args, **kwargs)

Return a function that will call a named method on the target object with optional positional and keyword arguments.

>>> lower = method_caller('lower')
>>> lower('MyString')
'mystring'
jaraco.functools.once(func)

Decorate func so it’s only ever called the first time.

This decorator can ensure that an expensive or non-idempotent function will not be expensive on subsequent calls and is idempotent.

>>> func = once(lambda a: a+3)
>>> func(3)
6
>>> func(9)
6
>>> func('12')
6
jaraco.functools.pass_none(func)

Wrap func so it’s not called if its first param is None

>>> print_text = pass_none(print)
>>> print_text('text')
text
>>> print_text(None)
jaraco.functools.print_yielded(func)

Convert a generator into a function that prints all yielded elements

>>> @print_yielded
... def x():
...     yield 3; yield None
>>> x()
3
None
jaraco.functools.retry_call(func, cleanup=<function <lambda>>, retries=0, trap=())

Given a callable func, trap the indicated exceptions for up to ‘retries’ times, invoking cleanup on the exception. On the final attempt, allow any exceptions to propagate.

Indices and tables