Skip to main content

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

Metadata

Release files for dataclasses-struct 1.5.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dataclasses-struct 1.5.2
File Size Uploaded Provenance Actions
dataclasses_struct-1.5.2.tar.gz 177.2 kB Without provenance Details for dataclasses_struct-1.5.2.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for dataclasses-struct 1.5.2
File Interpreter ABI Platform Actions
dataclasses_struct-1.5.2-py3-none-any.whl Python 3 none any Details for dataclasses_struct-1.5.2-py3-none-any.whl

Total release size: 192.5 kB

Release files / dataclasses_struct-1.5.2.tar.gz

Download URL dataclasses_struct-1.5.2.tar.gz
Size 177.2 kB
Tags Source
SHA-256 checksum
How to use checksums
c1256d287366a58c4eba763cb526b023df5beb8d2bbdb96d283ccc00632bd8aa
BLAKE2b-256 checksum
How to use checksums
77dc7a3717a2899ef6894bc9bb16672a64e0fa5ef2d64cf67c55f3a4320369b8
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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}

Release files / dataclasses_struct-1.5.2-py3-none-any.whl

Download URL dataclasses_struct-1.5.2-py3-none-any.whl
Size 15.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0447a8da57742899ca56a3d9f81c799ebd591c5ed79c21e9988655d4a7be173
BLAKE2b-256 checksum
How to use checksums
ef3e753157fee8db5cbbf1d9260dea07cb19c73b33a14fec03da259b6af244a6
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.13.5 {"installer":{"name":"uv","version":"0.13.5","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}

Security for dataclasses-struct 1.5.2

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.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page