Hdf5Saver

Inheritance Diagram

Inheritance diagram of tenpy.tools.hdf5_io.Hdf5Saver

Methods

Hdf5Saver.__init__(h5group[, format_selection])

Hdf5Saver.create_group_for_obj(path, obj)

Create an HDF5 group self.h5group[path] to store obj.

Hdf5Saver.memorize_save(h5gr, obj)

Store objects already saved in the memo_save.

Hdf5Saver.save(obj[, path])

Save obj in self.h5group[path].

Hdf5Saver.save_dataset(obj, path, type_repr)

Save obj as a hdf5 dataset; in dispatch table.

Hdf5Saver.save_dict(obj, path, type_repr)

Save the dictionary obj; in dispatch table.

Hdf5Saver.save_dict_content(obj, h5gr, subpath)

Save contents of a dictionary obj in the existing h5gr.

Hdf5Saver.save_dtype(obj, path, type_repr)

Save a dtype object; in dispatch table.

Hdf5Saver.save_global(obj, path, type_repr)

Save a global object like a function or class.

Hdf5Saver.save_ignored(obj, path, type_repr)

Don’t save the Hdf5Ignored object; just return None.

Hdf5Saver.save_iterable(obj, path, type_repr)

Save an iterable obj like a list, tuple or set; in dispatch table.

Hdf5Saver.save_iterable_content(obj, h5gr, …)

Save contents of an iterable obj in the existing h5gr.

Hdf5Saver.save_none(obj, path, type_repr)

Save the None object as a string (dataset); in dispatch table.

Hdf5Saver.save_range(obj, path, type_repr)

Save a range object; in dispatch table.

Hdf5Saver.save_reduce(func, args[, state, …])

Save the return values of obj.__reduce__ following the pickle protocol.

Class Attributes and Properties

Hdf5Saver.dispatch_save

class tenpy.tools.hdf5_io.Hdf5Saver(h5group, format_selection=None)[source]

Bases: object

Class to save simple enough objects into a HDF5 file.

The intended use of this class is through save_to_hdf5(), which is simply an alias for Hdf5Saver(h5group).save(obj, path).

It exports python objects to a HDF5 file such that they can be loaded with the Hdf5Loader, or a call to load_from_hdf5(), respectively.

The basic structure of this class is similar as the Pickler from pickle.

See Saving to disk: input/output for a specification of what can be saved and what the resulting datastructure is.

Parameters
  • h5group (Group) – The HDF5 group (or HDF5 File) where to save the data.

  • format_selection (dict) – This dictionary allows to set a output format selection for user-defined Hdf5Exportable.save_hdf5() implementations. For example, LegCharge checks it for the key "LegCharge".

h5group

The HDF5 group (or HDF5 File) where to save the data.

Type

Group

dispatch_save

Mapping from a type keytype to methods f of this class. The method is called as f(self, obj, path, type_repr). The call to f should save the object obj in self.h5group[path], call memorize_save(), and set h5gr.attr[ATTR_TYPE] = type_repr to a string type_repr in order to allow loading with the dispatcher in Hdf5Loader.dispatch_save[type_repr].

Type

dict

memo_save

A dictionary to remember all the objects which we already stored to h5group. The dictionary key is the object id; the value is a two-tuple of the hdf5 group or dataset where an object was stored, and the object itself. See memorize_save().

Type

dict

format_selection

This dictionary allows to set a output format selection for user-defined Hdf5Exportable.save_hdf5() implementations. For example, LegCharge checks it for the key "LegCharge".

Type

dict

save(obj, path='/')[source]

Save obj in self.h5group[path].

Parameters
  • obj (object) – The object (=data) to be saved.

  • path (str) – Path within h5group under which the obj should be saved. To avoid unwanted overwriting of important data, the group/object should not yet exist, except if path is the default '/'.

Returns

h5gr – The h5py group or dataset in which obj was saved.

Return type

Group | Dataset

create_group_for_obj(path, obj)[source]

Create an HDF5 group self.h5group[path] to store obj.

Also handle ending of path with '/', and memorize obj in memo_save.

Parameters
  • path (str) – Path within h5group under which the obj should be saved. To avoid unwanted overwriting of important data, the group/object should not yet exist, except if path is the default '/'.

  • obj (object) – The object (=data) to be saved.

Returns

  • h5group (Group) – Newly created h5py (sub)group self.h5group[path], unless path is '/', in which case it is simply the existing self.h5group['/'].

  • subpath (str) – The group.name ending with '/', such that other names can be appended to get the path for subgroups or datasets in the group.

:raises ValueError : if self.h5group[path]` already existed and path is not '/'.:

memorize_save(h5gr, obj)[source]

Store objects already saved in the memo_save.

This allows to avoid copies, if the same python object appears multiple times in the data of obj. Examples can be shared LegCharge objects or even shared Array. Using the memo also avoids crashes from cyclic references, e.g., when a list contains a reference to itself.

Parameters
  • h5gr (Group | Dataset) – The h5py group or dataset in which obj was saved.

  • obj (object) – The object saved.

save_reduce(func, args, state=None, listitems=None, dictitems=None, state_setter=None, obj=None, path=None)[source]

Save the return values of obj.__reduce__ following the pickle protocol.

save_none(obj, path, type_repr)[source]

Save the None object as a string (dataset); in dispatch table.

save_dataset(obj, path, type_repr)[source]

Save obj as a hdf5 dataset; in dispatch table.

save_iterable(obj, path, type_repr)[source]

Save an iterable obj like a list, tuple or set; in dispatch table.

save_iterable_content(obj, h5gr, subpath)[source]

Save contents of an iterable obj in the existing h5gr.

Parameters
  • obj (dict) – The data to be saved

  • h5gr (Group) – h5py Group under which the keys and values of obj should be saved.

  • subpath (str) – Name of h5gr with '/' in the end.

save_dict(obj, path, type_repr)[source]

Save the dictionary obj; in dispatch table.

save_dict_content(obj, h5gr, subpath)[source]

Save contents of a dictionary obj in the existing h5gr.

The format depends on whether the dictionary obj has simple keys valid for hdf5 path components (see valid_hdf5_path_component()) or not. For simple keys: directly use the keys as path. For non-simple keys: save list of keys und "keys" and list of values und "values".

Parameters
  • obj (dict) – The data to be saved

  • h5gr (Group) – h5py Group under which the keys and values of obj should be saved.

  • subpath (str) – Name of h5gr with '/' in the end.

Returns

type_repr – Indicates whether the data was saved in the format for a dictionary with simple keys or general keys, see comment above.

Return type

REPR_DICT_SIMPLE | REPR_DICT_GENERAL

save_range(obj, path, type_repr)[source]

Save a range object; in dispatch table.

save_dtype(obj, path, type_repr)[source]

Save a dtype object; in dispatch table.

save_ignored(obj, path, type_repr)[source]

Don’t save the Hdf5Ignored object; just return None.

save_global(obj, path, type_repr)[source]

Save a global object like a function or class.