construct-classes
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.
Metadata
Release files for construct-classes 0.2.3
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| construct_classes-0.2.3.tar.gz | 5.4 kB | Without provenance | Details for construct_classes-0.2.3.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| construct_classes-0.2.3-py3-none-any.whl | Python 3 | none | any | Details for construct_classes-0.2.3-py3-none-any.whl |
Total release size: 10.1 kB
Release files / construct_classes-0.2.3.tar.gz
| Download URL | construct_classes-0.2.3.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
669ccbb8545a4a67a6213a23ab6201368bb0c960d9d0861f236a9829f05b6abf
|
|
BLAKE2b-256 checksum How to use checksums |
4b51ccbdcd0bcf52190159f9492333f4134b0a783477f1a1063351b0196777ae
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Release files / construct_classes-0.2.3-py3-none-any.whl
| Download URL | construct_classes-0.2.3-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
3685b737464f2bb756127c3d87058e5709772c9ed34952a587738b660f4838b1
|
|
BLAKE2b-256 checksum How to use checksums |
0fbcedccca24faff8437a885f670ec15fb61f46abd5be4c3ca571389ba184d75
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is 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}
|
Security for construct-classes 0.2.3
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.