Skip to main content

Small Pydantic helpers for cacheable models

Project description

pydantic-cacheable-model

Small helpers to make your Pydantic v2 models trivially cacheable to disk.


Installation

pip install pydantic-cacheable-model

Supports Python 3.11+


Usage

from __future__ import annotations
from datetime import datetime
from pydantic_cacheable_model import CacheableModel, CacheId

class UserModel(CacheableModel):
    email: CacheId[str]  # <- used as the cache identifier
    joined: datetime

# Create and cache to disk
u = UserModel(email="alice@example.com", joined=datetime.now())
u.cache()

# Load a single item by identifier
u2 = UserModel.load(cache_id="alice@example.com")
assert u2 == u

# Optionally handle missing caches or validation mismatches
maybe_user = UserModel.load(cache_id="missing@example.com", not_found_ok=True)

# List everything cached
all_users = UserModel.load_all_cached()

Configuration

Below are the configuration knobs you can use. This single block shows all options; pick the ones you need.

from __future__ import annotations
from datetime import datetime
from pydantic_cacheable_model import CacheableModel, CacheId

# 1) Customize cache root and directory name
class UserModel(CacheableModel):
    # Change root directory (default: ".cache")
    CACHE_ROOT = ".data-cache"

    # Force a specific subdirectory name (otherwise derived from class name)
    CACHE_DIRNAME = "users"

    email: CacheId[str]
    joined: datetime


# 2) Customize filename mapping (avoid if ids contain unsafe characters)
class FriendlyFilenames(CacheableModel):
    slug: CacheId[str]

    @classmethod
    def cache_id_to_filename(cls, *, cache_id: str) -> str:
        # Default uses a SHA-256 hash; this stores plain ids instead
        return f"{cache_id}.json"


# 3) Custom identifier logic without using CacheId
class Document(CacheableModel):
    kind: str
    slug: str

    @property
    def cache_id(self) -> str:
        return f"{self.kind}/{self.slug}"

How It Works

  • Cache location: ./.cache/<model-dir>/ by default. <model-dir> is derived from the class name, removing a trailing Model and converting CamelCase to kebab-case (e.g., UserModeluser, LongNameModellong-name).
  • File naming: sha256(cache_id).json for safe, stable filenames.
  • Data format: pretty-printed JSON. Enum values and datetime objects serialize automatically.
  • Choosing the identifier: mark one field with CacheId[T]. If you need custom logic, override the cache_id property instead.

API

  • CACHE_ROOT: str — class var; default ".cache".
  • CACHE_DIRNAME: str | None — class var; override to force subdirectory name. If None, derived from the class name.
  • cache_dir_path() -> str — directory path where this model caches files.
  • cache_id_to_filename(*, cache_id: str) -> str — classmethod; maps an id to a filename (default: SHA-256 hex + .json).
  • get_cache_path(*, cache_id: str) -> str — full path to the cache file for a given id.
  • load(*, cache_id: str, not_found_ok: bool = False, warn_mismatch: bool = True) -> Self | None — load and validate. If not_found_ok=True, returns None on missing or, when validation fails, returns None and optionally warns.
  • load_all_cached() -> list[Self] — load all cached instances for the model.
  • cache() -> None — write the instance to disk.
  • cache_path: str — the path where this instance is cached.

License

MIT — free to use and modify.

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

pydantic_cacheable_model-1.0.0.tar.gz (3.3 kB view details)

Uploaded Source

Built Distribution

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

pydantic_cacheable_model-1.0.0-py3-none-any.whl (4.5 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_cacheable_model-1.0.0.tar.gz.

File metadata

File hashes

Hashes for pydantic_cacheable_model-1.0.0.tar.gz
Algorithm Hash digest
SHA256 5d5b28bf499812ea13cca24e28235e5b6d40a7aa353fde9b59bcc953f9d782ab
MD5 e0eb9635b8d8f18a9e1ac7c7e4604f57
BLAKE2b-256 20dea33698c27c1e4eecc11441c4f7a5a3f936894913d5c9516b17c30405e941

See more details on using hashes here.

File details

Details for the file pydantic_cacheable_model-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_cacheable_model-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 e8be865c81a671e6c8e7f2f37827fc5b146230c03a525703d3400c2fe14ab220
MD5 58a863a335233bff374703c19c574b4c
BLAKE2b-256 f820e0f2bab41fdb2525bc575e6ef079a818e6fe1c99327981193ce04fc7ac57

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