Converting dataclasses to and from fixed-length binary data using struct
Project description
dataclasses-struct
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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file dataclasses_struct-1.4.0.tar.gz
.
File metadata
- Download URL: dataclasses_struct-1.4.0.tar.gz
- Upload date:
- Size: 106.7 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
4f727f5251b5e12bb97a9eef3b162c76d862b341a0c2c7fc0036995ea72e63c3
|
|
MD5 |
8fe1a89120680822a6c6f01d242ece94
|
|
BLAKE2b-256 |
ba049ee69793964845a3e70cfabe5de5ea4c96db64159a9915ddf2c55d982656
|
File details
Details for the file dataclasses_struct-1.4.0-py3-none-any.whl
.
File metadata
- Download URL: dataclasses_struct-1.4.0-py3-none-any.whl
- Upload date:
- Size: 14.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.7.12
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 |
963f6ce708914de94e6c7faf39410b9bbb29b3d77e2555da5414a8b22d1b4058
|
|
MD5 |
4695bd3a4ae3d723d24e74f351b09bcf
|
|
BLAKE2b-256 |
a4e49e6e67efa2e4e2bb3adbbaff65792d16fd8cba62a49c0b977c7332191125
|