EndfObject
- class endf_parserpy.utils.accessories.EndfObject(obj, array_type, leading)[source]
Class for enhanced access to nested data structures.
This class facilitates the interaction with nested data structures composed of
dict- andlist-like objects as well as primitive data types (str,int, andfloat). More precisely, the retrieval of objects from the nested structure and the insertion and modification of objects can be performed by making use of references as enabled by theEndfPathclass. 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 data structure, this class behaves almost in the same way as a normal Pythondictorlist(depending on constructor argumentobj). The exception is that whenever an object is retrieved that isdict- orlist-like, it will be converted on-the-fly to anEndfDictorEndfList, respectively, and returned in that form to the user. Also,EndfDictandEndfListinstances to be associated with a key are converted to the underlying base object before being stored (seeunwrap()method).The constructor takes two arguments.
- Parameters:
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-like object retrieved from anEndfDict(termed the rootEndfDict) is automatically enwrapped in anEndfDictinstances before being returned to the user. Therootattribute of the returnedEndfDictinstances 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 base object.
The
EndfObjectclass can be regarded as an interface wrapping around an underlyingdict- orlist-like object. This function permits the retrieval of the underlying object.- Parameters:
recursive (bool) – If
True, allEndfObjectobjects present in the underlying object are recursively converted to their base type, i.e. eitherdictorlist.- Returns:
The underlying base object.
- Return type:
Note
The use of
recursive=Trueis only necessary if the user has stored anEndfObjectobject anywhere in the underlying base object. In contrast, if an attempt is made to store anEndfObjectobject in anotherEndfObjectobject, the former object is (non-recursively) unwrapped (withunwrap()) before being stored. Therefore, as long as the underlying base object is only accessed via anEndfDictinstance, there should never be anEndfObjectobject in the underlying base object.Example
>>> testdict = {'a': 1, 'b': 2} >>> viewdict = EndfDict(testdict) >>> assert id(testdict) != id(viewdict) >>> retdict = viewdict.unwrap() >>> assert id(testdict) == id(retdict)