EndfPath
- class endf_parserpy.EndfPath(pathspec='', array_type='dict', leading='dict')[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
strorintand that keys of typestrdo 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 elementbin the nested dictionary{'a': {1: {'b': 5}can be referenced by the stringa/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 forma/1/basa[1]/b.Once this class is instantiated with a specific reference, the methods
get()andset()can be used to retrieve and define, respectively, the object at the associated location of a given nested dictionary. The functionexists()allows to test whether an object is present at the referenced location and finally the functionremove()allows to remove a key rom the nested dictionary.This class is derived from
collections.abc.Sequence(liketuple) with each element in the Sequence being a key associated with a specific level in the hierarchy of a nested dictionary. As for thetupledatatype, 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 asEndfPathinstances.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
intorstr. It can be a composite key given in a string with individual keys separated by a slash, e.g.a/b/1or equivalentlya/b[1]. This argument can also be a tuple containing the indvidual keys, e.g.('a', 'b', 1).array_type (str) – Must be either
"dict"or"list"and determines if arrays should be assumed to be represented asdict(default) orlist.leading (str) – Must be either
"dict"or"list". Arrays with leading consecutive integer path will be represented according to this argument choice, e.g. for(1, 2, a, 3)andleading="dict", the first two levels will be of typedict.
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') >>> p6 = EndfPath('a[1]']) + EndfPath('2/c') >>> assert p1 == p2 and p2 == p3 and p3 == p4 >>> assert p4 == p5 and p5 == p6
- exists(dict_like)[source]
Test whether a key exists at the
EndfPathlocation.- Parameters:
dict_like (dict) – The (nested) dictionary for which the existence of a key at the
EndfPathlocation should be checked.- Returns:
Trueif key exists atEndfPathlocation, otherwiseFalse.- Return type:
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
EndfPathinstance.- Return type:
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
EndfPathlocation.- Parameters:
dict_like (dict) – The (nested) dictionary from which the key at the
EndfPathlocation 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
EndfPathlocation.- Parameters:
Note
Intermediate dictionaries will be created if missing. If an intermediate key exists that does not refer to a
dict(or other datatype derived fromcollections.abc.MutableMapping), this method will fail.Example
>>> testdict = {} >>> p = EndfPath('a/b/c') >>> p.set(testdict, 12) >>> print(testdict)