Skip to main content

PersistedState

Simple and fast solution for persisting small states:

from persistedstate import PersistedState

STATE = PersistedState("example.state", last_id=0)

for current_id in range(STATE.last_id + 1, 100_001):
    print(f"Processing item #{current_id}")
    STATE.last_id = current_id

print("Processing DONE.")

You can interrupt this script, and next time it will continue from the first unprocessed item. As another example see benchmarks/perftest.py.

Mapped YAML state file

The use case is persisting small amount of data which can be edited easily with a text editor. The database is an UTF-8 encoded YAML file, so it can be highlighted and edited manually if needed. (It also uses YAML stream file format for journal.) The YAML file is fully mapped to Python objects, so every change is synchronized to disk immediately. E.g. you can do this:

STATE = PersistedState("state.yaml", processed_items=[])
STATE.setdefault("key", {})
STATE["key"].setdefault("nested", 2)

STATE.processed_items.append("<some item>")
STATE["key"]["nested"] += 1

Note: slice operations on lists are not supported (e.g. state.mylist[1:3]). Use individual index access instead.

Failure tolerance

It uses Write-Ahead-Logging and atomic vacuum, so there will be no data loss.

Thread safe

Changing the state is thread safe. You also can use the ._thread_lock attribute to make atomic changes:

def atomic_increment():
    with STATE._thread_lock:
        STATE.counter += 1

Performance

For its use case it outperforms existing key-value store modules. For example incrementing a counter (for the details see benchmarks/perftest.py):

state.counter = 0
for _ in range(COUNT_TO):
    state.counter += 1

Counting to 10,000 (using Windows and Python 3.11):

PersistedState   0.193 sec
DiskCache        1.222 sec
SqliteDict       3.089 sec
Lmdb            14.944 sec
Shelve          39.771 sec

The example seems to be silly, but this is very close to the use case it was developed for. For complex data structures or big amount of data I suggest using other libraries, like DiskCache. (The rule of thumb is when your state file is too big to be edited easily in your favorite text editor, you may think about using another key-value store library.)

Metadata

Release files for persistedstate 26.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 persistedstate 26.2
File Size Uploaded Provenance Actions
persistedstate-26.2.tar.gz 5.7 kB Without provenance Details for persistedstate-26.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for persistedstate 26.2
File Interpreter ABI Platform Actions
persistedstate-26.2-py3-none-any.whl Python 3 none any Details for persistedstate-26.2-py3-none-any.whl

Total release size: 12.0 kB

Release files / persistedstate-26.2.tar.gz

Download URL persistedstate-26.2.tar.gz
Size 5.7 kB
Tags Source
SHA-256 checksum
How to use checksums
6af1a558ec070db6a274c7a319ec20a64e10c42c57d23b3c78ed8cefaa566a8d
BLAKE2b-256 checksum
How to use checksums
97c5d58e6f932c20732b0a3aa5e7a6eb6a3bb08333badc3602bade60ada1047e
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / persistedstate-26.2-py3-none-any.whl

Download URL persistedstate-26.2-py3-none-any.whl
Size 6.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
2efcc7fdd0b475b1b5fe70f0cd86c5da891d7186f9bb2bd32ea27f81e79212e0
BLAKE2b-256 checksum
How to use checksums
065869bf2d4e996f644a146f1e03cbecc078bc6936d0d6978cd329f746036ccf
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.9 {"installer":{"name":"uv","version":"0.13.9","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":null,"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Security for persistedstate 26.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