Helper models and backports
Project description
pydantic-store
A collection of Pydantic models and storage approaches for ease of reading to/from files or creating caches.
Installation
pip install pydantic-store
Optional Dependencies
Additional dependencies needed for storing as YAML or TOML (not included to keep the dependency limited to pydantic)
# For YAML support
pip install ruamel.yaml
# For TOML writing support
pip install tomli-w
# For TOML reading on Python < 3.11
pip install tomli
Quick Start
from pydantic_store import BaseModel, ListModel, DictModel, JsonStore, PydanticDBM
# Define your models
class User(BaseModel):
name: str
age: int
email: str
# Create and save to file
user = User(name="Alice", age=30, email="alice@example.com")
user.to_file("user.json")
# Load from file
loaded_user = User.from_file("user.json")
Model Types
BaseModel
Enhanced Pydantic BaseModel with file I/O capabilities.
from pydantic_store import BaseModel
class Config(BaseModel):
database_url: str
debug: bool = False
max_connections: int = 10
# Save to different formats
config = Config(database_url="postgresql://localhost/mydb")
config.to_file("config.json") # JSON format
config.to_file("config.yaml") # YAML format
config.to_file("config.toml") # TOML format
# Load from file (format auto-detected by extension)
config = Config.from_file("config.yaml")
RootModel
A generic root model for wrapping single values with validation.
from pydantic_store import RootModel
class Port(RootModel[int]):
pass
port = Port(8080)
port.to_file("port.json") # Saves: 8080
loaded_port = Port.from_file("port.json")
ListModel
A list-like model that behaves like a Python list while providing Pydantic validation.
from pydantic_store import ListModel
TodoList = ListModel[str]
todos = TodoList(["Buy groceries", "Walk the dog"])
# Use like a regular list
todos.append("Read a book")
todos.extend(["Exercise", "Cook dinner"])
print(len(todos)) # 5
print(todos[0]) # "Buy groceries"
# Persist to file
todos.to_file("todos.json")
# Load from file
loaded_todos = TodoList.from_file("todos.json")
DictModel
A dictionary-like model that behaves like a Python dict with validation.
from pydantic_store import DictModel
Settings = DictModel[str, int]
settings = Settings({"timeout": 30, "retries": 3})
# Use like a regular dict
settings["max_workers"] = 4
settings.update({"cache_size": 1000})
print(settings.keys())
print(len(settings))
# Persist to file
settings.to_file("settings.yaml")
PydanticDBM
A wrapper around the sqlite DBM backend (backported from 3.14) to store and retrieve pydantic models.
from pydantic_store import PydanticDBM
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
UserDBM = PydanticDBM[User]
# Method 1: Type subscription
with UserDBM("users.db") as db:
user = User(name="Alice", age=30)
db["alice"] = user
retrieved_user = db["alice"] # Automatically validated as User
# Method 2: Explicit storage format
with PydanticDBM("users.db", storage_format=User) as db:
db["bob"] = User(name="Bob", age=25)
JsonStore
A persistent dictionary that automatically saves changes to disk.
from pydantic_store import JsonStore
from pathlib import Path
# Connect to a JSON file (creates if doesn't exist)
store = JsonStore[str].connect(Path("data.json"))
# Changes are automatically persisted
store["user:1"] = "Alice"
store["user:2"] = "Bob"
# Data is immediately written to data.json
print(store["user:1"]) # "Alice"
Supported Formats
pydantic-store supports multiple file formats with automatic format detection:
- JSON: Always available
- YAML: Human-readable format (requires
ruamel.yaml) - TOML: Configuration-friendly format (requires
tomlifor reading,tomli-wfor writing)
You can also specify the format:
model.to_file("data.txt", file_format="json")
Licence
MIT Licence - see LICENSE.md for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pydantic_store-0.1.0.tar.gz.
File metadata
- Download URL: pydantic_store-0.1.0.tar.gz
- Upload date:
- Size: 7.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
085d8645ff5f3be627f7bdce7577ac01ae814b23af51cc733fdb97709e50a2dd
|
|
| MD5 |
91f3d15910c0376a292ae8df968196b6
|
|
| BLAKE2b-256 |
618a15b454aa4fb962af81cfa60282f35219f2c23c491fb2cc6691b25b5d0d12
|
Provenance
The following attestation bundles were made for pydantic_store-0.1.0.tar.gz:
Publisher:
auto_publish.yml on mysociety/pydantic-store
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_store-0.1.0.tar.gz -
Subject digest:
085d8645ff5f3be627f7bdce7577ac01ae814b23af51cc733fdb97709e50a2dd - Sigstore transparency entry: 705584769
- Sigstore integration time:
-
Permalink:
mysociety/pydantic-store@1f254aaa7be4f5a9cd8b7f7a5759ba1796b7ce9d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/mysociety
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
auto_publish.yml@1f254aaa7be4f5a9cd8b7f7a5759ba1796b7ce9d -
Trigger Event:
push
-
Statement type:
File details
Details for the file pydantic_store-0.1.0-py3-none-any.whl.
File metadata
- Download URL: pydantic_store-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
598dacd0324556a32bdbbfda77c2dacbc75bd46cf3e2a385f8cd2eee0d8400f2
|
|
| MD5 |
bf11fc9ae2a1a1622b28b278fde8d72e
|
|
| BLAKE2b-256 |
a21c8e1acff20de54fe8ebb9c21847ab1b1929e857a36ea16eb7def48eee1f0a
|
Provenance
The following attestation bundles were made for pydantic_store-0.1.0-py3-none-any.whl:
Publisher:
auto_publish.yml on mysociety/pydantic-store
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pydantic_store-0.1.0-py3-none-any.whl -
Subject digest:
598dacd0324556a32bdbbfda77c2dacbc75bd46cf3e2a385f8cd2eee0d8400f2 - Sigstore transparency entry: 705584779
- Sigstore integration time:
-
Permalink:
mysociety/pydantic-store@1f254aaa7be4f5a9cd8b7f7a5759ba1796b7ce9d -
Branch / Tag:
refs/heads/main - Owner: https://github.com/mysociety
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
auto_publish.yml@1f254aaa7be4f5a9cd8b7f7a5759ba1796b7ce9d -
Trigger Event:
push
-
Statement type: