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- and list-like objects as well as primitive data types (str, int, and float). 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 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 data structure, this class behaves almost in the same way as a normal Python dict or list (depending on constructor argument obj). The exception is that whenever an object is retrieved that is dict- or list-like, it will be converted on-the-fly to an EndfDict or EndfList, respectively, and returned in that form to the user. Also, EndfDict and EndfList instances to be associated with a key are converted to the underlying base object before being stored (see unwrap() method).

The constructor takes two arguments.

Parameters:
  • obj (Union[None, dict, list]) – The dict- or list-like object for which enhanced access is desired.

  • array_type (str) – Either "dict" or "list". With the former choice, if an array needs to be created, it will be represented as dict. With the latter choice, it will be represented by a list.

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 EndfDict 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 base object.

The EndfObject class can be regarded as an interface wrapping around an underlying dict- or list-like object. This function permits the retrieval of the underlying object.

Parameters:

recursive (bool) – If True, all EndfObject objects present in the underlying object are recursively converted to their base type, i.e. either dict or list.

Returns:

The underlying base object.

Return type:

Union[dict, list]

Note

The use of recursive=True is only necessary if the user has stored an EndfObject object anywhere in the underlying base object. In contrast, if an attempt is made to store an EndfObject object in another EndfObject object, the former object is (non-recursively) unwrapped (with unwrap()) before being stored. Therefore, as long as the underlying base object is only accessed via an EndfDict instance, there should never be an EndfObject object 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)