Shared Key-Value Store (pyrustic/shared)

0.3.0 · active · verified Sun Mar 29

Shared is a Python package from the Pyrustic Open Ecosystem designed for storing unstructured application data, managing configuration files, caching data, and exchanging data. It handles collections, binary data, and SQL queries, utilizing human-readable files under the hood via Paradict for serialization. It is currently at version 0.3.0 and is part of a collection of lightweight Python projects.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to create a `Dossier` instance, save a Python dictionary to it using a key, and then retrieve the data. The data is persisted to human-readable files. It also includes cleanup instructions for the created directory.

import os
from shared import Dossier, HOME
from datetime import datetime
from pathlib import Path

# Create a dossier (or access an existing one)
# For demonstration, we'll use a temporary path
# In a real app, HOME typically refers to user's home directory
# For testing, ensure 'my_test_dossier' directory is created or handled

# Use a temporary directory for quickstart if HOME is not ideal
# Ensure the directory exists or can be created
project_root = Path(os.environ.get('SHARED_DEMO_PATH', Path.cwd() / 'shared_data'))
project_root.mkdir(parents=True, exist_ok=True)

path = project_root / "my_dossier"
dossier = Dossier(path)

# Sample profile data
now = datetime.now()
profile = {
    "name": "alex",
    "access_datetime": now.isoformat(), # Store datetime as ISO format string
    "pi": 3.14,
    "books": ["Seul sur Mars", "The Fall"],
    "is_author": True,
    "fingerprint": None
}

# Save profile dictionary in the dossier
dossier.set("my_profile", profile)
print(f"Profile saved: {profile}")

# Retrieve profile dictionary
profile_bis = dossier.get("my_profile")
print(f"Profile retrieved: {profile_bis}")

# Assert that the retrieved profile matches the original (after JSON serialization)
assert profile == profile_bis
print("Profiles match!")

# Clean up (optional for quickstart, but good practice)
import shutil
if project_root.exists():
    shutil.rmtree(project_root)
    print(f"Cleaned up directory: {project_root}")

view raw JSON →