xdol.x_codecs#
Extra codecs: object <-> bytes pairs, and stores that apply them.
Besides resolve_values_to_bytes(), this module holds the value-encoding half of the
persistent memoizer (xdol.memoize):
Codec– anencode/decodepair, with ready-madeJSON_CODEC(human-readable, diff-friendly),TEXT_CODECandBYTES_CODEC(pass-through, for images/audio), looked up by name throughget_codec();with_codec()– apply a codec to the values of a bytes store;paired_store()– for a result that mixes metadata and a binary asset, a store whose value is(metadata, asset_bytes), persisted as<key>.json+<key>.bin.
>>> store = with_codec({}, "json")
>>> store["k"] = {"b": 1, "a": [1, 2]}
>>> store["k"]
{'a': [1, 2], 'b': 1}
>>> raw = {}
>>> pairs = paired_store(raw)
>>> pairs["img1"] = ({"prompt": "a cat"}, b"PNGDATA")
>>> sorted(raw)
['img1.bin', 'img1.json']
>>> pairs["img1"]
({'prompt': 'a cat'}, b'PNGDATA')
Module Attributes
Text / JSON results, stored human-readably so a committed cache is reviewable. |
|
|
|
Pass-through, for binary assets (image / audio bytes). |
|
|
Functions
|
Resolve a |
|
A JSON |
|
See |
|
Wrap a bytes |
Classes
|
An object <-> bytes pair, for the values of a store. |
|
Values are |
Exceptions
A stored value could not be decoded (corrupt or truncated entry, wrong codec). |
- xdol.x_codecs.BYTES_CODEC = (<function _as_bytes>, <function <lambda>>)#
Pass-through, for binary assets (image / audio bytes).
- xdol.x_codecs.CODECS: dict[str, Codec] = {'bytes': (<function _as_bytes>, <function <lambda>>), 'json': (<function json_codec.<locals>.encode>, <function json_codec.<locals>.<lambda>>), 'text': (<function <lambda>>, <function <lambda>>)}#
CODECS["pickle"] = Codec(pickle.dumps, pickle.loads).- Type:
Named codecs. Register your own
- class xdol.x_codecs.Codec(encode, decode)[source]#
Bases:
NamedTupleAn object <-> bytes pair, for the values of a store.
- exception xdol.x_codecs.CodecDecodeError[source]#
Bases:
ValueErrorA stored value could not be decoded (corrupt or truncated entry, wrong codec).
Raised by
with_codec()andPairedStorein place of whatever the decoder raised (chained as__cause__), so callers can tell “this entry is undecodable” apart from store, network or configuration failures.
- xdol.x_codecs.JSON_CODEC = (<function json_codec.<locals>.encode>, <function json_codec.<locals>.<lambda>>)#
Text / JSON results, stored human-readably so a committed cache is reviewable.
- class xdol.x_codecs.PairedStore(store, *, meta_codec='json', meta_suffix='.json', asset_suffix='.bin')[source]#
Bases:
MutableMappingValues are
(metadata, asset_bytes)pairs, kept as two entries in a bytes store.store[k] = (meta, blob)writesk + asset_suffix(blobverbatim) andk + meta_suffix({"asset_sha256": ..., "meta": meta}throughmeta_codec). The metadata entry is written last and is what makes a key exist, so a crash mid-write leaves no half-entry that reads back as valid;in/iteration/lenonly see keys whose both halves are present. The digest guards against two writers of the same key interleaving (A’s metadata with B’s asset): such a pair raisesCodecDecodeErroron read instead of returning the chimera. Entries in the older bare-metadata layout are still read (unverified).
- xdol.x_codecs.TEXT_CODEC = (<function <lambda>>, <function <lambda>>)#
strresults, stored as UTF-8.
- xdol.x_codecs.get_codec(codec)[source]#
Resolve a
Codec, a name inCODECS, or an(encode, decode)pair.- Return type:
>>> get_codec("json") is JSON_CODEC True >>> get_codec((str.encode, bytes.decode)).decode(b"x") 'x'
- xdol.x_codecs.json_codec(*, indent=2, default=None)[source]#
A JSON
Codec: keys sorted, non-ASCII kept, so stored files diff well.Strict by default: a value JSON cannot represent raises
TypeErrorrather than being silently stringified. Passdefault=(as injson.dumps()) to opt in.- Return type:
>>> c = json_codec(indent=None) >>> c.decode(c.encode({"b": "é", "a": 1})) {'a': 1, 'b': 'é'} >>> c.encode({"b": 1, "a": 2}) b'{"a": 2, "b": 1}'
- xdol.x_codecs.paired_store(store, **kwargs)[source]#
See
PairedStore.storeis a bytesMutableMapping(files, S3, …).- Return type:
- xdol.x_codecs.with_codec(store, codec='json')[source]#
Wrap a bytes
storeso its values are objects, encoded/decoded bycodec.A value that fails to decode raises
CodecDecodeError.- Return type: