h5.archive.HDFArchive

class h5.archive.HDFArchive(descriptor=None, open_flag='a', key_as_string_only=True, reconstruct_python_object=True, init={})[source]

Top-level handle to an HDF5 file.

Opens a local path, a remote URL (read-only), an in-memory buffer, or a fresh in-memory file, and exposes its contents through the HDFArchiveGroup dict-like interface.

Supports with blocks and a context-managed lifetime.

The underlying file is closed in __del__ / __exit__.

Parameters:
descriptorstr or bytes, optional
  • If descriptor is a simple string, it is interpreted as a local file name.

  • If descriptor is a remote url (e.g. http://ipht.cea.fr/triqs/data/single_site_bethe.output.h5) then the h5 file is downloaded as a temporary file and opened. In that case, open_flag must be 'r' (read-only). The temporary file is deleted at exit.

  • If descriptor is a bytes object, the bytes are interpreted as an hdf5 file and opened in memory only. Here open_flag must keep its default 'a'.

  • If descriptor is None (default), a new hdf5 file is created in memory only. Here open_flag must keep its default 'a'.

open_flagstr, optional

Opening mode, one of 'r' (read-only), 'w' (truncate/write) or 'a' (read-write, default). Memory files require 'a'.

key_as_string_onlybool, optional

If True (default), keys are stored as plain strings.

reconstruct_python_objectbool, optional

If True (default), registered Python classes are reconstructed on read. If False, entries are returned as raw datasets / subgroups.

inititerable of (key, value), optional

Any generator of (key, value) tuples, e.g. dict.items(). The archive is filled with these values on construction.

Methods

as_bytes()

Serialize the underlying HDF5 file to an in-memory byte buffer.

create_group(key)

Create a new empty subgroup.

create_softlink(target_key, key[, ...])

Create an HDF5 soft link key pointing at target_key.

get_raw(key)

Return the entry at key without reconstructing a Python object.

is_data(p)

Return whether p names a dataset (leaf) of this group.

is_group(p)

Return whether p names a subgroup of this group.

items()

Iterate over the (key, value) pairs stored in the group.

keys()

Return the names of the entries in this group.

update(object_with_dict_protocol)

Copy all (key, value) pairs of a mapping into the group.

values()

Iterate over the values stored in the group.

write_attr(key, val)

Write an HDF5 attribute on this group.

Examples

>>> # retrieve a remote archive (in read-only mode)
>>> h = HDFArchive('https://example.com/archive.h5', 'r')
>>>
>>> # full copy of an archive
>>> HDFArchive(f, 'w', init = HDFArchive(fmp, 'r').items())
>>>
>>> # partial copy of the file fmp, keeping only the key 'G'
>>> HDFArchive(f, 'w', init = [(k, v) for (k, v) in HDFArchive(fmp, 'r').items() if k in ['G']])
>>>
>>> # faster: objects are retrieved lazily (generator instead of list)
>>> HDFArchive(f, 'w', init = ((k, v) for (k, v) in HDFArchive(fmp, 'r').items() if k in ['G']))
>>>
>>> # partial copy with on-the-fly processing via a function P
>>> HDFArchive(f, 'w', init = ((k, P(v)) for (k, v) in HDFArchive(fmp, 'r').items() if k in ['G']))