schlichtanders.myfunctools module

This module is one of my best modules. Enjoy.

class schlichtanders.myfunctools.Average(repeat_n_times=1)[source]

Bases: object

computes result several times and returns averages of all

class schlichtanders.myfunctools.AverageExp(repeat_n_times=1, numerical_stable=True)[source]

Bases: object

like average, only that the average is computed on exponential scale

log(Average(exp(x)))

class schlichtanders.myfunctools.Compose(*funcs, **kwargs)[source]

Bases: object

this is class for the compose method

It overwrites + operator and (experimental) also . operator for function concatination. As this of course works only with objects of type Compose, one way to use the concatination syntax is to start with the trivial function lambda x:x which is also available as a Compose, named “I”.

Hence
composed_func = I + func1 + func2 composed_func = I . func1 . func2 composed_func = lambda *args, **kwargs: func1(func2(*args, **kwargs))

are essentially the same, only that they support better kwargs passing throughout the chain of functions.

The . syntax might not be recommandable as this might confuse others. It is not meant as an operator (the operator functionality uses python’s frame hack). At least use it with spaces inbetween, so that it looks more like an operator.

expand_tuple = None

python 2.7 workaround for keywords after *args

class schlichtanders.myfunctools.SimulateOnline(cycle=True, hash_by=<function <lambda>>)[source]

Bases: object

regards args as iterators where f is executed on each separately, consecutively

static hash_by_hash(all_args)[source]
static hash_by_id(all_args)[source]
schlichtanders.myfunctools.as_wrapper(*fmaps, **kwargs)[source]

transforms fmap/fmaps into a wrapper function which can be applied to a function

again a version of lift

Parameters:reverse (bool) – if True (default), function composition order is used (like compose_fmap), else order like used in lift
schlichtanders.myfunctools.compose(*funcs, **kwargs)[source]

Higher level function to compose several functions

The composed function supports passing of kwarks arguments, where each function gets only those args, which an inspect on the function signature revealed.

Parameters:
  • funcs (function) – functions to be concatinated. By default (func1, func2, func3) -> func1(func2(func3(...))).
  • firstlatest (bool (default True)) –
    Value Effect
    True (func1, func2, func3) -> func1(func2(func3(...)))
    False (func1, func2, func3) -> func3(func2(func1(...)))
  • expand_tuple (bool (default True)) – If True expand a return value of type tuple, so that next function is called like f(*tuple)
Returns:

Return type:

concatinated functions

schlichtanders.myfunctools.compose_fmap(*fmaps)[source]

internally like lift, only that it returns a fmap CAUTION: order is exactly reversed compared to lift (because of compose analogy)

schlichtanders.myfunctools.convert(obj, type)[source]

converts object obj to the given type

(may converted to a subtype, e.g. for abstract types) if obj is already of type type, then it is directly returned

Parameters:
  • obj (arbitrary) – to be converted
  • type (class) – convert to
Returns:

Return type:

converted object

schlichtanders.myfunctools.convert_to_list(obj)[source]
schlichtanders.myfunctools.convertible = {(<type 'object'>, <type 'list'>): <function convert_to_list>}

They are mainly useful for working with numeric return types and hence not that general as the functions here. Still they fit well enough.

schlichtanders.myfunctools.decorator_from_fmap(*fmaps, **kwargs)
schlichtanders.myfunctools.fmap(func, *contexts, **kwargs_contexts)[source]

generic map interface to lift a function to be able to work with different containers / contexts

Instead of a normal call func(*args, **kwargs) use fmap(func, *args, **kwargs) to work natively on more abstract containers.

Support for lists, generators, tuples (everything map supports), functions, and generally classes which implement “__map__” are listed in fmappable.

Parameters:
  • func (function) – to be mapped
  • contexts (list of same type) – to be mapped upon
  • kwargs_contexts (kwargs) – to be mapped upon
  • _inplace (bool, defaults to False) – kwarg which will be popped from func_kwargs, indicating whether the function shall be mapped in place (if possible) Note, that for this func must return the same number of outputs as contexts
Returns:

Return type:

mapped result

schlichtanders.myfunctools.fmap_dict(func, *contexts, **kwargs_contexts)[source]

fmap implementation to work with dicts (more general Mapping)

inplace only affects *contexts

schlichtanders.myfunctools.fmap_function(func, *contexts, **kwargs_contexts)[source]

fmap implementation to work with functions

schlichtanders.myfunctools.fmap_iterable(func, *contexts, **kwargs_contexts)[source]

inplace works only for Mutable types and will effect only contexts

schlichtanders.myfunctools.fmap_list(func, *contexts, **kwargs_contexts)[source]

fmap implementation to work with lists (more general Sequence)

schlichtanders.myfunctools.fmap_singleton(f, *singleton_args)[source]
schlichtanders.myfunctools.identity(x)[source]
schlichtanders.myfunctools.lift(f, *fmaps)[source]

will transform func to a new function with the fmaps applied like function composition e.g. >>> f_lifted = lift(f, summap, Average(10)) will kind of first execute f, then summap on f, and then Average over summap finally. From inner towards outer.

If no fmaps are given, the general fmap is used

lift is kind of function composition for fmaps, only without fancy kwargs support

schlichtanders.myfunctools.meanexp(values)[source]
schlichtanders.myfunctools.meanexpmap(f, *batch_args)[source]

numerical stable version of log(1/n*sum(exp(...))

schlichtanders.myfunctools.meanmap(f, *batch_args)[source]

assumes args and kwargs refer to lists

executes f on each list entry and returns summed up values

schlichtanders.myfunctools.sumexp(values)[source]
schlichtanders.myfunctools.sumexpmap(f, *batch_args)[source]

numerical stable version of log(sum(exp(...))

schlichtanders.myfunctools.summap(f, *batch_args)[source]

assumes args and kwargs refer to lists

executes f on each list entry and returns summed up values

schlichtanders.myfunctools.use_as_needed(func, kwargs, args=())[source]

calls the given function with the subset of kwargs which is supported by the function

optionally you can also pass args

Parameters:
  • func (function) – to be called
  • kwargs (dict) –
  • args (tuple) –
Returns:

Return type:

returns the output of the function