Generic_Util package¶
Subpackages¶
Submodules¶
Generic_Util.benchmarking module¶
Functions covering typical code-timing scenarios, such as a “with” statement context, an n-executions timer, and a convenient function for comparing and summarising n execution times of different implementations of the same function.
- Generic_Util.benchmarking.compare_implementations(fs_with_shared_args: dict[str, Callable], n=200, wait=1, verbose=True, fs_with_own_args: dict[str, tuple[Callable, list, dict]] | None = None, args: list | None = None, kwargs: dict | None = None)¶
Benchmark multiple implementations of the same function called n times (each with the same args and kwargs), with a break between functions. Recommended later output view if verbose is False:
print(table.to_markdown(index = False)). :param fs_with_own_args: alternative to fs_with_shared_args, args and kwargs arguments: meant for additional functions taking different *args and **kwargs.
- Generic_Util.benchmarking.merge_bench_tables(*tables, verbose=True)¶
- Generic_Util.benchmarking.time_context(name: str = None)¶
“with” statement context for timing the execution of the enclosed code block, i.e.
with time_context('Name of code block'): ...
- Generic_Util.benchmarking.time_n(f: Callable, n=2, *args, **kwargs)¶
Run f (with given arguments) n times and return the execution intervals
Generic_Util.iter module¶
Generic_Util.misc module¶
Functions with less generic purpose than in the above; currently mostly to do with min/max-based operations.
- Generic_Util.misc.bin_array(n: int) list[int]¶
Returns the binary representation of an integer as a list of binary values WITHOUT converting to string
- Generic_Util.misc.interval_overlap(ab: tuple[float, float], cd: tuple[float, float]) float¶
Compute the overlap of two intervals (expressed as tuples of start and end values)
- Generic_Util.misc.min_max(xs: Sequence[_a]) tuple[_a, _a]¶
Mathematically most efficient joint identification of min and max (minimum comparisons = 3n/2 - 2). .. note:
- This function is numba-compilable, e.g. as ``njit(nTup(f8,f8)(f8[::1]))(min_max)`` (see ``Generic_Util.numba.types`` for ``nTup`` shorthand), - If using numpy arrays, min and max are cached for O(1) lookup, and one would imagine this is the used algorithm
Generic_Util.operator module¶
Functions regarding item retrieval, and syntactic sugar for patterns of function application.
- Generic_Util.operator.fst(ab: tuple[_a, _b]) _a¶
Same as operator.itemgetter(0), but intended (and type-annotated) specifically for 2-tuple use
- Generic_Util.operator.get_nested(xss_: Iterable[Iterable], *key_path) Generator¶
Follow a path of keys for a nested combination of iterables
- Generic_Util.operator.on(f: Callable, xs: Iterable[_a], g: Callable[[_a, ...], _b], *args, **kwargs)¶
Transform xs by element-wise application of g and call f with them as its arguments. E.g.
on(operator.gt, (a, b), len).. rubric:: Notes
- Generic_Util.operator.on_a(f: Callable, xs: Iterable, a: str)¶
Extract attribute a from xs elements and call f with them as its arguments. E.g.
on_a(operator.eq, [a, b], '__class__')
- Generic_Util.operator.on_m(f: Callable, xs: Iterable, m: str, *args, **kwargs)¶
Call method m on xs elements and call f with their results as its arguments. E.g.
on_m(operator.gt, [a, b], 'count', 'hello').. rubric:: Notes
- Generic_Util.operator.snd(ab: tuple[_a, _b]) _b¶
Same as operator.itemgetter(1), but intended (and type-annotated) specifically for 2-tuple use
Module contents¶
Copyright (c) 2023 Thomas Fletcher. All rights reserved.
Generic-Util: Convenient functions not found in Python’s Standard Library (regarding benchmarking, iterables, functional tools, operators and numba compilation)