xoutil.future.functools - Higher-order functions and callable objects

This module extends the standard library’s functools. You may use it as a drop-in replacement in many cases.

Avoid importing * from this module since could be different in Python 2.7 and Python 3.3.

We added the following features.

class xoutil.future.functools.ctuple[source]

Simple tuple marker for compose().

Since is a callable you may use it directly in compose instead of changing your functions to returns ctuples instead of tuples:

>>> def compat_print(*args):
...     for arg in args:
...         print(arg)

>>> compose(compat_print, ctuple, list, range, math=False)(3)
0
1
2

# Without ctuple prints the list
>>> compose(compat_print, list, range, math=False)(10)
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

Deprecated since version 1.8.0: Use xoutil.fp.tool.pos_args.

xoutil.future.functools.compose(*funcs, math=True)[source]

Returns a function that is the composition of several callables.

By default compose behaves like mathematical function composition: this is to say that compose(f1, ... fn) is equivalent to lambda _x: fn(...(f1(_x))...).

If any “intermediate” function returns a ctuple it is expanded as several positional arguments to the next function.

Changed in version 1.5.5: At least a callable must be passed, otherwise a TypeError is raised. If a single callable is passed it is returned without change.

Parameters:math – Indicates if compose should behave like mathematical function composition: last function in funcs is applied last. If False, then the last function in func is applied first.

Deprecated since version 1.8.0: Use xoutil.fp.tools.compose.

xoutil.future.functools.power(*funcs, times)[source]

Returns the “power” composition of several functions.

Examples:

>>> import operator
>>> f = power(partial(operator.mul, 3), 3)
>>> f(23) == 3*(3*(3*23))
True

>>> power(operator.neg)
Traceback (most recent call last):
...
TypeError: power() takes at least 2 arguments (1 given)
class xoutil.future.functools.lwraps(f, n, *, name=None, doc=None, wrapped=None)[source]

Lambda wrapper.

Useful for decorate lambda functions with name and documentation.

As positional arguments could be passed the function to be decorated and the name in any order. So the next two identity definitions are equivalents:

>>> from xoutil.future.functools import lwraps as lw

>>> identity = lw('identity', lambda arg: arg)

>>> identity = lw(lambda arg: arg, 'identity')

As keyword arguments could be passed some special values, and any number of literal values to be assigned:

  • name: The name of the function (__name__); only valid if not given as positional argument.
  • doc: The documentation (__doc__ field).
  • wrapped: An object to extract all values not yet assigned. These values are (‘__module__’, ‘__name__’ and ‘__doc__’) to be assigned, and ‘__dict__’ to be updated.

If the function to decorate is present in the positional arguments, this same argument function is directly returned after decorated; if not a decorator is returned similar to standard wraps().

For example:

>>> from xoutil.future.functools import lwraps as lw

>>> is_valid_age = lw('is-valid-human-age', lambda age: 0 < age <= 120,
...                   doc=('A predicate to evaluate if an age is '
...                        'valid for a human being.')

>>> @lw(wrapped=is_valid_age)
... def is_valid_working_age(age):
...     return 18 < age <= 70

>>> is_valid_age(16)
True

>>> is_valid_age(200)
False

>>> is_valid_working_age(16)
False

New in version 1.7.0.

xoutil.future.functools.curry(f)[source]

Return a function that automatically ‘curries’ is positional arguments.

Example:

>>> add = curry(lambda x, y: x + y)
>>> add(1)(2)
3

>>> add(1, 2)
3

>>> add()()()(1, 2)
3

We have backported several Python 3.3 features but maybe not all.

xoutil.future.functools.update_wrapper(wrapper, wrapped, assigned=WRAPPER_ASSIGNMENTS, updated=WRAPPER_UPDATES)[source]

Update a wrapper function to look like the wrapped function. The optional arguments are tuples to specify which attributes of the original function are assigned directly to the matching attributes on the wrapper function and which attributes of the wrapper function are updated with the corresponding attributes from the original function. The default values for these arguments are the module level constants WRAPPER_ASSIGNMENTS (which assigns to the wrapper function’s __name__, __module__, __annotations__ and __doc__, the documentation string) and WRAPPER_UPDATES (which updates the wrapper function’s __dict__, i.e. the instance dictionary).

To allow access to the original function for introspection and other purposes (e.g. bypassing a caching decorator such as lru_cache()), this function automatically adds a __wrapped__ attribute to the wrapper that refers to the original function.

The main intended use for this function is in decorator functions which wrap the decorated function and return the wrapper. If the wrapper function is not updated, the metadata of the returned function will reflect the wrapper definition rather than the original function definition, which is typically less than helpful.

update_wrapper() may be used with callables other than functions. Any attributes named in assigned or updated that are missing from the object being wrapped are ignored (i.e. this function will not attempt to set them on the wrapper function). AttributeError is still raised if the wrapper function itself is missing any attributes named in updated.