src.util.basic module¶
Classes and functions that define and operate on basic data structures.
- src.util.basic.abstract_attribute(obj=None)[source]¶
Decorator for abstract attributes in abstract base classes by analogy with
abc.abstract_method()
. Based on https://stackoverflow.com/a/50381071.
- class src.util.basic.MDTFABCMeta(name, bases, namespace, /, **kwargs)[source]¶
Bases:
ABCMeta
Wrap the metaclass for abstract base classes to enable definition of abstract attributes via
abstract_attribute()
. Based on https://stackoverflow.com/a/50381071.- Raises:
NotImplementedError – If a child class doesn’t define an abstract attribute, by analogy with
abc.abstract_method()
.
- mro()¶
Return a type’s method resolution order.
- class src.util.basic.Singleton(*args, **kwargs)[source]¶
Bases:
SingletonMeta
Parent class defining the Singleton pattern. We use this as safer way to pass around global state.
- class src.util.basic.MultiMap(*args, **kwargs)[source]¶
Bases:
defaultdict
Extension of the
dict
class that allows doing dictionary lookups from either keys or values.Syntax for lookup from keys is unchanged, while lookup from values is done on the
inverse()
attribute and returns a list of matching keys if more than one match is present. See https://stackoverflow.com/a/21894086.Example:
>>> d = MultiMap({'key1': 'val', 'key2':'val'}) >>> d['key1'] 'val' >>> d.inverse['val'] ['key1', 'key2']
- __init__(*args, **kwargs)[source]¶
Inherited from
collections.defaultdict
. Construct by passing an ordinarydict
.
- fromkeys(value=None, /)¶
Create a new dictionary with keys from iterable and values set to value.
- class src.util.basic.WormDict(dict=None, /, **kwargs)[source]¶
-
Dict which raises exceptions when trying to overwrite or delete an existing entry. “WORM” is an acronym for “write once, read many.”
- Raises:
WormKeyError – If code attempts to reassign or delete an existing key.
- classmethod from_struct(d)[source]¶
Construct a WormDict from a dict d. Intended to be used for automatic type coercion done on fields of a
mdtf_dataclass()
.
- class src.util.basic.ConsistentDict(dict=None, /, **kwargs)[source]¶
Bases:
WormDict
Like
WormDict
, but we only raiseWormKeyError
if we try to reassign to a different value.- Raises:
WormKeyError – If code attempts to reassign an existing key to a value different than the current one.
- classmethod from_struct(d)¶
Construct a WormDict from a dict d. Intended to be used for automatic type coercion done on fields of a
mdtf_dataclass()
.
- class src.util.basic.WormDefaultDict(default_factory=None, *args, **kwargs)[source]¶
Bases:
WormDict
src.util.basic.WormDict
withcollections.defaultdict
functionality.- __init__(default_factory=None, *args, **kwargs)[source]¶
Inherited from
collections.defaultdict
and takes same arguments.
- classmethod from_struct(d)¶
Construct a WormDict from a dict d. Intended to be used for automatic type coercion done on fields of a
mdtf_dataclass()
.
- class src.util.basic.NameSpace[source]¶
Bases:
dict
A dictionary that provides attribute-style access.
For example, d[‘key’] = value becomes d.key = value. All methods of
dict
are supported.Note
Recursive access (d.key.subkey, as in C-style languages) is not supported.
Implementation is based on https://github.com/Infinidat/munch.
- Raises:
AttributeError – In cases where dict would raise a
KeyError
.
- classmethod fromDict(x)[source]¶
Recursively transforms a dictionary into a NameSpace via copy. (Note: as dicts are not hashable, they cannot be nested in sets/frozensets.)
- fromkeys(value=None, /)¶
Create a new dictionary with keys from iterable and values set to value.
- class src.util.basic.MDTFEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
_MDTFEnumMixin
,Enum
Customize behavior of
Enum
:Assign (integer) values automatically to the members of the enumeration.
Provide a
from_struct()
method to simplify instantiating an instance from a string. Intended to be used for automatic type coercion done on fields of amdtf_dataclass()
. To avoid potential confusion with reserved keywords, we use the Python convention that members of the enumeration are all uppercase.
- class src.util.basic.MDTFIntEnum(value, names=None, *, module=None, qualname=None, type=None, start=1, boundary=None)[source]¶
Bases:
_MDTFEnumMixin
,IntEnum
Customize
IntEnum
analogous toMDTFEnum
.- as_integer_ratio()¶
Return integer ratio.
Return a pair of integers, whose ratio is exactly equal to the original int and with a positive denominator.
>>> (10).as_integer_ratio() (10, 1) >>> (-10).as_integer_ratio() (-10, 1) >>> (0).as_integer_ratio() (0, 1)
- bit_count()¶
Number of ones in the binary representation of the absolute value of self.
Also known as the population count.
>>> bin(13) '0b1101' >>> (13).bit_count() 3
- bit_length()¶
Number of bits necessary to represent self in binary.
>>> bin(37) '0b100101' >>> (37).bit_length() 6
- conjugate()¶
Returns self, the complex conjugate of any int.
- denominator¶
the denominator of a rational number in lowest terms
- from_bytes(byteorder='big', *, signed=False)¶
Return the integer represented by the given array of bytes.
- bytes
Holds the array of bytes to convert. The argument must either support the buffer protocol or be an iterable object producing bytes. Bytes and bytearray are examples of built-in objects that support the buffer protocol.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.
- signed
Indicates whether two’s complement is used to represent the integer.
- imag¶
the imaginary part of a complex number
- numerator¶
the numerator of a rational number in lowest terms
- real¶
the real part of a complex number
- to_bytes(length=1, byteorder='big', *, signed=False)¶
Return an array of bytes representing an integer.
- length
Length of bytes object to use. An OverflowError is raised if the integer is not representable with the given number of bytes. Default is length 1.
- byteorder
The byte order used to represent the integer. If byteorder is ‘big’, the most significant byte is at the beginning of the byte array. If byteorder is ‘little’, the most significant byte is at the end of the byte array. To request the native byte order of the host system, use `sys.byteorder’ as the byte order value. Default is to use ‘big’.
- signed
Determines whether two’s complement is used to represent the integer. If signed is False and a negative integer is given, an OverflowError is raised.
- src.util.basic.sentinel_object_factory(obj_name)[source]¶
Return a unique singleton object/class (same difference for singletons). For implementation, see python docs.
- class src.util.basic.MDTF_ID(id_=None)[source]¶
Bases:
object
Class wrapping
UUID
, to provide unique ID numbers for members of the object hierarchy (cases, pods, variables, etc.), so that we don’t need to require that objects in these classes have unique names.
- src.util.basic.is_iterable(obj)[source]¶
Test if obj is an iterable collection.
- Parameters:
obj – Object to test.
- Returns:
True if obj is an iterable collection and not a string.
- Return type:
- src.util.basic.to_iter(obj, coll_type=<class 'list'>)[source]¶
Cast arbitrary object obj to an iterable collection. If obj is not a collection, returns a one-element list containing obj.
- src.util.basic.from_iter(obj)[source]¶
Inverse of
to_iter()
. If obj is a single-element iterable collection, return its only element.
- src.util.basic.remove_prefix(s1, s2)[source]¶
If string s1 starts with string s2, return s1 with s2 removed. Otherwise return s1 unmodified.
- src.util.basic.remove_suffix(s1, s2)[source]¶
If string s1 ends with string s2, return s1 with s2 removed. Otherwise return s1 unmodified.
- src.util.basic.filter_kwargs(kwarg_dict, function)[source]¶
Given keyword arguments kwarg_dict, return only those kwargs accepted by function.
- src.util.basic.splice_into_list(list_, splice_d, key_fn=None, log=<Logger>)[source]¶
Splice sub-lists (values of splice_d) into list list_ after their corresponding entries (keys of slice_d). Example:
>>> splice_into_list(['a','b','c'], {'b': ['b1', 'b2']}) ['a', 'b', 'b1', 'b2', 'c']
- Parameters:
list_ (list) – Parent list to splice sub-lists into.
splice_d (dict) – Sub-lists to splice in. Keys are entries in list_ and values are the sub-lists to insert after that entry. Duplicate or missing entries are handled appropriately.
key_fn (function) – Optional. If supplied, function applied to elements of list_ to compare to keys of splice_d.
- Returns:
Spliced list_ as described above.
- src.util.basic.deserialize_class(name)[source]¶
Given the name of a currently defined class, return the class itself. This avoids security issues with calling
eval()
. Based on https://stackoverflow.com/a/11781721.- Parameters:
name (str) – name of the class to look up.
- Returns:
class
with the given name, if currently imported.- Raises:
ValueError – If class not found in current namespace.