Skip to main content

A file based database using yaml as file format

Project description

YamlDB

GitHub Repo PyPI Versions PyPI Version License GitHub issues Contributors

Linux macOS Windows

YamlDB is a lightweight, easy-to-use file-based database that uses YAML as its storage format. It allows you to treat a YAML file as a dictionary in your Python application, providing a balance between the simplicity of a flat file and the functionality of a database.

Key Features

  • Human Readable: Data is stored in standard YAML, making it easy to inspect and edit manually.
  • Dot Notation: Access and set nested values using dot-separated keys (e.g., db["user.profile.name"]).
  • Powerful Querying: Integrated with JMESPath for complex searching and filtering.
  • Data Integrity:
    • Atomic Writes: Uses temporary files to ensure that crashes during saving don't corrupt your database.
    • Transactions: Supports atomic transactions via a context manager; changes are rolled back if an error occurs.
    • Concurrency Control: Implements file-based locking to prevent data corruption when multiple processes access the same file.
  • Flexible Backends: Supports both persistent file storage (:file:) and volatile in-memory storage (:memory:).

Installation

pip install yamldb

Note: Requires Python 3.8 or newer.

Quick Start

Basic CRUD Operations

from yamldb import YamlDB

# Initialize the database
db = YamlDB(filename="data.yml")

# Set values (creates parents automatically)
db["user.name"] = "Gregor"
db["user.age"] = 30
db["settings.theme"] = "dark"

# Get values with an optional default
name = db.get("user.name") # "Gregor"
city = db.get("user.city", default="Unknown") # "Unknown"

# Delete a key
db.delete("settings.theme")

# Save changes to disk
db.save()

Advanced Searching with JMESPath

YamlDB uses JMESPath for querying, allowing you to filter and transform your data.

# Sample data: {"users": [{"name": "Gregor", "age": 30}, {"name": "Alice", "age": 25}]}
db["users"] = [{"name": "Gregor", "age": 30}, {"name": "Alice", "age": 25}]

# Find users older than 28
results = db.search("[?age > `28`]")
# [{'name': 'Gregor', 'age': 30}]

# Get only the names of all users
names = db.search("users[].name")
# ['Gregor', 'Alice']

Transactions

Use the transaction() context manager to ensure a group of updates are applied atomically.

try:
    with db.transaction():
        db["account.balance"] = 100
        db["account.status"] = "active"
        # If an exception occurs here, both changes are rolled back
        raise RuntimeError("Something went wrong!")
except RuntimeError:
    print("Transaction rolled back!")

# Balance remains unchanged if it was previously different

Concurrency and Locking

YamlDB automatically handles file locking to prevent concurrent processes from corrupting the data. If a lock is held by another process, YamlDB will wait for a timeout period before raising a RuntimeError.

API Reference

YamlDB(filename="yamldb.yml", backend=":file:", data=None)

  • filename: Path to the YAML file.
  • backend: Either ":file:" (default) or ":memory:".
  • data: Optional initial dictionary to populate the DB.

Core Methods

  • get(key, default=None): Retrieves a value using dot notation.
  • set(key, value): Sets a value using dot notation. Also supports db[key] = value.
  • delete(key): Removes a key using dot notation.
  • save(filename=None): Atomically writes the current state to disk.
  • load(filename=None): Reloads the data from the file.
  • search(query): Executes a JMESPath query against the data.
  • transaction(): Context manager for atomic updates.
  • keys(): Returns a list of all keys in the database.
  • clear(): Removes all data from the database.
  • dict(): Returns the internal data as a standard Python dictionary.
  • yaml(): Returns the data as a YAML string.

Development and Tests

The best way to contribute is with issues and pull requests.

git clone https://github.com/cloudmesh/yamldb.git
cd yamldb
pip install -r requirements.txt
pip install -r requirements-dev.txt
pip install -e .

Run tests with:

pytest -v tests/

Alternatives

  • jmespath: The query language used by YamlDB.
  • TinyDB: Another lightweight document database.
  • nosqlite: A NoSQL wrapper for SQLite.
  • MongoDB: A full-featured document database (YamlDB is a simpler alternative for small-scale needs).

Contributors

Special thanks to all the contributors who have helped improve YamlDB. You can see the full list of contributors on GitHub.

Acknowledgments

Continued work was in part funded by the NSF CyberTraining: CIC: CyberTraining for Students and Technologies from Generation Z with the award numbers 1829704 and 2200409.

Download files

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

Source Distribution

yamldb-1.0.11.tar.gz (17.0 kB view details)

Uploaded Source

Built Distribution

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

yamldb-1.0.11-py2.py3-none-any.whl (9.9 kB view details)

Uploaded Python 2Python 3

File details

Details for the file yamldb-1.0.11.tar.gz.

File metadata

  • Download URL: yamldb-1.0.11.tar.gz
  • Upload date:
  • Size: 17.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for yamldb-1.0.11.tar.gz
Algorithm Hash digest
SHA256 3aeb2c33d64fa98f46f48071e96b56424f45385d7c225119fea0d98b268797c7
MD5 58f46d5ffda6a55f6779200930552d4b
BLAKE2b-256 1a1011e8702f9c979eb3ecbc63265963ebf3f97641a6080fe7fdff6127aefc4d

See more details on using hashes here.

File details

Details for the file yamldb-1.0.11-py2.py3-none-any.whl.

File metadata

  • Download URL: yamldb-1.0.11-py2.py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.14.4

File hashes

Hashes for yamldb-1.0.11-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 e35102f875d8baf7e3cd650292b845ee2e3c4b132fe35a6bc6f252c0211c8d8f
MD5 65201c5e0e7e2092fc4f5031f9a2e103
BLAKE2b-256 535035c5aeb84b7f88a11099984f0d614e3337c49071c4324577f417eaf3f5e2

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