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.2.tar.gz (5.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.2.tar.gz.

File metadata

File hashes

Hashes for python_library_presisted_model-0.1.2.tar.gz
Algorithm Hash digest
SHA256 ad0ac9ac6f4c8829244791754fbaf54c4463f21b2c2ca0879903445fe26240ca
MD5 4958304bd97495cd4f259b2050a8b69c
BLAKE2b-256 140a994fb357fb282a3a51ed8a1bed394b7c0855761b85863221ad37998a9a77

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for python_library_presisted_model-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 edd1c3f4d2eb30a75134499d71168f4875e80622937d68b9c2b874c4ee571307
MD5 319150913f9ec64a96ffae1fc849fdfc
BLAKE2b-256 d366c349fda8ad51f45da7b180784234f423654a1a960981464044702ab82527

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