pyiron_base.interfaces.has_dict.HasDict

pyiron_base.interfaces.has_dict.HasDict#

class pyiron_base.interfaces.has_dict.HasDict[source]#

Bases: ABC

Abstract interface to convert objects to dictionaries for storage.

Subclasses must to implement _from_dict() and _to_dict() and may implement instantiate(). _to_dict() is excepted to return a dict mapping string names to the values the object needs serialized.

On recreating an object from scratch with create_from_dict() first instantiate() is called and then from_dict() with the same obj_dict, i.e. it is roughly equivalent to

>>> my_dict = dict(...)
>>> my_object = MyType.instantiate(my_dict)
>>> my_object.from_dict(my_dict)

Implementations should make sure that calling to_dict after from_dict returns an equivalent dictionary even when the object was not obtained from instantiate(), such that

>>> my_dict = dict(...)
>>> my_object = MyType(...)
>>> my_object.from_dict(my_dict)
>>> my_object.to_dict() == my_dict
True
__init__()#

Methods

__init__()

from_dict(obj_dict[, version])

Populate the object from the serialized object.

instantiate(obj_dict[, version])

Create a blank instance of this class.

to_dict()

Reduce the object to a dictionary.

from_dict(obj_dict: dict, version: str = None)[source]#

Populate the object from the serialized object.

Parameters:
  • obj_dict (dict) – data previously returned from to_dict()

  • version (str) – version tag written together with the data

classmethod instantiate(obj_dict: dict, version: str = None) Self[source]#

Create a blank instance of this class.

This can be used when some values are already necessary for the objects __init__.

Parameters:
  • obj_dict (dict) – data previously returned from to_dict()

  • version (str) – version tag written together with the data

Returns:

a blank instance of the object that is sufficiently initialized to call _from_dict() on it

Return type:

object

to_dict() dict[source]#

Reduce the object to a dictionary.

Returns:

serialized state of this object

Return type:

dict