EndfPath

class endf_parserpy.EndfPath(pathspec='')[source]

Class to store a reference to a location in a nested dictionary.

An instance of this class maintains a reference that points to a specific location in a nested dictionary. It is assumed that all keys in the dictionaries are either of type str or int and that keys of type str do not contain slashes. Under these assumptions, objects in a nested dictionary can be referenced by concatenating the keys referring to different levels in the nested hierarchy in a single string, with the individual keys separated by a slash. For example, the element b in the nested dictionary {'a': {1: {'b': 5} can be referenced by the string a/1/b. Sometimes, it is a good mental model to think of dictionaries with integer keys as a kind of array, and it should therefore also be allowed to rewrite paths of the form a/1/b as a[1]/b.

Once this class is instantiated with a specific reference, the methods get() and set() can be used to retrieve and define, respectively, the object at the associated location of a given nested dictionary. The function exists() allows to test whether an object is present at the referenced location and finally the function remove() allows to remove a key rom the nested dictionary.

This class is derived from Sequence (like tuple) with each element in the Sequence being a key associated with a specific level in the hierarchy of a nested dictionary. As for the tuple datatype, references can be concatenated using the + operator and two references can be compared using the == operator. Also iterating over the individual keys within a reference works, which are returned as EndPath instances.

The EndfPath constructor accepts the following parameters.

Parameters:

pathspec (Union[int, str, tuple]) – This argument can be provided in several forms. It can be a single key represented by a variable of type int or str. It can be a composite key given in a string with individual keys separated by a slash, e.g. a/b/1 or equivalently a/b[1]. This argument can also be a tuple containing the indvidual keys, e.g. ('a', 'b', 1).

Examples

>>> p1 = EndfPath('a/1/2/c')
>>> p2 = EndfPath('a[1]/2/c')
>>> p3 = EndfPath(('a', '1', '2', 'c'))
>>> p4 = EndfPath(('a', 1, '2', 'c'))
>>> p5 = EndfPath('a[1,2]/c')
>>> p5 = EndfPath('a[1]']) + EndfPath('2/c')
>>> assert p1 == p2 and p2 == p3 and p3 == p4
>>> assert p4 == p5 and p5 == p6
>>> assert p6 == p7
exists(dict_like)[source]

Test whether a key exists at the EndfPath location.

Parameters:

dict_like (dict) – The (nested) dictionary for which the existence of a key at the EndfPath location should be checked.

Returns:

true if key exists at EndfPath location, otherwise false.

Return type:

bool

Example

>>> testdict = {'a': {1: 10}}
>>> p = EndfPath('a/1')
>>> p.exists(testdict)
get(dict_like)[source]

Retrieve object from nested dictionary.

Parameters:

dict_like (dict) – The (nested) dictionary from which an object should be retrieved.

Returns:

The object at the location referred to by this EndfPath instance.

Return type:

object

Example

>>> testdict = {'a': 1: {'b': 5}}
>>> p = EndfPath('a/1/b')
>>> assert p.get(testdict) == 5
remove(dict_like)[source]

Remove key from nested dictionary at EndfPath location.

Parameters:

dict_like (dict) – The (nested) dictionary from which the key at the EndfPath location should be removed.

Example

>>> testdict = {'a': {1: 10}}
>>> p = EndfPath('a/1')
>>> p.remove(testdict)
set(dict_like, value)[source]

Insert an object into a nested dictionary at the EndfPath location.

Parameters:
  • dict_like (dict) – The (nested) dictionary to be extended/altered.

  • value (Object) – The object that should be inserted at the location referenced by this EndfPath instance.

Note

Intermediate dictionaries will be created if missing. If an intermediate key exists that does not refer to a dict (or other datatype derived from MutableMapping), this method will fail.

Example

>>> testdict = {}
>>> p = EndfPath('a/b/c')
>>> p.set(testdict, 12)
>>> print(testdict)