bytespec

bytespec describes a sequential binary format with a typed Python class. You define how fields are represented; the library writes and reads them, calculates lengths, and advances the offset. This is useful for custom protocols and existing formats that fit this model.

Install the package (Python 3.10+):

python -m pip install bytespec
from bytespec import ProtoModel

class User(ProtoModel):
    name: str
    active: bool

user = User(name="Anna", active=True)
encoded = user.encode()           # bytes for storage or transmission
decoded = User.decode(encoded)   # a User again

print(decoded.name)  # Anna

Reading requires the same model class. Field types and their settings determine the binary representation; you can choose an integer size, a string encoding, or a custom serialization rule as needed.

Why bytespec? compares the same packet in struct, Construct, and bytespec and explains where each fits. Your first model takes this example further with other types, optional fields, and lists. If you already know what you need, open API Reference or Binary / wire format.