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.

Files for structdc, version 0.0.3
Filename, size File type Python version Upload date Hashes
Filename, size structdc-0.0.3-py3-none-any.whl (3.0 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size structdc-0.0.3.tar.gz (1.9 kB) File type Source Python version None Upload date Hashes View

Supported by

Pingdom Pingdom Monitoring Google Google Object Storage and Download Analytics Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN DigiCert DigiCert EV certificate StatusPage StatusPage Status page