httk.data.db.codecs¶
Value codecs: exact, round-trippable encodings of rich Python values into scalar columns.
A ValueCodec describes how one field value of a given Python type is
stored across one or more scalar columns and recovered exactly. The registry
(register_value_codec() / codec_for() / codec_named())
mirrors the leaf-codec registry idiom in httk.core.vectors: a frozen
dataclass plus module-level registration functions, with built-in codecs
registered at import time.
Rational values are stored losslessly: a query/index column holds the
nearest float (comparisons on it are documented float-approximate), while a
companion *_exact text column holds a canonical exact form that is the
round-trip source of truth. The canonical text formats are:
FRACTION_EXACT_FORMAT— a reduced fraction as"p/q", always with the/qpart ("1/1"for one), denominator positive. Used forfractions.FractionandFracScalar.SURD_EXACT_FORMAT— aSurdScalaras its canonical radicand map:"r1:p1/q1;r2:p2/q2;..."sorted by squarefree radicand ("0"for zero), e.g."1:1/2;2:3/4"for1/2 + (3/4)*sqrt(2).FRACVECTOR_EXACT_FORMAT— aFracVectortensor as"d;n0,n1,n2,...": the least positive common denominatordfollowed by the integer numerators flattened row-major. Used by the schema layer for fixed-shape (Shape) fields and per-row child-table storage.
httk-core is a hard dependency of httk-data, so the httk.core vector types are imported unconditionally; nothing in this module needs sqlalchemy.
Attributes¶
The five scalar column kinds a storage backend must provide. |
|
The canonical exact text of a rational — reduced |
|
The canonical exact text of a surd scalar — |
|
The canonical exact text of a rational tensor — common denominator, then numerators row-major. |
Classes¶
An exact encoding of one Python value type across one or more scalar columns. |
Functions¶
|
Register |
|
Return the registered value-codec names, in registration order. |
|
Return the codec for |
|
Return the codec registered as |
|
The canonical |
|
Parse |
|
The canonical |
|
Parse |
|
The canonical |
|
Parse |
|
The elements of a rational tensor as a flat row-major tuple of nearest floats. |
Module Contents¶
- type httk.data.db.codecs.ScalarKind = Literal['int', 'float', 'str', 'bool', 'bytes'][source]¶
The five scalar column kinds a storage backend must provide.
- httk.data.db.codecs.FRACTION_EXACT_FORMAT: Final = 'p/q'[source]¶
The canonical exact text of a rational — reduced
"p/q", always with the/qpart.
- httk.data.db.codecs.SURD_EXACT_FORMAT: Final = 'r1:p1/q1;r2:p2/q2;...'[source]¶
The canonical exact text of a surd scalar —
radicand:coefficientterms sorted by radicand.
- httk.data.db.codecs.FRACVECTOR_EXACT_FORMAT: Final = 'd;n0,n1,n2,...'[source]¶
The canonical exact text of a rational tensor — common denominator, then numerators row-major.
- class httk.data.db.codecs.ValueCodec[source]¶
An exact encoding of one Python value type across one or more scalar columns.
The
columnstuple gives(suffix, kind)pairs: the empty suffix names the field’s own column (by convention the query column), non-empty suffixes are appended to the field name by the schema layer (e.g."_exact").encodeanddecodemap a value to and from the column-value tuple, incolumnsorder, and must round-trip exactly.- python_type: type[source]¶
The Python type this codec stores; matched exactly first, then by subclass.
- columns: tuple[tuple[str, ScalarKind], Ellipsis][source]¶
The
(column name suffix, scalar kind)pairs the codec encodes into.
- encode: collections.abc.Callable[[Any], tuple[Any, Ellipsis]][source]¶
Encode a value into one scalar per entry of
columns, in order.
- decode: collections.abc.Callable[[tuple[Any, Ellipsis]], Any][source]¶
Recover the exact value from the tuple produced by
encode.
- httk.data.db.codecs.register_value_codec(codec: ValueCodec) None[source]¶
Register
codec; raiseValueErroron a duplicate name or Python type.
- httk.data.db.codecs.known_value_codecs() list[str][source]¶
Return the registered value-codec names, in registration order.
- httk.data.db.codecs.codec_for(python_type: Any) ValueCodec | None[source]¶
Return the codec for
python_type, or None if no codec covers it.An exact type match wins; otherwise the registered codecs are scanned once in registration order and the first whose
python_typeis a base class ofpython_typeis returned (deterministic by registration order).
- httk.data.db.codecs.codec_named(name: str) ValueCodec[source]¶
Return the codec registered as
name; raiseValueErrorlisting the known names.
- httk.data.db.codecs.encode_fraction_exact(value: fractions.Fraction) str[source]¶
The canonical
FRACTION_EXACT_FORMATtext ofvalue(e.g."-7/3","1/1").
- httk.data.db.codecs.decode_fraction_exact(text: str) fractions.Fraction[source]¶
Parse
FRACTION_EXACT_FORMATtext back into an exactfractions.Fraction.
- httk.data.db.codecs.encode_surdscalar_exact(value: httk.core.SurdScalar) str[source]¶
The canonical
SURD_EXACT_FORMATtext ofvalue("0"for zero).The terms are the canonical radicand map of the surd — squarefree radicands in increasing order, each with its reduced rational coefficient — so the text is unique per value and round-trips exactly.
- httk.data.db.codecs.decode_surdscalar_exact(text: str) httk.core.SurdScalar[source]¶
Parse
SURD_EXACT_FORMATtext back into an exactSurdScalar.
- httk.data.db.codecs.encode_fracvector_exact(value: httk.core.FracVector) str[source]¶
The canonical
FRACVECTOR_EXACT_FORMATtext of a rational tensor.The tensor is flattened row-major and normalized through
to_fractions():dis the least positive common denominator of the (reduced) elements and the numerators are the elements scaled byd. The text is therefore canonical — independent of the internal denominator the input happened to carry — and lossless at arbitrary precision. The shape itself is not encoded; fixed-shape schema fields carry it in their declaration.
- httk.data.db.codecs.decode_fracvector_exact(text: str, rows: int, cols: int) httk.core.FracVector[source]¶
Parse
FRACVECTOR_EXACT_FORMATtext into arowsxcolsFracVector.The numerators are laid out row-major into a tensor of shape
(rows, cols); their count must equalrows * cols. RaisesValueErroron malformed text or a count/shape mismatch.
- httk.data.db.codecs.encode_fracvector_floats(value: httk.core.FracVector) tuple[float, Ellipsis][source]¶
The elements of a rational tensor as a flat row-major tuple of nearest floats.
This is the (documented approximate) companion of
encode_fracvector_exact(), used to fill the per-element float query/index columns of fixed-shape fields.