pyiron_base.storage.hdfio.DummyHDFio#
- class pyiron_base.storage.hdfio.DummyHDFio(project, h5_path: str, cont: dict | None = None, root: DummyHDFio | None = None)[source]#
-
A dummy ProjectHDFio implementation to serialize objects into a dict instead of a HDF5 file.
It is modeled after ProjectHDFio, but supports just enough methods to successfully write objects.
After all desired objects have been written to it, you may extract a pure dict from with with .to_dict.
A simple example for storing data containers:
>>> from pyiron_base import DataContainer, Project >>> pr = Project(...) >>> hdf = DummyHDFio(pr, '/', {}) >>> d = DataContainer({'a': 42, 'b':{'c':4, 'g':33}}) >>> d.to_hdf(hdf) >>> hdf.to_dict() {'READ_ONLY': False, 'a__index_0': 42, 'b__index_1': { 'READ_ONLY': False, 'c__index_0': 4, 'g__index_1': 33, 'NAME': 'DataContainer', 'TYPE': "<class 'pyiron_base.storage.datacontainer.DataContainer'>", 'OBJECT': 'DataContainer', 'VERSION': '0.1.0', 'HDF_VERSION': '0.2.0' }, 'NAME': 'DataContainer', 'TYPE': "<class 'pyiron_base.storage.datacontainer.DataContainer'>", 'OBJECT': 'DataContainer', 'VERSION': '0.1.0', 'HDF_VERSION': '0.2.0'}
- __init__(project, h5_path: str, cont: dict | None = None, root: DummyHDFio | None = None)[source]#
- Parameters:
project (Project) – the project this object should advertise itself belong to; in practice it is not often used for writing objects
h5_path (str) – the path of the HDF group this object fakes
cont (dict, optional) – dict to save written values into, make a new one if not given
root (DummyHDFio, optional) – if this object will be a child of another one, the parent must be passed here, to make hdf[‘..’] work.
Methods
__init__
(project, h5_path[, cont, root])close
()Surface from a sub group.
create_group
(name)Create a new sub group.
get
(key[, default])Internal wrapper function for __getitem__() - self[name]
list_all
()Returns dictionary of :method:`.list_groups()` and :method:`.list_nodes()`.
Return a list of names of all nested groups.
Return a list of names of all nested nodes.
open
(name)Descend into a sub group.
read_dict_from_hdf
([group_paths, recursive])Read data from the HDF5 file and return it as a dictionary.
to_dict
()Convert the HDF5 data to a dictionary.
to_object
([class_name])Load the full pyiron object from an HDF5 file
write_dict_to_hdf
(data_dict)Write a dictionary to the HDF5 file.
Attributes
h5_path
project
- close() DummyHDFio [source]#
Surface from a sub group.
If this object was not returned from a previous call to
open()
it returns itself silently.
- create_group(name: str) DummyHDFio [source]#
Create a new sub group.
- Parameters:
name (str) – name of the new group
- get(key: str | slice, default: object | None = None) dict | list | float | int [source]#
Internal wrapper function for __getitem__() - self[name]
- Parameters:
key (str, slice) – path to the data or key of the data object
default (object) – default value to return if key doesn’t exist
- Returns:
data or data object
- Return type:
dict, list, float, int
- list_all()#
Returns dictionary of :method:`.list_groups()` and :method:`.list_nodes()`.
- Returns:
- results of :method:`.list_groups() under the key "groups"; results of :method:`.list_nodes()` und the
key “nodes”
- Return type:
dict
- list_groups()#
Return a list of names of all nested groups.
- Returns:
group names
- Return type:
list of str
- list_nodes()#
Return a list of names of all nested nodes.
- Returns:
node names
- Return type:
list of str
- open(name: str) DummyHDFio [source]#
Descend into a sub group.
If name does not exist yet, create a new group. Calling
close()
on the returned object returns this object.- Parameters:
name (str) – name of sub group
- Returns:
sub group
- Return type:
GenericStorage
- read_dict_from_hdf(group_paths: List[str] = [], recursive: bool = False) dict | Any [source]#
Read data from the HDF5 file and return it as a dictionary.
- Parameters:
group_paths (List[str]) – List of group paths to read data from.
recursive (bool) – If True, read data recursively from all groups.
- Returns:
The read data as a dictionary or any other object if recursive is True.
- Return type:
Union[dict, Any]
- to_dict() dict [source]#
Convert the HDF5 data to a dictionary.
- Returns:
The converted dictionary.
- Return type:
dict
- to_object(class_name: str | None = None, **kwargs) object [source]#
Load the full pyiron object from an HDF5 file
- Parameters:
class_name (str, optional) – if the ‘TYPE’ node is not available in the HDF5 file a manual object type can be set, must be as reported by str(type(obj))
**kwargs – optional parameters optional parameters to override init parameters
- Returns:
pyiron object of the given class_name