Skip to main content

A simple ORM-like utility for operating on local YAML files via Python dataclasses

Project description

ymlstash

A simple ORM-like utility for operating on local YAML files via Python dataclasses.

Install

Package is published on PyPI - https://pypi.org/project/ymlstash/

Install from pip or your favorite package manager:

$ pip install ymlstash

Usage

Define a dataclass:

from dataclasses import dataclass
from typing import ClassVar

@dataclass
class User:
    name: str
    age: int
    active: bool
    key: ClassVar[str] = "name"

Note the special key field which is used to denote that name should be used as the primary key field. If an object has name: "foo", it will be saved as foo.yml in the stash root directory.

Instantiate a new object:

user = User(name="yuval", age=42, active=True)

Save it to file:

from ymlstash import YmlStash

stash = YmlStash(User, "path/to/db")
stash.save(user)

This will create a yuval.yml file in the stash root directory.

When saving to file, a key field must be present on the dataclass, otherwise an explicit key must be passed:

stash.save(obj, key="custom-key")

Load from file:

user = stash.load("yuval")

List all keys existing in stash:

keys = stash.list_keys()

License

MIT

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

ymlstash-1.0.1.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

ymlstash-1.0.1-py3-none-any.whl (3.3 kB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page