Save and load versioned Python object bundles with metadata.
Project description
persistent-bundles
Save and load versioned Python object bundles with optional metadata.
persistent-bundles is a small library for objects that manage their own on-disk persistence. It writes a bundle directory containing the saved object, a manifest with the object's class name and semantic version, and optional JSON metadata.
Installation
pip install persistent-bundles
Usage
import json
from pathlib import Path
from typing import Self, override
from persistent_bundles.api import load_bundle, save_bundle
from persistent_bundles.types import Loadable, Savable
class MyObject(Savable, Loadable):
def __init__(self, number: int) -> None:
self.number = number
@override
def save(self, path: Path) -> None:
with (path / "number.json").open("w") as f:
json.dump({"number": self.number}, f)
@classmethod
@override
def load(cls, path: Path) -> Self:
with (path / "number.json").open("r") as f:
data = json.load(f)
return cls(data["number"])
@classmethod
@override
def get_class_version(cls) -> str:
return "1.0.0"
path = Path("example.bundle")
save_bundle(MyObject(42), path, metadata={"created_by": "example"})
obj, metadata = load_bundle(path, {"MyObject": MyObject})
Bundles require a .bundle path suffix. When loading, the saved class version must have the same semantic-version major number as the current class version.
Development
uv run pytest
uv build
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
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 persistent_bundles-0.2.0.tar.gz.
File metadata
- Download URL: persistent_bundles-0.2.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a5f024063c7d9126835922226cb2959536ed02b05db746a3991135a616941cf0
|
|
| MD5 |
1671c978971d95130f9dcff644953a10
|
|
| BLAKE2b-256 |
f8c239c790083e46af0f3ef1801c2cebb18935789645ef6a8376f8b82cd90063
|
File details
Details for the file persistent_bundles-0.2.0-py3-none-any.whl.
File metadata
- Download URL: persistent_bundles-0.2.0-py3-none-any.whl
- Upload date:
- Size: 5.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.8.14
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
66eed613f424260e67ea472ba0638604c95669bbd594666316c8f14dacb32333
|
|
| MD5 |
d617e233662a135342ce4d6a78b43bd7
|
|
| BLAKE2b-256 |
083ac8327ebb0a65e09d23b69d542f1ab7e1b41f377854ca6b511131739daac1
|