Skip to main content

Data class mapper for C structures

Project description

PyPI

Data class mapper for C structures

Warning: alpha version, work in progress

Install

pip install structdc

Features

  • Supports packed and aligned structures
  • Generates serialization and deserialization methods on data class creation

Example usage

from io import BytesIO
from dataclasses import dataclass
from structdc import StructMixin, Uint32, Uint64


@dataclass
class AlignedStruct(StructMixin):
    a: Uint32
    b: Uint64


input = BytesIO(b'\xff\xff\xff\xff\x00\x00\x00\x00\xee\xee\xee\xee\xee\xee\xee\xee')
decoded = AlignedStruct.from_bytestream(input)
print(decoded)
"""
AlignedStruct(a=4294967295, b=17216961135462248174)
"""


@dataclass
class PackedStruct(StructMixin, packed=True):
    a: Uint32
    b: Uint64

input.seek(0)

decoded = PackedStruct.from_bytestream(input)
print(decoded)
"""
PackedStruct(a=4294967295, b=17216961131453612032)
"""

output = BytesIO(b'\x00' * 16)
source_aligned = AlignedStruct(255, 65535)
source_aligned.to_bytestream(output)
output.seek(0)
print(output.getvalue())
"""
b'\xff\x00\x00\x00\x00\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00'
"""

output = BytesIO(b'\x00' * 12)
source_packed = PackedStruct(255, 65535)
source_packed.to_bytestream(output)
output.seek(0)
print(output.getvalue())
"""
b'\xff\x00\x00\x00\xff\xff\x00\x00\x00\x00\x00\x00'
"""

Coming soon

  • Float numbers
  • Strings and bytestrings
  • Arrays
  • Nested structures

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

structdc-0.0.3.tar.gz (1.9 kB view hashes)

Uploaded source

Built Distribution

structdc-0.0.3-py3-none-any.whl (3.0 kB view hashes)

Uploaded py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page