A library for reading and writing hierarchical data files
Project description
richfile
A more natural approach to saving hierarchical data structures.
richfile saves any Python object using directory structures on disk, and loads
them back again into the same Python objects.
richfile can save any atomic Python object, including custom classes, so long
as you can write a function to save and load it. It is intended as a replacement
for things like: pickle, json, yaml, HDF5, Parquet, netCDF, zarr,
numpy, etc. when you want to save a complex data structure in a human-readable
and editable format. We find the richfile format ideal to use when you are
building a data processing pipeline and you want to contain intermediate results
in a format that allows for custom data types, is insensitive to version changes
(pickling issues), allows for easy debugging, and is human readable.
It is easy to use, the code is simple and pure python, and the operations follow ACID principles.
Installation
pip install richfile
Examples
Try out the examples in the demo_notebook.ipynb file.
Usage
Saving and loading data is simple:
## Given some complex data structure
data = {
"name": "John Doe",
"age": 25,
"address": {
"street": "1234 Elm St",
"zip": None
},
"siblings": [
"Jane",
"Jim"
],
"data": [1,2,3],
(1,2,3): "complex key",
}
## Save it
import richfile as rf
r = rf.RichFile("path/to/data.richfile").save(data)
## Load it back
data = rf.RichFile("path/to/data.richfile").load()
You can also load just a part of the data:
r = rf.RichFile("path/to/data.richfile")
first_sibling = r["siblings"][0].load() ## Lazily load a single item using pythonic indexing
print(f"First sibling: {first_sibling}")
>>> First sibling: Jane
View the contents of a richfile directory without loading it:
r.view_directory_structure()
Output:
Directory structure:
Viewing tree structure of richfile at path: ~/path/data.richfile (dict)
├── name.dict_item (dict_item)
| ├── key.json (str)
| ├── value.json (str)
|
├── age.dict_item (dict_item)
| ├── key.json (str)
| ├── value.json (int)
|
├── address.dict_item (dict_item)
| ├── key.json (str)
| ├── value.dict (dict)
| | ├── street.dict_item (dict_item)
| | | ├── key.json (str)
| | | ├── value.json (str)
| | |
| | ├── zip.dict_item (dict_item)
| | | ├── key.json (str)
| | | ├── value.json (None)
| | |
| |
|
├── siblings.dict_item (dict_item)
| ├── key.json (str)
| ├── value.list (list)
| | ├── 0.json (str)
| | ├── 1.json (str)
| |
|
├── data.dict_item (dict_item)
| ├── key.json (str)
| ├── value.list (list)
| | ├── 0.json (int)
| | ├── 1.json (int)
| | ├── 2.json (int)
| |
|
├── 5.dict_item (dict_item)
| ├── key.tuple (tuple)
| | ├── 0.json (int)
| | ├── 1.json (int)
| | ├── 2.json (int)
| |
| ├── value.json (str)
|
You can also add new data types easily:
## Add type to a RichFile object
r = rf.RichFile("path/to/data.richfile")
r.register_type(
type_name='numpy_array',
function_load=lambda path: np.load(path),
function_save=lambda path, obj: np.save(path, obj),
object_class=np.ndarray,
library='numpy',
suffix='npy',
)
## OR
## Add type to the global workspace / kernel so that all new RichFile objects can use it
rf.functions.register_type(
type_name='numpy_array',
function_load=lambda path: np.load(path),
function_save=lambda path, obj: np.save(path, obj),
object_class=np.ndarray,
library='numpy',
suffix='npy',
)
Installation from source
git clone https://github.com/RichieHakim/richfile
cd richfile
pip install -e .
Considerations and Limitations
- Inversibility: When creating custom data types, it is important to consider whether the saving and loading operations are exactly reversible.
- ACID principles are reasonably followed via the use of temporary files, file locks, and atomic operations. However, the library is not a database, and therefore cannot guarantee the same level of ACID compliance as a database. In addition, atomic replacements of existing non-empty directories require two operations, which reduces atomicity.
- Performance: Data structures with many branches will require many files and operations, which may become slow. Consider packaging highly branched data structures into a single file that supports hierarchical data, such as JSON, HDF5, Parquet, netCDF, zarr, numpy, etc. and making a custom data type for it.
TODO:
- Tests
- Documentation
- Examples
- Readme
- License
- PyPi
-
Hashing -
Item assignment (safely) - Custom saving/loading functions
-
Put the library imports in the function calls - Add handling for data without a known type
- Change name of library to something more descriptive
- Test out memmap stuff
-
Make it a .zip type - Add mutability
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file richfile-0.5.0.tar.gz.
File metadata
- Download URL: richfile-0.5.0.tar.gz
- Upload date:
- Size: 28.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8eb542b898b0dc97a0141a5b5ea315cd60d35e4eb3c6d65eeb639301daf2c2bc
|
|
| MD5 |
2ad254a48ee491930d9216cc55373560
|
|
| BLAKE2b-256 |
4c77851067f0aa16437a271e27e451436b9cb2ea751b60e2aff7d956adabe291
|
Provenance
The following attestation bundles were made for richfile-0.5.0.tar.gz:
Publisher:
pypi_release.yml on RichieHakim/richfile
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
richfile-0.5.0.tar.gz -
Subject digest:
8eb542b898b0dc97a0141a5b5ea315cd60d35e4eb3c6d65eeb639301daf2c2bc - Sigstore transparency entry: 199090836
- Sigstore integration time:
-
Permalink:
RichieHakim/richfile@6e65bfff9267193c8e0a9ec6d6bbfe15077e0340 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/RichieHakim
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi_release.yml@6e65bfff9267193c8e0a9ec6d6bbfe15077e0340 -
Trigger Event:
workflow_dispatch
-
Statement type:
File details
Details for the file richfile-0.5.0-py3-none-any.whl.
File metadata
- Download URL: richfile-0.5.0-py3-none-any.whl
- Upload date:
- Size: 24.6 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
2ca163d994323add747c41d3eeeddbbecaabeed16825a58fdfcfe1839425cc58
|
|
| MD5 |
005a85e124110cb4f29a3057952db558
|
|
| BLAKE2b-256 |
391e531de6afdc70dee80bd18ae4b1408b7ac43d7b6a80931e33c2757da503e4
|
Provenance
The following attestation bundles were made for richfile-0.5.0-py3-none-any.whl:
Publisher:
pypi_release.yml on RichieHakim/richfile
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
richfile-0.5.0-py3-none-any.whl -
Subject digest:
2ca163d994323add747c41d3eeeddbbecaabeed16825a58fdfcfe1839425cc58 - Sigstore transparency entry: 199090837
- Sigstore integration time:
-
Permalink:
RichieHakim/richfile@6e65bfff9267193c8e0a9ec6d6bbfe15077e0340 -
Branch / Tag:
refs/heads/main - Owner: https://github.com/RichieHakim
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
pypi_release.yml@6e65bfff9267193c8e0a9ec6d6bbfe15077e0340 -
Trigger Event:
workflow_dispatch
-
Statement type: