Skip to main content

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

persistent_bundles-0.1.0.tar.gz (2.5 kB view details)

Uploaded Source

Built Distribution

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

persistent_bundles-0.1.0-py3-none-any.whl (4.3 kB view details)

Uploaded Python 3

File details

Details for the file persistent_bundles-0.1.0.tar.gz.

File metadata

  • Download URL: persistent_bundles-0.1.0.tar.gz
  • Upload date:
  • Size: 2.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.8.14

File hashes

Hashes for persistent_bundles-0.1.0.tar.gz
Algorithm Hash digest
SHA256 1940ea378b113066cca0f1e6b32ac03d37322b5e8efa5a6e9c5cc461bb2eeec5
MD5 8d149862c972f7c0ad43e56acaa3f656
BLAKE2b-256 f24459ff6a9b0c6eff85280bc4e275f676ed27568e7fbb494bd51353f625cbb6

See more details on using hashes here.

File details

Details for the file persistent_bundles-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for persistent_bundles-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5c95ab5ff8ad568beda2bd6d93dae29e094739646cf72ee682a8c7d5ad381e91
MD5 9ab5265783ea63e371ef8f8c02c1dccf
BLAKE2b-256 4e86bb876bfb4a571ef3100689f3184124a9d683da1ce1dc935eca97d1d9bb61

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