HDF5 Wrapper

The hdf5-wrapper python package adds a wrapper to h5py that greatly simplifies reading/writing to/from hdf5-format files.

Usage

Once loaded, the contents of a file can be navigated in the same way as a native python dictionary.

from geos.hdf5_wrapper import hdf5_wrapper

data = hdf5_wrapper('data.hdf5')

test = data['test']
for k, v in data.items():
  print('key: %s, value: %s' % (k, str(v)))

If the user indicates that a file should be opened in write-mode (w) or read/write-mode (a), then the file can be created or modified. Note: for these changes to be written to the disk, the wrapper may need to be closed or deleted.

from geos.hdf5_wrapper import hdf5_wrapper
import numpy as np

data = hdf5_wrapper('data.hdf5', mode='w')
data['string'] = 'string'
data['integer'] = 123
data['array'] = np.random.randn(3, 4, 5)
data['child'] = {'float': 1.234}

Existing dictionaries can be placed on the current level:

existing_dict = {'some': 'value'}
data.insert(existing_dict)

And external hdf5 format files can be linked together:

for k in ['child_a', 'child_b']:
  data.link(k, '%s.hdf5' % (k))

API

class geos.hdf5_wrapper.wrapper.hdf5_wrapper(fname: str = '', target: h5py.File | None = None, mode: str = 'r')

A class for reading/writing hdf5 files, which behaves similar to a native dict

close() None

Closes the database

copy() Dict[str, Any]

Copy the entire database into memory

Returns:

a dictionary holding the database contents

Return type:

dict

get_copy() Dict[str, Any]

Copy the entire database into memory

Returns:

a dictionary holding the database contents

Return type:

dict

insert(x: Dict[str, Any] | hdf5_wrapper) None

Insert the contents of the target object to the current location

Parameters:

x (dict, hdf5_wrapper) – the dictionary to insert

keys() Iterable[str]

Get a list of groups and arrays located at the current level

Returns:

a list of key names pointing to objects at the current level

Return type:

list

Link an external hdf5 file to this location in the database

Parameters:
  • k (str) – the name of the new link in the database

  • target (str) – the path to the external database

values() Iterable[hdf5_wrapper | Any]

Get a list of values located on the current level