Skip to main content

Save Python dictionaries into HDF5 files; load HDF5 files into Python dictionaries.

Project description

h5ify

Save Python dictionaries into HDF5 files; load HDF5 files into Python dictionaries.

The dictionary can be a nested dictionary of dictionaries, the terminal values of which are numbers, lists/tuples of numbers, arrays, etc. If the value of a key is not another dictionary, it is stored as a Dataset in the HDF5 file, otherwise it creates a new Group.

The attrs key can be used at each level of a nested dictionary to store metadata for the corresponding Group objects in .attrs. This currently cannot be used to store .attrs metadata for Dataset objects. The value for each attrs key must be a dictionary that is not nested.

Install

pip install h5ify

Examples

Make a small dictionary, then save it.

import h5ify

d = {'x': 1.0, 'y': 2, 'z': [1, 2, 3], 'attrs': {'info': 'README example'}}
h5ify.save('tmp.h5', d)

Load the saved dictionary.

dd = h5ify.load('tmp.h5')
print(dd)
{'attrs': {'info': 'README example'}, 'x': 1.0, 'y': 2, 'z': array([1, 2, 3])}

Note that lists/tuple are converted to numpy arrays by h5py.

You can use the usual h5py API to open the stored HDF5 file.

import h5py

with h5py.File('tmp.h5', 'r') as f:
    for key, val in f.items():
        print(key, val[()])
    for key, val in f.attrs.items():
        print(key, val)
x 1.0
y 2
z [1 2 3]
info README example

h5ify opens HDF5 files in a mode, meaning "Read/write if exists, create otherwise". You cannot save a dictionary with the same file name and Dataset keys.

h5ify.save('tmp.h5', d)
ValueError: Unable to synchronously create dataset (name already exists)

You can append values that are not yet saved to the same file, however.

h5ify.save('tmp.h5', {'w': 42})
print(h5ify.load('tmp.h5'))
{'attrs': {'info': 'README example'}, 'w': 42, 'x': 1.0, 'y': 2, 'z': array([1, 2, 3])}

Or you can overwrite by specifying the write mode:

h5ify.save('tmp.h5', {**d, 'w': 42}, mode = 'r')
print(h5ify.load('tmp.h5'))
{'attrs': {'info': 'README example'}, 'w': 42, 'x': 1.0, 'y': 2, 'z': array([1, 2, 3])}

Any additional keyword arguments to h5ify.save are passed to the create_dataset function in h5py.

h5ify.save('tmp.h5', {'comp': [100]}, compression = 'gzip', compression_opts = 9)

That should cover it. Let me know if you have questions!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

h5ify-0.2.2.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

h5ify-0.2.2-py3-none-any.whl (3.6 kB view details)

Uploaded Python 3

File details

Details for the file h5ify-0.2.2.tar.gz.

File metadata

  • Download URL: h5ify-0.2.2.tar.gz
  • Upload date:
  • Size: 3.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for h5ify-0.2.2.tar.gz
Algorithm Hash digest
SHA256 a803b56fe6a08a477abcb2b66c2c2071d7268fafc2981f1c02e6c1e4adbeb4da
MD5 b572ca0c5d788714c8f00bc5d6304f5e
BLAKE2b-256 7dc81503a0912f76eea1925cf1c1f74b64278b1091f5e58802a192d985238e6d

See more details on using hashes here.

File details

Details for the file h5ify-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: h5ify-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 3.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.11.10

File hashes

Hashes for h5ify-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 bc17898401f8c4efd974cdeee82f49087ba98f310d6e83500a254bcd17d52340
MD5 3fd3348614e363e269d7f4398291252b
BLAKE2b-256 7aa7c06fcc013a19d136209be772c2b15f7a12289ae2e3e3bc5f27d859c9bcf3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page