utils.functional

Functional-style utilties.

Module Contents

Classes

DummyContext()
mlazy() Memoized lazy evaluation.
_regen(self,it)

Functions

noop(*args,**kwargs) No operation.
pass1(arg,*args,**kwargs) Return the first positional argument.
evaluate_promises(it)
first(predicate,it) Return the first element in it that predicate accepts.
firstmethod(method,on_call=None) Multiple dispatch.
chunks(it,n) Split an iterator into chunks with n elements each.
padlist(container,size,default=None) Pad list with default elements.
mattrgetter(*attrs) Get attributes, ignoring attribute errors.
uniq(it) Return all unique elements in it, preserving order.
regen(it) Convert iterator to an object that can be consumed multiple times.
_argsfromspec(spec,replace_defaults=True)
head_from_fun(fun,bound=False,debug=False) Generate signature function from actual function.
arity_greater(fun,n)
fun_takes_argument(name,fun,position=None)
fun_accepts_kwargs(fun) Return true if function accepts arbitrary keyword arguments.
fun_accepts_kwargs(fun) Return true if function accepts arbitrary keyword arguments.
maybe(typ,val) Call typ on value if val is defined.
seq_concat_item(seq,item) Return copy of sequence seq with item added.
seq_concat_seq(a,b) Concatenate two sequences: a + b.
class DummyContext
__enter__()
__exit__(*exc_info)
class mlazy

Memoized lazy evaluation.

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

evaluate()
noop(*args, **kwargs)

No operation.

Takes any arguments/keyword arguments and does nothing.

pass1(arg, *args, **kwargs)

Return the first positional argument.

evaluate_promises(it)
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.

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

chunks(it, n)

Split an iterator into chunks with n elements each.

Warning:

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.

Example:

# 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]]

padlist(container, size, default=None)

Pad list with default elements.

Example:
>>> 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')
mattrgetter(*attrs)

Get attributes, ignoring attribute errors.

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

uniq(it)

Return all unique elements in it, preserving order.

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.

class _regen(it)
__init__(it)
__reduce__()
__length_hint__()
__iter__()
__getitem__(index)
data()
_argsfromspec(spec, replace_defaults=True)
head_from_fun(fun, bound=False, debug=False)

Generate signature function from actual function.

arity_greater(fun, n)
fun_takes_argument(name, fun, position=None)
fun_accepts_kwargs(fun)

Return true if function accepts arbitrary keyword arguments.

fun_accepts_kwargs(fun)

Return true if function accepts arbitrary keyword arguments.

maybe(typ, val)

Call typ on value if val is defined.

seq_concat_item(seq, item)

Return copy of sequence seq with item added.

Returns:
Sequence: if seq is a tuple, the result will be a tuple,
otherwise it depends on the implementation of __add__.
seq_concat_seq(a, b)

Concatenate two sequences: a + b.

Returns:
Sequence: The return value will depend on the largest sequence
  • if b is larger and is a tuple, the return value will be a tuple.
  • if a is larger and is a list, the return value will be a list,