src.util.basic module¶
Classes and functions that define and operate on basic data structures.
-
class
src.util.basic._AbstractAttributePlaceholder[source]¶ Bases:
objectPlaceholder 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.ABCMetaWrap 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:
typePrivate metaclass that creates a
Singletonbase 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.SingletonMetaParent 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 theSingletoncorrectly, instead of getting the state set during previous tests.
-
classmethod
-
class
src.util.basic.MultiMap(*args, **kwargs)[source]¶ Bases:
collections.defaultdictExtension of the
dictclass 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.
-
class
src.util.basic.WormDict(**kwargs)[source]¶ Bases:
collections.UserDict,dictDict which raises eexceptions when trying to overwrite or delete an existing entry. “WORM” is an acronym for “write once, read many.”
-
_abc_impl= <_abc_data object>¶
-
-
class
src.util.basic.ConsistentDict(**kwargs)[source]¶ Bases:
src.util.basic.WormDictLike 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.WormDictsrc.util.basic.WormDictwithcollections.defaultdictfunctionality.-
_abc_impl= <_abc_data object>¶
-
-
class
src.util.basic.NameSpace[source]¶ Bases:
dictA dictionary that provides attribute-style access.
For example, d[‘key’] = value becomes d.key = value. All methods of
dictare supported.- Note: recursive access (d.key.subkey, as in C-style languages) is not
supported.
Implementation is based on https://github.com/Infinidat/munch.
-
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.
-
_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.MDTFEnum(value)[source]¶ Bases:
src.util.basic._MDTFEnumMixin,enum.EnumCustomize
Enum. 1) Assign (integer) values automatically to the members of the enumeration. 2) Provide afrom_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.IntEnumCustomize
IntEnumanalogous toMDTFEnum.-
_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:
objectClass 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.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_dinto listlist_after their corresponding entries (keys ofslice_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 ofsplice_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.