EndfDict

class endf_parserpy.EndfDict(obj=None, array_type='dict')[source]

EndfDict class extends dict with EndfPath functionality.

The EndfDict class behaves like a list but offers in addition enhanced indexing capabilities. This class is derived from the EndfObject class and inherits its methods.

Constructor for the EndfDict class.

Parameters:
  • obj (Optional[dict]) – If obj is provided, the instantiated EndfDict class provides a view object. Otherwise, the created instance will be initialized with an empty dictionary.

  • array_type (str) – Can be "dict" or "list" and indicates the datatype to use for representing arrays.

exists(path)

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)

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)