chemreac.util.pyutil¶
Utility functions used throughout chemreac.
-
chemreac.util.pyutil.
monotonic
(arr, positive=0, strict=False)[source]¶ Check monotonicity of a serie
- Parameters
- arr: array_like
Array to be checked for monotonicity
- positive: -1, 0 or 1 (default: 0)
-1: negative, 1: positive, 0: either
- strict: bool (default: False)
Disallow zero difference between neighboring instances
- Returns
- bool
Examples
>>> monotonic([0, 0, -1, -2]) True >>> monotonic([0, 0, 1, 2], strict=True) False >>> monotonic([1, 2, 3], -1) False
-
class
chemreac.util.pyutil.
progress
(iterable, output=None, proc_time=True)[source]¶ Print a progress bar of dots
- Parameters
- iterable: iterable
must have
__len__
- output: fileobject
default: sys.stdout
- proc_time: bool
show process time (in seconds) passed at end of iteration.
Examples
>>> vals = list(range(7)) >>> squares = [] >>> for val in progress(vals, proc_time=False): ... squares.append(val**2) ... 7: ....... >>> squares [0, 1, 4, 9, 16, 25, 36]
-
chemreac.util.pyutil.
set_dict_defaults_inplace
(dct, *args)[source]¶ Modifies a dictionary in-place by populating key/value pairs present in the default dictionaries which have no key in original dictionary dct. Useful for passing along keyword argument dictionaries between functions.
- Parameters
- dct: dict
- *args: dictionaries
- Returns
- dct: (possibly modified) input dictionary
Examples
>>> d = {1: None} >>> set_dict_defaults_inplace(d, {2: []}) >>> d == {1: None, 2: []} True >>> f = {'a': 1, 'b': 3} >>> g = {'a': 1} >>> set_dict_defaults_inplace(g, {'b': 2, 'a': 7}, {'b': 3}) >>> f == g True >>> h = {42: True, 'b': 3} >>> i = {} >>> set_dict_defaults_inplace(i, {42: True, 'b': 2}, {'b': 3}) >>> h == i True