giving.utils
- giving.utils.keyword_decorator(deco)
Wrap a decorator to optionally takes keyword arguments.
- giving.utils.lax_function(fn)
Add a
**kwargs
argument to fn if it does not have one.fn
is not modified. If it has a varkw argument,fn
is returned directly, otherwise a new function is made that takes varkw and does nothing with them (it just drops them)Example
@lax_function def f(x): return x * x f(x=4, y=123, z="blah") # works, returns 16. y and z are ignored.
- giving.utils.reducer(func=None, default_seed=NotSet, postprocess=NotSet)
Create a reduction operator.
Note
If
func
is not given, this returns a decorator.If func is a function, it should have the signature
func(accum, x)
whereaccum
is the last result andx
is the new data.If func is a class, it should have the following methods:
reduce(accum, x)
, used whenscan
is boolean (scan=False
orscan=True
)roll(accum, x, drop, last_size, current_size)
(seegiving.operators.roll()
), used whenscan
is an integer (scan=n
). It should implement an optimized way to reduce over the last n elements of a sequence. Note thatlast_size
andcurrent_size
cannot exceedn
.Any arguments to
__init__
will be passed over when the operator is called.
Example:
@reducer def sum(last, new): return last + new
@reducer class sum: def reduce(self, last, new): return last + new def roll(self, last, new, drop, last_size, current_size): result = last + new if last_size == current_size: result -= drop return result
- Parameters
func – A function or class that defines the reduction.
default_seed – The default seed to start the reduction.
postprocess – A postprocessing step to apply to the result of the operator.
- Returns
A reduction operator that takes a
scan
argument to perform a scan or roll operation instead of reduce.
- giving.utils.reduced_traceback(skip=['giving.*', 'rx.*'], depth=2)
Create a traceback from the current frame, minus skipped modules.
- Parameters
skip – List of modules to skip.
depth – Depth at which to start.