Skip to main content

Converting dataclasses to and from fixed-length binary data using struct

Project description

dataclasses-struct

PyPI version Python versions Tests status Code coverage License: MIT Documentation

A simple Python package that combines dataclasses with struct for packing and unpacking Python dataclasses to fixed-length bytes representations.

Documentation: https://harrymander.xyz/dataclasses-struct

Example

from typing import Annotated

import dataclasses_struct as dcs

@dcs.dataclass_struct()
class Test:
    x: int
    y: float
    z: dcs.UnsignedShort
    s: Annotated[bytes, 10]  # fixed-length byte array of length 10

@dcs.dataclass_struct()
class Container:
    test1: Test
    test2: Test
>>> dcs.is_dataclass_struct(Test)
True
>>> t1 = Test(100, -0.25, 0xff, b'12345')
>>> dcs.is_dataclass_struct(t1)
True
>>> t1
Test(x=100, y=-0.25, z=255, s=b'12345')
>>> packed = t1.pack()
>>> packed
b'd\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\xd0\xbf\xff\x0012345\x00\x00\x00\x00\x00'
>>> Test.from_packed(packed)
Test(x=100, y=-0.25, z=255, s=b'12345\x00\x00\x00\x00\x00')
>>> t2 = Test(1, 100, 12, b'hello, world')
>>> c = Container(t1, t2)
>>> Container.from_packed(c.pack())
Container(test1=Test(x=100, y=-0.25, z=255, s=b'12345\x00\x00\x00\x00\x00'), test2=Test(x=1, y=100.0, z=12, s=b'hello, wor'))

Installation

This package is available on pypi:

pip install dataclasses-struct

To work correctly with mypy, an extension is required; add to your mypy.ini:

[mypy]
plugins = dataclasses_struct.ext.mypy_plugin

See the docs for more info on type checking.

Development and contributing

Pull requests are welcomed!

This project uses uv for packaging and dependency management. To install all dependencies (including development dependencies) into a virtualenv for local development:

uv sync

Uses pytest for testing:

uv run pytest

(You may omit the uv run if the virtualenv is activated.)

Uses ruff for linting and formatting, which is enforced on pull requests:

uv run ruff format
uv run ruff check

See pyproject.toml for the list of enabled checks. I recommend installing the provided pre-commmit hooks to ensure new commits pass linting:

pre-commit install

This will help speed-up pull requests by reducing the chance of failing CI checks.

PRs must also pass mypy checks (uv run mypy).

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

dataclasses_struct-1.5.0.tar.gz (187.7 kB view details)

Uploaded Source

Built Distribution

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

dataclasses_struct-1.5.0-py3-none-any.whl (15.1 kB view details)

Uploaded Python 3

File details

Details for the file dataclasses_struct-1.5.0.tar.gz.

File metadata

  • Download URL: dataclasses_struct-1.5.0.tar.gz
  • Upload date:
  • Size: 187.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dataclasses_struct-1.5.0.tar.gz
Algorithm Hash digest
SHA256 6349253bccc837f040bcac525bd057713f1a885bafb396023d8e3afb3d7bdd31
MD5 30b140d0486468a7300141c1cacce76f
BLAKE2b-256 4cf1bb2db16e829a0f7e3fd182e79b1d2621cfa81ef821496104ad25a6b84ff2

See more details on using hashes here.

File details

Details for the file dataclasses_struct-1.5.0-py3-none-any.whl.

File metadata

  • Download URL: dataclasses_struct-1.5.0-py3-none-any.whl
  • Upload date:
  • Size: 15.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.11.2 {"installer":{"name":"uv","version":"0.11.2","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

File hashes

Hashes for dataclasses_struct-1.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e2b02ed8e3ccef7b67225779cda26a2b9b3583b43c4137294ce9b723c77335d
MD5 81b30ceb03a97f69f4979cb13493175c
BLAKE2b-256 dbc310f3826e37740b9cadcb91a42300d2dbd72bb7c5e8151b9ae6718eef0696

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