# h5ify
Save Python dictionaries to HDF5 files, and 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])}
```
Any additional arguments or 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!
Raw data
{
"_id": null,
"home_page": "https://github.com/mdmould/h5ify",
"name": "h5ify",
"maintainer": null,
"docs_url": null,
"requires_python": null,
"maintainer_email": null,
"keywords": null,
"author": "Matthew Mould",
"author_email": "mattdmould@gmail.com",
"download_url": "https://files.pythonhosted.org/packages/49/58/84077b80f808cdfd6b1c8b7908ed55a6dcf74b5fbfc164a7c3aa5bcda909/h5ify-0.1.1.tar.gz",
"platform": null,
"description": "# h5ify\n\nSave Python dictionaries to HDF5 files, and load HDF5 files into Python dictionaries.\n\nThe 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`.\n\nThe `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.\n\n## Install\n\n`pip install h5ify`\n\n## Examples\n\nMake a small dictionary, then save it.\n```\nimport h5ify\n\nd = {'x': 1.0, 'y': 2, 'z': [1, 2, 3], 'attrs': {'info': 'README example'}}\nh5ify.save('tmp.h5', d)\n```\n\nLoad the saved dictionary.\n```\ndd = h5ify.load('tmp.h5')\nprint(dd)\n```\n```\n{'attrs': {'info': 'README example'}, 'x': 1.0, 'y': 2, 'z': array([1, 2, 3])}\n```\nNote that lists/tuple are converted to numpy arrays by h5py.\n\nYou can use the usual h5py API to open the stored HDF5 file.\n```\nimport h5py\n\nwith h5py.File('tmp.h5', 'r') as f:\n for key, val in f.items():\n print(key, val[()])\n for key, val in f.attrs.items():\n print(key, val)\n```\n```\nx 1.0\ny 2\nz [1 2 3]\ninfo README example\n```\n\n`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.\n```\nh5ify.save('tmp.h5', d)\n```\n```\nValueError: Unable to synchronously create dataset (name already exists)\n```\n\nYou can append values that are not yet saved to the same file, however.\n```\nh5ify.save('tmp.h5', {'w': 42})\nprint(h5ify.load('tmp.h5'))\n```\n```\n{'attrs': {'info': 'README example'}, 'w': 42, 'x': 1.0, 'y': 2, 'z': array([1, 2, 3])}\n```\n\nAny additional arguments or keyword arguments to `h5ify.save` are passed to the `create_dataset` function in `h5py`.\n```\nh5ify.save('tmp.h5', {'comp': [100]}, compression = 'gzip', compression_opts = 9)\n```\n\nThat should cover it. Let me know if you have questions!\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "h5ify",
"version": "0.1.1",
"project_urls": {
"Homepage": "https://github.com/mdmould/h5ify"
},
"split_keywords": [],
"urls": [
{
"comment_text": "",
"digests": {
"blake2b_256": "50e64127eb621b18163c1a2f92740da40187fb327b23c46bb3ff9770d9036249",
"md5": "4ebea5a808cd90b18726f7ec439332fd",
"sha256": "f6c50f1b8c7388f346835cf10173a20e1e7d6f54a9ed9dfe963474048b25fe28"
},
"downloads": -1,
"filename": "h5ify-0.1.1-py3-none-any.whl",
"has_sig": false,
"md5_digest": "4ebea5a808cd90b18726f7ec439332fd",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": null,
"size": 3468,
"upload_time": "2024-06-28T21:18:18",
"upload_time_iso_8601": "2024-06-28T21:18:18.034834Z",
"url": "https://files.pythonhosted.org/packages/50/e6/4127eb621b18163c1a2f92740da40187fb327b23c46bb3ff9770d9036249/h5ify-0.1.1-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": "",
"digests": {
"blake2b_256": "495884077b80f808cdfd6b1c8b7908ed55a6dcf74b5fbfc164a7c3aa5bcda909",
"md5": "24a14d55b7e584d3870f1c0f47468979",
"sha256": "6ad0da43a358d230e1c184c1ba185a1a36fb1b502d0304635882e033a4abc823"
},
"downloads": -1,
"filename": "h5ify-0.1.1.tar.gz",
"has_sig": false,
"md5_digest": "24a14d55b7e584d3870f1c0f47468979",
"packagetype": "sdist",
"python_version": "source",
"requires_python": null,
"size": 3216,
"upload_time": "2024-06-28T21:18:19",
"upload_time_iso_8601": "2024-06-28T21:18:19.148459Z",
"url": "https://files.pythonhosted.org/packages/49/58/84077b80f808cdfd6b1c8b7908ed55a6dcf74b5fbfc164a7c3aa5bcda909/h5ify-0.1.1.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2024-06-28 21:18:19",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "mdmould",
"github_project": "h5ify",
"travis_ci": false,
"coveralls": false,
"github_actions": false,
"requirements": [
{
"name": "h5py",
"specs": []
}
],
"lcname": "h5ify"
}