A type-aware bitfield serializing / deserializing library for Python
Project description
bydantic
bydantic is a Python library for serializing and deserializing bitfields.
bydantic allows you to declaratively define bitfields as Python classes with
type hints, which then can be automatically serialized and deserialized to and
from raw bytes.
The name bydantic is a portmanteau of "bit" and "pydantic" -- just as pydantic gives developers a way to declaratively define data models with type hints and then serialize and deserialize raw objects against those models, bydantic gives developers a way to do the same with bitfields.
Installation
bydantic is available on PyPI and can be installed using pip:
pip install bydantic
Note: Consider the current version as somewhat of a developer preview. bydantic is still under active development, and the API is still not quite settled.
Quick Start
Here's a simple example of how bydantic can be used:
import bydantic as bd
class Foo(bd.Bitfield):
a: int = bd.uint_field(4)
b: int = bd.uint_field(4)
c: str = bd.str_field(n_bytes=1)
This defines a bitfield with three fields: a and b are 4-bit integers, and
c is a 1-byte (8-bit) string. You can then serialize and deserialize instances
of Foo to and from raw bytes:
foo = Foo(a=1, b=2, c="x")
# Serialize to bytes
print(foo.to_bytes()) # b'\x12x'
# Deserialize from bytes
foo2 = Foo.from_bytes_exact(b'\x34y')
print(foo2) # Foo(a=3, b=4, c='y')
The power of bydantic, however, is that field types can be composed into complex data structures. For example:
from __future__ import annotations
import bydantic as bd
class Foo(bd.Bitfield):
a: int = bd.uint_field(4)
b: int = bd.uint_field(4)
c: str = bd.str_field(n_bytes=1)
def discriminator(b: Bar):
return bd.int_field(8) if b.d[0].a == 0 else bd.str_field(n_bytes=1)
class Bar(bd.Bitfield):
d: list[Foo] = bd.list_field(Foo, n_items = 2)
e: int | str = bd.dynamic_field(discriminator)
bar = Bar(d=[Foo(a=0, b=1, c="x"), Foo(a=2, b=3, c="y")], e=42)
# Serialize to bytes
print(bar.to_bytes()) # b'\x01x#y*'
# Deserialize from bytes
bar2 = Bar.from_bytes_exact(b'\x01x#y*')
print(bar2) # Bar(d=[Foo(a=0, b=1, c='x'), Foo(a=2, b=3, c='y')], e=42)
This just scratches the surface of what bydantic can do... continue reading the docs for more info.
Features
- Field type primitives
(e.g.
int_field,str_field,bytes_field, etc.) - Field type combinators
(e.g.
list_field,dynamic_field,mapped_field, etc.) - Serialization & Deserialization context
- Bitfield reordering / alignment
- Clear error messages for serialization / deserialization failures, even when fields are deeply nested
Related Projects
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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file bydantic-0.0.1.tar.gz.
File metadata
- Download URL: bydantic-0.0.1.tar.gz
- Upload date:
- Size: 21.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3fbed8141584cf6deb83f555aca0ddc817feb054707fc256b9e629b7a9a11011
|
|
| MD5 |
02a9099a36c77c3a9ce76bd14fd84de9
|
|
| BLAKE2b-256 |
ce17baa1dfd4686ad588bba28b055b70f76163c55b364a6afca6672e642e95df
|
Provenance
The following attestation bundles were made for bydantic-0.0.1.tar.gz:
Publisher:
publish-release.yml on khusmann/bydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bydantic-0.0.1.tar.gz -
Subject digest:
3fbed8141584cf6deb83f555aca0ddc817feb054707fc256b9e629b7a9a11011 - Sigstore transparency entry: 198378678
- Sigstore integration time:
-
Permalink:
khusmann/bydantic@3ecc31dfdbde6c4a3242b13374caea9da6b506f5 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/khusmann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-release.yml@3ecc31dfdbde6c4a3242b13374caea9da6b506f5 -
Trigger Event:
push
-
Statement type:
File details
Details for the file bydantic-0.0.1-py3-none-any.whl.
File metadata
- Download URL: bydantic-0.0.1-py3-none-any.whl
- Upload date:
- Size: 18.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
7cbbce4df4f3e5576bc015bcd0c431b8a8c78943943d77865d06ebd24797c169
|
|
| MD5 |
cff017fa4ed9d95553ab1b80cb00b39a
|
|
| BLAKE2b-256 |
57d660aea3e6c81b3e2bebeb206834f8a7839e2fe5d7299165c5dfe3c5786bb8
|
Provenance
The following attestation bundles were made for bydantic-0.0.1-py3-none-any.whl:
Publisher:
publish-release.yml on khusmann/bydantic
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bydantic-0.0.1-py3-none-any.whl -
Subject digest:
7cbbce4df4f3e5576bc015bcd0c431b8a8c78943943d77865d06ebd24797c169 - Sigstore transparency entry: 198378680
- Sigstore integration time:
-
Permalink:
khusmann/bydantic@3ecc31dfdbde6c4a3242b13374caea9da6b506f5 -
Branch / Tag:
refs/tags/v0.0.1 - Owner: https://github.com/khusmann
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
publish-release.yml@3ecc31dfdbde6c4a3242b13374caea9da6b506f5 -
Trigger Event:
push
-
Statement type: