EndfDict

class endf_parserpy.EndfDict(mapping=None)[source]

Class for enhanced access to nested dictionaries.

This class facilitates the interaction with nested dictionaries. More precisely, the retrieval of objects from the nested structured and the insertion and modification of objects can be performed by making use of references as enabled by the EndfPath class. Therefore access to elements is possible with syntax, such as d['a/b/1/c'] and d['a/b[1]/c']. Apart from the enhanced capabilities to refer to objects in a nested dictionary, this class behaves almost in the same way as a normal Python dict. The only exception is that whenever an object is retrieved that is dict_like, it will be converted on-the-fly to an EndfDict instance and returned in that form to the user. Similary, EndfDict instances to be associated with a key are converted to the underlying dict_like object before being stored (see unwrap() method).

The constructor takes a single argument.

Parameters:

mapping ([None, Dict]) – The dict_like object for which enhanced access is desired. If this argument is None, an empty dictionary will be created.

Example

>>> testdict = {'a': 1, 'b': {1: 'u', 2: 'v'}}
>>> viewdict = EndfDict(testdict)
>>> viewdict['b/1'] = 'w'
>>> assert testdict['b'][1] == 'w'
>>> assert viewdict['b/1'] == testdict['b'][1]
>>> viewdict[2, 3] = 7
>>> assert viewdict['2/3'] == 7
exists(path)[source]

Check whether object exists under path.

Parameters:

path (EndfPath) – An EndfPath or object that is accepted by its constructor.

property path

Get the EndfPath associated with this EndfDict.

If this EndfDict was retrieved from another EndfDict, the path attribute contains the associated location in the nested dictionary.

Example

>>> testdict = {'a': {'b': 'c'}}
>>> viewdict = EndfDict(testdict)
>>> viewdict2 = viewdict['a/b']
>>> assert viewdict2.path == EndfPath('a/b')
property root

Get the EndfDict instance associated with the root dictionary.

Any dict_like object retrieved from an EndfDict (termed the root EndfDict) is automatically enwrapped in an EndfDict instances before being returned to the user. The root attribute of the returned EndDict instances holds a reference to the root EndfDict object.

Example

>>> testdict = {'a': {'b': {'c': 'd'}}}
>>> viewdict = EndfDict(testdict)
>>> assert id(viewdict) == id(viewdict.root)
>>> viewdict2 = testdict['a/b']
>>> assert id(viewdict2.root) == id(viewdict)
unwrap(recursive=False)[source]

Returns the underlying dictionary.

The EndfDict class can be regarded as an interface wrapping around an underlying dict_like object. This function permits the retrieval of the underlying object.

Parameters:

recursive (bool) – If true, all EndfDict objects present in the underlying dict_like object are recursively converted to type dict.

Returns:

The underlying dict_like object.

Return type:

Dict

Note

The use of recursive=true is only necessary if the user has stored an EndfDict object anywhere in the underlying dict_like object. In contrast, if an attempt is made to store an EndfDict object in another EndfDict object, the former object is (non-recursively) unwrapped (with unwrap()) before being stored. In other words, as long as the underlying dict_like object is only accessed via an EndfDict instance, there should never be an EndfDict object in the underlying dict_like object.

Example

>>> testdict = {'a': 1, 'b': 2}
>>> viewdict = EndfDict(testdict)
>>> assert id(testdict) != id(viewdict)
>>> retdict = viewdict.unwrap()
>>> assert id(testdict) == id(retdict)