Skip to Content
Binal
Fundamentals

Binary Models

Define models with BinaryModel

A binary model is a Python class:

from binal import BinaryModel, Float32, UInt32


class Point(BinaryModel, byte_order="big"):
    id: UInt32
    x: Float32
    y: Float32
    z: Float32


point = Point(id=1, x=1.0, y=2.0, z=3.0)
point.encode()
Point.decode(data)
Point.fixed_size()  # 16
point.encoded_size()  # 16

Models are independently usable and naturally composable: a model can contain other models as fields.

Byte order is declared per model with byte_order="big" or byte_order="little".