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
EndfPathclass. Therefore access to elements is possible with syntax, such asd['a/b/1/c']andd['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 Pythondict. The only exception is that whenever an object is retrieved that isdict_like, it will be converted on-the-fly to anEndfDictinstance and returned in that form to the user. Similary,EndfDictinstances to be associated with a key are converted to the underlyingdict_likeobject before being stored (seeunwrap()method).The constructor takes a single argument.
- Parameters:
mapping ([None, Dict]) – The
dict_likeobject for which enhanced access is desired. If this argument isNone, 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
- property path
Get the
EndfPathassociated with thisEndfDict.If this
EndfDictwas retrieved from anotherEndfDict, thepathattribute 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
EndfDictinstance associated with the root dictionary.Any
dict_likeobject retrieved from anEndfDict(termed the rootEndfDict) is automatically enwrapped in anEndfDictinstances before being returned to the user. Therootattribute of the returnedEndDictinstances holds a reference to the rootEndfDictobject.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_likeobject. This function permits the retrieval of the underlying object.- Parameters:
recursive (bool) – If
true, allEndfDictobjects present in the underlyingdict_likeobject are recursively converted to typedict.- Returns:
The underlying
dict_likeobject.- Return type:
Dict
Note
The use of
recursive=trueis only necessary if the user has stored anEndfDictobject anywhere in the underlyingdict_likeobject. In contrast, if an attempt is made to store anEndfDictobject in anotherEndfDictobject, the former object is (non-recursively) unwrapped (withunwrap()) before being stored. In other words, as long as the underlyingdict_likeobject is only accessed via anEndfDictinstance, there should never be anEndfDictobject in the underlyingdict_likeobject.Example
>>> testdict = {'a': 1, 'b': 2} >>> viewdict = EndfDict(testdict) >>> assert id(testdict) != id(viewdict) >>> retdict = viewdict.unwrap() >>> assert id(testdict) == id(retdict)