Utils¶
Functions
|
Equivalent of @property for class |
|
Hash values in a nested structure such as dict or list. |
|
Return the value of the environment variable |
|
Return |
|
Return |
|
Decorator retrying funtcions with exponential backoff. |
Classes
|
- deep_hash(value, prefix=None, fmt=None)¶
Hash values in a nested structure such as dict or list. Most useful to check the integrity of nested values
- Parameters
value (nested-object) – nested structure to hash, supporting numpy arrays and scipy sparse
prefix (bytes?) – optional prefix to make hash unique
fmt (str?) – output format. Can be
'long'
(default) or'int'
or'bytes20'
or'hex40'
- Returns
long
orint
orbytes
orstr
, depending onfmt
Example
>>> val = { >>> 'arrays': { >>> 'int': numpy.arange(1000), >>> 'struct': numpy.array([(11, 12), (21, 22)], [('a', int), ('b', int)]), >>> 'struct-sliced': numpy.array([(11, 12), (21, 22)], [('a', int), ('b', int)])[['a']], >>> 'transposed': numpy.arange(3*5).reshape((3, 5)).T, >>> }, >>> 'sparse': { >>> 'csr': scipy.sparse.random(5, 4, 0.1, 'csr'), >>> 'csc': scipy.sparse.random(5, 4, 0.1, 'csc'), >>> 'coo': scipy.sparse.random(5, 4, 0.1, 'coo'), >>> }, >>> 'scalars': [1, 2.5, 'str', b'bytes', numpy.float32(1.5), numpy.bytes_(b'npbytes')] >>> }
The hash can returned with different formats:
>>> h1 = deep_hash(val, fmt='long') >>> h2 = deep_hash(val, fmt='hex40') >>> h3 = deep_hash(val, fmt='bytes20') >>> h1, h2, h3 (699019679910377672527134164600537195154359546715, '7a7120641ee1b531deb2f14d04c986be5d89735b', b'zq d\x1e\xe1\xb51\xde\xb2\xf1M\x04\xc9\x86\xbe]\x89s[')
A prefix can be added to make the hash unique:
>>> h1 = deep_hash(val) >>> h2 = deep_hash(val, prefix=b'my-prefix') >>> h1 == h2 False
Two inputs that are not identical lead to different hash values:
>>> val['arrays']['int'][50] += 1 >>> h1 == deep_hash(val) False >>> val['arrays']['int'][50] -= 1 >>> h1 == deep_hash(val) True
- class ClassPropertyDescriptor(fget, fset=None)¶
-
Example
>>> class MyTest(object): >>> _val = 42
>>> @classproperty >>> def val(cls): >>> return cls._val
>>> @val.setter >>> def val(cls, value): >>> cls._val = value
- __init__(fget, fset=None)¶
- classproperty(func)¶
Equivalent of @property for class
- getenv(name, default=None, cast=None)¶
Return the value of the environment variable
name
if it exists, otherwise return the default value.- Specifying a value for the cast argument enables to return the value of the environement
variable with the specified format (instead of the default
'str'
format).
- Parameters
name (str) – the name of the environement variable
default (str?) – string denoting the default value in case name does not exists. (
default: None
)cast (function?) – either function to cast the environment variable, or
list
to split comma separated values, orbool
to handle strings:'1'
,'true'
,'yes'
(case insensitive), or'b64'
to decode base64-encoded bytes.
- Returns
environment variable
- is_true(value)¶
Return
True
if the input value is'1'
,'true'
or'yes'
(case insensitive)- Parameters
value (str) – value to be evaluated
- Returns
bool
Example
>>> is_true('1') True
- is_false(value)¶
Return
True
if the input value is'0'
,'false'
or'no'
(case insensitive)- Parameters
value (str) – value to be evaluated
- Returns
bool
Example
>>> is_false('0') True >>> is_false('1') False
- retry(base=1, multiplier=8, max_retry=2, exception=<class 'Exception'>, reraise=True)¶
Decorator retrying funtcions with exponential backoff.
The retry mechanism can be aborted by the client by using myfunction(…, __retry__=False).
The maximum time of execution is \(\sum_{k=1}^{max\_retry}base \times multiplier^k\).
- Parameters
base (int) – base time
multiplier (int) – multiplier
max_retry (int) – maximum number of attempts
exception – exception