Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Python API

The Python extension is built with PyO3 and published as an abi3 wheel for CPython 3.10 and newer. The complete generated reference is here.

Encoder and decoder

The wheel does not bundle schema YAML. Load a schema from a file path in your project; this example uses the checked-in demo schema from a source checkout.

from pathlib import Path

import predsync
from predsync.generated import Telemetry, TelemetryMode

schema_yaml = Path("examples/generated_example/schema/telemetry.yaml").read_text()
sample = Telemetry(
    x=1.25,
    mode=TelemetryMode.Active,
    label="nominal",
    samples=[7, 8, 9],
)

encoder = predsync.Encoder(schema_yaml, message=Telemetry.message_index, tick_ns=1_000_000)
decoder = predsync.Decoder(schema_yaml, message=Telemetry.message_index, resync_n=1)

for tick in range(3):
    frame, info = encoder.encode(sample.to_values(), tick * 1_000_000)
    decoded = decoder.feed(frame)
    assert Telemetry.from_values(decoded.values()) == sample

Encoder.encode accepts either a field-name dict or a schema-order list. Enums cross the boundary as variant names. The returned info dict has len, n_predicted, n_included, n_refresh, n_reset and bound_applies.

DecodedFrame exposes message_index, missed, resynced, fields and values(). Each entry in fields has validity and value; check validity before using a predicted value.

EncodeError and DecodeError carry a kind string such as Malformed, BadCrc, UnknownMessageId or StaleFrame.

Generated classes

predsync.generated is installed when the package is imported. It exposes the four-field demo Telemetry and its TelemetryMode enum. Continuing the example above:

restored = Telemetry.from_values(decoded.values())
assert restored == sample

Generated classes carry schema_yaml, message_index and field_names, so they can construct an Encoder or Decoder without a second schema copy. from_values raises ValueError for an incompatible value list.

Custom predictors

Registry holds Python callables with reset, update, predict and state_hash methods. register_model_checked also verifies a saved trace against the model before registration. Create engines with encoder_with_registry or decoder_with_registry; a plain Encoder or Decoder never sees the registry.

Encoder, decoder and registry objects are unsendable. Use one per thread, or keep them on a single-threaded executor such as the default ROS 2 executor. Do not share one decoder between several publishers; the message ID identifies a message type, not a sender.

Type stubs

The wheel ships py.typed and predsync.pyi, so type checkers see typed signatures for the native module. Generated classes are plain Python classes and are checked from their emitted source.