Skip to main content

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.

Metadata

Release files for python-library-presisted-model 0.1.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for python-library-presisted-model 0.1.2
File Size Uploaded Provenance
python_library_presisted_model-0.1.2.tar.gz 5.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for python-library-presisted-model 0.1.2
File Interpreter ABI Platform
python_library_presisted_model-0.1.2-py3-none-any.whl Python 3 none any Details

Total release size: 12.6 kB

Release files / python_library_presisted_model-0.1.2.tar.gz

Download URL python_library_presisted_model-0.1.2.tar.gz
Size 5.9 kB
Tags Source
SHA-256 checksum
How to use checksums
ad0ac9ac6f4c8829244791754fbaf54c4463f21b2c2ca0879903445fe26240ca
BLAKE2b-256 checksum
How to use checksums
140a994fb357fb282a3a51ed8a1bed394b7c0855761b85863221ad37998a9a77
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Release files / python_library_presisted_model-0.1.2-py3-none-any.whl

Download URL python_library_presisted_model-0.1.2-py3-none-any.whl
Size 6.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
edd1c3f4d2eb30a75134499d71168f4875e80622937d68b9c2b874c4ee571307
BLAKE2b-256 checksum
How to use checksums
d366c349fda8ad51f45da7b180784234f423654a1a960981464044702ab82527
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.13.6

Security for python-library-presisted-model 0.1.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page