Parse your binary structs into dataclasses
Project description
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file construct_classes-0.2.2.tar.gz.
File metadata
- Download URL: construct_classes-0.2.2.tar.gz
- Upload date:
- Size: 4.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
c644026fef4d082fd6632efa974376d77e8be7d95e4e57a6df74407fc0954efd
|
|
| MD5 |
7d169b93f6439ca92ec85fe5a2eb602f
|
|
| BLAKE2b-256 |
756fe2e98ed52e94fd9db21a7f816061e0d47fef9b13077b5a9940a7b55e0b98
|
File details
Details for the file construct_classes-0.2.2-py3-none-any.whl.
File metadata
- Download URL: construct_classes-0.2.2-py3-none-any.whl
- Upload date:
- Size: 4.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.20
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
bf616b174ad53adbd388beb4fc1a5af4ba42289b186e6a741142262ead337b3f
|
|
| MD5 |
92070757c9977dd34fecabb820e564e9
|
|
| BLAKE2b-256 |
9e9abfe55ec4afaecb64512cc39f3a801e76fcc5660b5a98d6c193af527b5f23
|