pysdic.core.serialization.Hdf5SerializerMixin#

Note

This class is internal: it is not exported in the pysdic namespace. Its methods are available on all the classes that inherit from it.

Serialization to and from HDF5 files (requires the optional package h5py).

class Hdf5SerializerMixin[source]#

Bases: SerializerMixin

Mixin class to provide HDF5 serialization and deserialization capabilities. Classes that inherit from this mixin can be easily converted to and from HDF5 format.

The dictionary returned by to_dict is mapped to HDF5 as follows:

  • dict -> group (written recursively).

  • non-empty list / tuple of dict -> group with subgroups "0", "1", … (read back as a list of dict).

  • numpy.ndarray, list, tuple, set -> dataset (read back as a numpy.ndarray). Arrays of strings are stored as UTF-8 strings and read back as arrays of str.

  • int, float, bool, str, bytes and numpy scalars -> attribute (read back as Python scalars).

  • None -> attribute with a sentinel value (read back as None).

Important

Lists, tuples and sets are read back as numpy arrays. When deserializing using the from_dict method, convert them back to lists or tuples if needed.

Important

Keys must be non-empty strings without "/" ("/" is the HDF5 path separator).

classmethod from_hdf5_file(file_path, **kwargs)[source]#

Deserialize an object from an HDF5 file.

Parameters:

file_path (Any) – The path to the HDF5 file to be read (str, Path, …).

Returns:

An instance of the class, populated with the data read from the HDF5 file.

Return type:

Any

to_hdf5_file(file_path, *, make_dirs=False, **kwargs)[source]#

Serialize the object to an HDF5 file.

Parameters:
  • file_path (Any) – The path to the file where the HDF5 representation of the class will be saved (str, Path, …).

  • make_dirs (bool, optional (default: False)) – If True, the method will create any missing directories in the file path before writing the HDF5.

Return type:

None