Skip to main content

snapclass

Human-readable file persistence for Python dataclasses, an adaptation of the cool datafiles project.

snapclass is a small persistence layer built around Python dataclasses. Decorate a dataclass, give it a path pattern, and its instances can save and load themselves as readable YAML, JSON, TOML, or text.

It is built for the stuff that belongs in a repo or project folder: prompts, configs, fixtures, lightweight app state, and little durable objects with names.

You can use a Stash to pick a different location for the serialized file (with env overrides) and have special format rules when you need. You can also include a sidecar when you have a doc or binary you want to save next to it.

ORM is a snap!

from snapclass import snapclass

@snapclass("{self.name}.yml")
class Note:
    name: str
    title: str
    body: str = ""

mynote = Note(
    "first_note",
    "Today I used snapclass!",
    "It was my first day of snapclass. My note was saved to YAML for me!",
)
mynote.snapshot.save()

# Load it back later with the same name.
same_note = Note.snapshots.get("first_note")
from snapclass import snapclass, Stash, Fresh

# Create a default location with an environment override.
runsloc = Stash("./runs", env="RUNS_DIR")

@snapclass("{self.name}.yml", stash=runsloc)
class RunData:
    name: str
    # shortcuts for common boilerplate factory code
    metrics: dict[str, float] = Fresh.Dict

RunData("baseline", {"accuracy": 0.98, "loss": 0.04}).snapshot.save()
from snapclass import snapclass, Stash, sidecar

@snapclass
class Style:
    voice: str
    temperature: float

# Locations can be nested with stashes.
app = Stash("./myapp", env="MYAPP_DATA")
articles = app / "articles"

@snapclass("{self.slug}/article.yml", stash=articles)
class Article:
    slug: str
    title: str
    style: Style
    body: str = sidecar.text("{self.slug}.md")

article = Article(
    "dusk-court",
    "Dusk Court",
    Style("warm", 0.4),
    body="# Dusk Court\n\nBe brief, warm, and useful.\n",
)
loaded = Article.snapshots.get("dusk-court")

FAQ

Why use snapclass over datafiles?

datafiles is great and you should absolutely use it for your app or script! I love it so much and use it in project after project. After years of use, I've run into a few limitations-- like issues when multiple modules needed different datafiles behavior in one process (because much of the behavior control is global). I designed snapclass to isolate some of that via stashes and added some extra features I liked along the way.

License

snapclass is licensed under the MIT License. See LICENSE.

Much of snapclass's core behavior, along with portions of its implementation and test suite, is adapted from the wonderful datafiles project by Jace Browning. See THIRD_PARTY_NOTICES.md.

Download files

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

Source Distribution

snapclass-0.1.2.tar.gz (91.3 kB view details)

Uploaded Source

Built Distribution

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

snapclass-0.1.2-py3-none-any.whl (48.8 kB view details)

Uploaded Python 3

File details

Details for the file snapclass-0.1.2.tar.gz.

File metadata

  • Download URL: snapclass-0.1.2.tar.gz
  • Upload date:
  • Size: 91.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for snapclass-0.1.2.tar.gz
Algorithm Hash digest
SHA256 3a1051c920b25f47524f8701448ab7abc2596746bcb5eb6a9f835474bc8b31a3
MD5 9fc698c2dbac4c40a195998f9175c79a
BLAKE2b-256 c495be17c60dc33f09797043394ef82e212ab12027f10eef7868dc2447e3674f

See more details on using hashes here.

Provenance

The following attestation bundles were made for snapclass-0.1.2.tar.gz:

Publisher: release.yml on Mattie/snapclass

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file snapclass-0.1.2-py3-none-any.whl.

File metadata

  • Download URL: snapclass-0.1.2-py3-none-any.whl
  • Upload date:
  • Size: 48.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for snapclass-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 352914d394da6399c8455ce2cea553b0968acd5d200c54e8da26a67e3068f081
MD5 bc5bfbde549a884e24e4cf3367e5e38d
BLAKE2b-256 3da71fe5326997105950106ea1c4120b2f415b4d9db1a51c9bc375335e919fac

See more details on using hashes here.

Provenance

The following attestation bundles were made for snapclass-0.1.2-py3-none-any.whl:

Publisher: release.yml on Mattie/snapclass

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

0.2.0

2 files

0.1.4

2 files

0.1.3

2 files

This release

0.1.2 This release

2 files

0.1.1

2 files

0.1.0

2 files

Supported by

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