Skip to main content

Pydantic models with debounced JSON persistence (field assignment triggers save).

Project description

python-library-presisted-model

基于 Pydantic v2 的小型持久化模型:子类描述字段,通过 load(path) 从 JSON 恢复或新建,之后在字段被赋值时按防抖策略自动写入磁盘。

PyPI 安装名(pip)python-library-presisted-model
Python 导入包名presisted_model

pip install python-library-presisted-model

何时会写入文件?

行为 是否触发落盘
模型字段 赋值(如 m.count = 1 是:会 schedule 一次防抖写入,在 连续静止满 debounce_seconds 后写入 最后一次 状态
list / dict原地修改(如 m.items.append(x) (未跟踪原地变更)
_ 开头的名称 (视为私有,不触发)
进程正常退出 已注册的实例会 尽力 atexit 刷盘(将待写入的防抖任务立即落盘,失败则忽略)

默认 debounce_seconds=0.5。短时间多次改同一字段,磁盘上通常只会看到静止后的最终 JSON。
无需也不应依赖任何「手动刷盘」API;若测试或脚本里需要确认文件已写出,在赋值后 sleep 略大于 debounce_seconds 即可。

最小示例

from pathlib import Path
from pydantic import Field
from presisted_model import PresistedModel


class AppState(PresistedModel):
    counter: int = 0
    label: str = Field(default="")


path = Path("state.json")
state = AppState.load(path, debounce_seconds=0.3)
state.counter += 1  # 触发赋值后,静止满 debounce 即落盘

API 要点

  • PresistedModel.load(path, *, debounce_seconds=0.5, json_indent=2)
    文件存在则 model_validate_json;不存在则用默认值构造;随后绑定路径并启用防抖持久化。
  • 存储格式model_dump_json 写入目标路径,写文件为 原子替换(临时文件再 replace)。

依赖

  • Python ≥ 3.10
  • pydantic >= 2

For AI agents / tooling: After pip install python-library-presisted-model, import PresistedModel from presisted_model. Persistence is debounced on Pydantic field assignment only; in-place mutations on nested mutable values are not auto-saved. Rely on debounce_seconds and process exit for durability; there is no public flush API.

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

python_library_presisted_model-0.1.1.tar.gz (4.9 kB view details)

Uploaded Source

Built Distribution

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

File details

Details for the file python_library_presisted_model-0.1.1.tar.gz.

File metadata

File hashes

Hashes for python_library_presisted_model-0.1.1.tar.gz
Algorithm Hash digest
SHA256 3dee3a1735615dd70596459ca06448ad69ba7f09ccf909ae7a6a72f3a255a826
MD5 6063c19935ce156a804d3b18139cbbec
BLAKE2b-256 390bc9b7bd17efa95862d2fac00f4647f72938e73e0406aec58b68df2948c107

See more details on using hashes here.

File details

Details for the file python_library_presisted_model-0.1.1-py3-none-any.whl.

File metadata

File hashes

Hashes for python_library_presisted_model-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 cbf41eabbe8cbc01d2fabf84029436c931643fa263c18c7e0bb28bc4ecc0230b
MD5 c0910776df30f67be492cb70ff89ebd7
BLAKE2b-256 88c8eb3bb86d1d19037d12be31b9fc6ba850662bba848a0e6af1d972bd988323

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