Skip to main content

Parse your binary structs into dataclasses

Project description

https://img.shields.io/pypi/v/construct-classes.svg Updates

Parse your binary data into dataclasses. Pack your dataclasses into binary data.

construct-classes rely on construct for parsing and packing. The programmer needs to manually write the Construct expressions. There is also no type verification, so it is the programmer’s responsibility that the dataclass and the Construct expression match.

For fully type annotated experience, install construct-typing.

This package typechecks with mypy and pyright.

Usage

Any child of Struct is a Python dataclass. It expects a Construct Struct expression in the SUBCON attribute. The names of the attributes of the dataclass must match the names of the fields in the Construct struct.

import construct as c
from construct_classes import Struct, subcon

class BasicStruct(Struct):
    x: int
    y: int
    description: str

    SUBCON = c.Struct(
        "x" / c.Int32ul,
        "y" / c.Int32ul,
        "description" / c.PascalString(c.Int8ul, "utf8"),
    )


data = b"\x01\x00\x00\x00\x02\x00\x00\x00\x05hello"
parsed = BasicStruct.parse(data)
print(parsed)  # BasicStruct(x=1, y=2, description='hello')

new_data = BasicStruct(x=100, y=200, description="world")
print(new_data.build())  # b'\x64\x00\x00\x00\xc8\x00\x00\x00\x05world'

construct-classes support nested structs, but you need to declare them explicitly:

class LargerStruct(Struct):
    # specify the subclass type:
    basic: BasicStruct = subcon(BasicStruct)
    # in case of a list, specify the item type:
    basic_array: List[BasicStruct] = subcon(BasicStruct)
    # the `subcon()` function supports all arguments of `dataclass.field`:
    default_array: List[BasicStruct] = subcon(BasicStruct, default_factory=list)

    # to refer to the subcon, use the `SUBCON` class attribute:
    SUBCON = c.Struct(
        "basic" / BasicStruct.SUBCON,
        "basic_array" / c.Array(2, BasicStruct.SUBCON),
        "default_array" / c.PrefixedArray(c.Int8ul, BasicStruct.SUBCON),
    )

Use dataclasses.field() to specify attributes on fields that are not subcons.

By default, subclasses of Struct are kw_only. This is specifically to allow setting default values on any fields regardless of order, so that your attributes can be listed in the subcon order.

However, you can pass any valid dataclass parameters to the Struct class via class attributes:

class MyStruct(Struct, kw_only=False, frozen=True):
    a: int
    b: int

my_struct = MyStruct(1, 2)  # ok
my_struct.a = 2  # FrozenInstanceError

Installing

Install using pip:

$ pip install construct-classes

Changelog

See CHANGELOG.rst.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

construct_classes-0.2.3.tar.gz (5.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

construct_classes-0.2.3-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

File details

Details for the file construct_classes-0.2.3.tar.gz.

File metadata

  • Download URL: construct_classes-0.2.3.tar.gz
  • Upload date:
  • Size: 5.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for construct_classes-0.2.3.tar.gz
Algorithm Hash digest
SHA256 669ccbb8545a4a67a6213a23ab6201368bb0c960d9d0861f236a9829f05b6abf
MD5 919fcbab3231961bdc6415079fef3b4d
BLAKE2b-256 4b51ccbdcd0bcf52190159f9492333f4134b0a783477f1a1063351b0196777ae

See more details on using hashes here.

File details

Details for the file construct_classes-0.2.3-py3-none-any.whl.

File metadata

  • Download URL: construct_classes-0.2.3-py3-none-any.whl
  • Upload date:
  • Size: 4.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.10.6 {"installer":{"name":"uv","version":"0.10.6","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for construct_classes-0.2.3-py3-none-any.whl
Algorithm Hash digest
SHA256 3685b737464f2bb756127c3d87058e5709772c9ed34952a587738b660f4838b1
MD5 7cafe2aeb8dcdc9d9e302bef8e095b01
BLAKE2b-256 0fbcedccca24faff8437a885f670ec15fb61f46abd5be4c3ca571389ba184d75

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page