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
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
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 asEndPathinstances.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).
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
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:
dict_like (dict) – The (nested) dictionary to be extended/altered.
value (Object) – The object that should be inserted at the location referenced by this
EndfPathinstance.
Note
Intermediate dictionaries will be created if missing. If an intermediate key exists that does not refer to a
dict(or other datatype derived fromMutableMapping), this method will fail.Example
>>> testdict = {} >>> p = EndfPath('a/b/c') >>> p.set(testdict, 12) >>> print(testdict)