tools
generic
Generic helpers for Python code
- lazy_indices_product(args: list[int])[source]
- itertools.product can blow up the memory because:
‘Before product() runs, it completely consumes the input iterables, keeping pools of values in memory to generate the products.’ (-> https://docs.python.org/3/library/itertools.html#itertools.product). There is sometimes a need for a ‘lazy’ cartesian product. The following code is inspired from: http://phrogz.net/lazy-cartesian-product and we are also following the advice in (https://hackernoon.com/generating-the-nth-cartesian-product-e48db41bed3f) and we are using gmpy2 (not bigfloat) for floating-point division.
This function generate all the possible tuples of indices (i1, i2,…) for i1 in range(l1), i2 in range(l2),… where l1, l2,… are the args passed to the function
- Returns
‘lazy’ cartesian product
integral
A few useful integrals
parameter
Handling of parameters with constraint, for example positivity, bounded, etc.
This is done by specifying the setter/getter properties of the parameter.
- positive(argument_name, *, condition=<function <lambda>>, message='expected a positive value')
- negative(argument_name, *, condition=<function <lambda>>, message='expected a negative value')
- strictly_positive(argument_name, *, condition=<function <lambda>>, message='expected a strictly positive value')
- strictly_negative(argument_name, *, condition=<function <lambda>>, message='expected a strictly negative value')
- positive_sequence(argument_name, *, condition=<function <lambda>>, message='expected a sequence of positive elements')
- negative_sequence(argument_name, *, condition=<function <lambda>>, message='expected a sequence of negative elements')
- strictly_positive_sequence(argument_name, *, condition=<function <lambda>>, message='expected of strictly sequence elements')
- strictly_negative_sequence(argument_name, *, condition=<function <lambda>>, message='expected of strictly sequence elements')
system
Generic tools around handling of files system.
timer
Timer decorator taken from “Fluent Python” p205
Adding this decorator to a function allows to time it and displays this information in the console.