Binary Models
Define models with BinaryModel
A binary model is a Python class:
from typing import Annotated
from binal import Array, BinaryModel, Float64, UInt16, UInt32
class Position(BinaryModel, byte_order="big"):
latitude: Float64
longitude: Float64
class Track(BinaryModel, byte_order="big"):
vehicle: UInt32
start: Position
points: Annotated[list[Position], Array(count_prefix=UInt16)]
track = Track(
vehicle=1042,
start=Position(25.0339, 121.5645),
points=[Position(25.0340, 121.5650), Position(25.0342, 121.5657)],
)
data = track.encode()
assert Track.decode(data) == track
Position.fixed_size() # 16
Track.fixed_size() # None: it depends on the number of points
track.encoded_size() # 54 A model is used on its own or as a field of another, as Position is here.
Byte order is declared per model with byte_order="big" or byte_order="little".
The constructor
The constructor is synthesized from the field declarations: every declared field is a constructor parameter of its declared type, in declaration order, usable positionally or by keyword. A field with a class-level default is optional. Type checkers see the synthesized signature, so a wrong argument type, an unknown keyword, or a missing required field is a static error, and IDEs complete parameter names.
Derived fields (Const, LengthOf, CountOf, Checksum, Offset, Padding) take no constructor argument; mark them = field(init=False) (from dataclasses) so the synthesized constructor excludes them; a derived field without it is a SchemaError. A derived value is read-only and computed from the instance’s current values on every access, so it never goes stale. A When-guarded field is annotated X | None and is None exactly when its condition is false; it is required like any other field unless it has a default such as = None. A field without a default cannot follow one with a default.
from dataclasses import field
from typing import Annotated
from binal import BinaryModel, Const, LengthOf, Sized, UInt8, UInt16, When
class Packet(BinaryModel, byte_order="big"):
magic: Annotated[bytes, Const(b"PKT1")] = field(init=False)
length: Annotated[UInt16, LengthOf("payload")] = field(init=False)
payload: Annotated[bytes, Sized("length")]
Packet(payload=b"hi") # length is derived, magic is constant
Packet(payload="hi") # type error: payload is bytes
Packet(payload=b"hi", unknown=1) # type error: unknown parameter
Packet() # type error: payload is required
class Reply(BinaryModel, byte_order="big"):
failed: UInt8
error: Annotated[UInt16 | None, When("failed", equals=1)] = None
Reply(failed=0) # error defaults to None, absent on the wire Nested models and order precedence
A nested model keeps its own configuration: a nested model’s own class-level byte_order / bit_order win over its parent’s, and a field-level ByteOrder marker on the field that holds the nested model wins over both. A model encodes identically standalone and nested under equivalent configuration.
Models compare by their init fields and are mutable, so they are not hashable.