src.util.basic module

Classes and functions that define and operate on basic data structures.

class src.util.basic._AbstractAttributePlaceholder[source]

Bases: object

Placeholder class used in the definition of the abstract_attribute() decorator.

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: abc.ABCMeta

Wrap the metaclass for abstract base classes to enable definition of abstract attributes; raises NotImplementedError if they aren’t defined in child classes. Based on https://stackoverflow.com/a/50381071.

class src.util.basic._Singleton[source]

Bases: type

Private metaclass that creates a Singleton base class when called. This version is copied from https://stackoverflow.com/a/6798042 and should be compatible with both Python 2 and 3.

_instances = {}
class src.util.basic.Singleton(*args, **kwargs)[source]

Bases: src.util.basic.SingletonMeta

Parent class defining the Singleton pattern. We use this as safer way to pass around global state.

classmethod _reset()[source]

Private method of all Singleton-derived classes added for use in unit testing only. Calling this method on test teardown deletes the instance, so that tests coming afterward will initialize the Singleton correctly, instead of getting the state set during previous tests.

class src.util.basic.MultiMap(*args, **kwargs)[source]

Bases: collections.defaultdict

Extension of the dict class that allows doing dictionary lookups from either keys or values.

Syntax for lookup from keys is unchanged, bd['key'] = 'val', while lookup from values is done on the inverse attribute and returns a set of matching keys if more than one match is present: bd.inverse['val'] = ['key1', 'key2']. See https://stackoverflow.com/a/21894086.

get_(key)[source]
to_dict()[source]
inverse()[source]
inverse_get_(val)[source]
class src.util.basic.WormDict(**kwargs)[source]

Bases: collections.UserDict, dict

Dict which raises eexceptions when trying to overwrite or delete an existing entry. “WORM” is an acronym for “write once, read many.”

classmethod from_struct(d)[source]
_abc_impl = <_abc_data object>
class src.util.basic.ConsistentDict(**kwargs)[source]

Bases: src.util.basic.WormDict

Like WormDict, but we only raise WormKeyError if we try to reassign to a different value.

_abc_impl = <_abc_data object>
class src.util.basic.WormDefaultDict(default_factory=None, *args, **kwargs)[source]

Bases: src.util.basic.WormDict

src.util.basic.WormDict with collections.defaultdict functionality.

_abc_impl = <_abc_data object>
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.

toDict()[source]

Recursively converts a NameSpace back into a dictionary.

classmethod _toDict(x)[source]

Recursively converts a NameSpace back into a dictionary. nb. As dicts are not hashable, they cannot be nested in sets/frozensets.

classmethod fromDict(x)[source]

Recursively transforms a dictionary into a NameSpace via copy. nb. As dicts are not hashable, they cannot be nested in sets/frozensets.

copy() → a shallow copy of D[source]
_freeze()[source]

Return immutable representation of (current) attributes.

We do this to enable comparison of two Namespaces, which otherwise would be done by the default method of testing if the two objects refer to the same location in memory. See https://stackoverflow.com/a/45170549.

class src.util.basic._MDTFEnumMixin[source]

Bases: object

classmethod from_struct(str_)[source]

Instantiate from string.

class src.util.basic.MDTFEnum(value)[source]

Bases: src.util.basic._MDTFEnumMixin, enum.Enum

Customize Enum. 1) Assign (integer) values automatically to the members of the enumeration. 2) Provide a from_struct() method to simplify instantiating an instance from a string. To avoid potential confusion with reserved keywords, we use the Python convention that members of the enumeration are all uppercase.

_generate_next_value_(start, count, last_values)
_member_names_ = []
_member_map_ = {}
_member_type_

alias of builtins.object

_value2member_map_ = {}
class src.util.basic.MDTFIntEnum(value)[source]

Bases: src.util.basic._MDTFEnumMixin, enum.IntEnum

Customize IntEnum analogous to MDTFEnum.

_generate_next_value_(start, count, last_values)
_member_names_ = []
_member_map_ = {}
_member_type_

alias of builtins.int

_value2member_map_ = {}
src.util.basic.sentinel_object_factory(obj_name)[source]

Return a unique singleton object/class (same difference for singletons). For implentation, see python docs.

class src.util.basic.MDTF_ID(id_=None)[source]

Bases: object

Class wrapping UUID, to provide unique ID numbers for 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]
src.util.basic.to_iter(obj, coll_type=<class 'list'>)[source]
src.util.basic.from_iter(obj)[source]
src.util.basic.remove_prefix(s1, s2)[source]
src.util.basic.remove_suffix(s1, s2)[source]
src.util.basic.filter_kwargs(kwarg_dict, function)[source]

Given a dict of kwargs, return only those kwargs accepted by function.

src.util.basic.splice_into_list(list_, splice_d, key_fn=None, log=<Logger src.util.basic (WARNING)>)[source]

Splice sub-lists in 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 – parent list to splice sub-lists into.

  • splice_d – dict of 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 (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, or raise ValueError.