quantlab.utils.sidecar#
JSON sidecars: small bookkeeping files beside the data they describe, rewritten atomically.
JsonSidecar holds the mechanics a resume ledger needs: an in-memory
payload, its atomic rewrite, and the symbol count and fingerprint the ledger
was last written with. A subclass sets path and _payload and decides
how its file is read and what its fingerprint hashes.
Examples#
>>> import json, tempfile
>>> from pathlib import Path
>>> class Demo(JsonSidecar):
... def __init__(self, path):
... self.path = path
... self._payload = {"symbol_count": 2, "symbol_fingerprint": "ab"}
>>> path = str(Path(tempfile.mkdtemp()) / "demo.json")
>>> sidecar = Demo(path)
>>> sidecar._flush()
>>> json.loads(Path(path).read_text())
{'symbol_count': 2, 'symbol_fingerprint': 'ab'}
>>> sidecar.symbol_count
2
Classes
A JSON sidecar's payload, its atomic rewrite and its symbol stamp. |