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

integral_xn_exp_minus_x(n: int, a: float, b: float, alpha: float)[source]

Integral of x^n exp(-alpha*|x|) over [a, b] where alpha>0

parameter

Handling of parameters with constraint, for example positivity, bounded, etc.

This is done by specifying the setter/getter properties of the parameter.

argument_with_condition(argument_name, condition, message)[source]
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')
greater_than(value)[source]
strictly_greater_than(value)[source]
less_than(value)[source]
strictly_less_than(value)[source]
between(left_bound, right_bound)[source]
strictly_between(left_bound, right_bound)[source]
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.

get_path_filename(file_path: str) tuple[str, str, str][source]
Parameters

file_path – this should be equal to __file__ in most case

Returns

path, file name and extension of the given file path

create_folder(folder_name: str) None[source]

Create new folder :param folder_name: name of the folder to be created

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.

timer(func)[source]

Generic timer decorator