Skip to main content

bydantic

Project Status: Active – The project has reached a stable, usable state and is being actively developed. PyPI - Version PyPI - Downloads

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 unsigned integers, and c is a 1-byte (8-bit) string:

Bitfield Diagram

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

Related Projects

Release files for bydantic 0.1.0

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

Source distribution (sdist)

Source distribution for bydantic 0.1.0
File Size Uploaded
bydantic-0.1.0.tar.gz 21.1 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for bydantic 0.1.0
File Interpreter ABI Platform
bydantic-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size:39.1 kB

Release files / bydantic-0.1.0.tar.gz

Download URL bydantic-0.1.0.tar.gz
Size 21.1 kB
Tags Source
SHA-256 checksum
How to use checksums
109742d3018048860e97393453cac079f52422417c15f75202917ada029ffbc0
BLAKE2b-256 checksum
How to use checksums
f2ebf78508b3db863e06a7657416122215182c1f51d61e547414ea176abdacc5
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 4, 2025.

Transparency log

Release files / bydantic-0.1.0-py3-none-any.whl

Download URL bydantic-0.1.0-py3-none-any.whl
Size 17.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c895f75a0a2f2d6342fffd013877943f90314d778b2e44e32d30b7a6f2b5f4f1
BLAKE2b-256 checksum
How to use checksums
40321e66493b3f1945e9ce4b2ce2cf7d7dcc9163c9c544a3ac3eb70aab99943b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/6.1.0 CPython/3.12.9

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on May 4, 2025.

Transparency log

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

0.0.3

2 release files

0.0.2

2 release files

0.0.1

2 release files

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