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 details)

Uploaded Source

Built Distribution

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

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

Uploaded Python 3

File details

Details for the file structdc-0.0.3.tar.gz.

File metadata

  • Download URL: structdc-0.0.3.tar.gz
  • Upload date:
  • Size: 1.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for structdc-0.0.3.tar.gz
Algorithm Hash digest
SHA256 a0df16f12b802ec08ba2662788648798d7c0197b1783f0c1e6b46a631b5b6836
MD5 c57bbd3330907434030df6524f9a2e27
BLAKE2b-256 3637f44f05b1268f13b553d402e2e36eaf38ea9db12c96bd91c976fafd90c68d

See more details on using hashes here.

File details

Details for the file structdc-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: structdc-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 3.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.21.0 setuptools/40.6.2 requests-toolbelt/0.9.1 tqdm/4.31.1 CPython/3.7.2

File hashes

Hashes for structdc-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 bf7b8b1e0325de745967c9f83d3026822be53c0055e4298d576a8f23d63ed2b2
MD5 d684f77bd897484f0de7f58a83043142
BLAKE2b-256 6a498536ead009960de08bfadb29601c5983e6bf9615dfddb4068dc71be435f7

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