Metadata-Version: 2.4
Name: binal
Version: 0.5.2
Summary: Declarative binary modeling for Python. Define a format once. Get encoding, decoding, validation, and layout from the same declaration.
Keywords: binary,binary-format,bit-level,bitfield,checksum,codec,crc,data-format,decode,decoding,deserialization,encode,encoding,endian,file-format,marshalling,packet,parser,protocol,schema,serialization,sleb128,struct,uleb128,unmarshalling,validation,varint
Author: Nolan
Author-email: Nolan <300344883+nolantw@users.noreply.github.com>
License-Expression: MIT
License-File: LICENSE
Classifier: Development Status :: 4 - Beta
Classifier: Intended Audience :: Developers
Classifier: Intended Audience :: Science/Research
Classifier: Intended Audience :: Telecommunications Industry
Classifier: Natural Language :: English
Classifier: Operating System :: MacOS
Classifier: Operating System :: Microsoft :: Windows
Classifier: Operating System :: OS Independent
Classifier: Operating System :: POSIX
Classifier: Operating System :: POSIX :: Linux
Classifier: Programming Language :: Python :: 3
Classifier: Programming Language :: Python :: 3 :: Only
Classifier: Programming Language :: Python :: 3.13
Classifier: Programming Language :: Python :: 3.14
Classifier: Programming Language :: Python :: 3.15
Classifier: Programming Language :: Python :: Implementation :: CPython
Classifier: Topic :: Communications
Classifier: Topic :: Software Development
Classifier: Topic :: Software Development :: Libraries
Classifier: Topic :: Software Development :: Libraries :: Python Modules
Classifier: Topic :: System :: Archiving
Classifier: Topic :: Utilities
Requires-Dist: fastcrc>=0.5
Requires-Dist: tibs>=2.0.2
Requires-Python: >=3.13
Project-URL: Homepage, https://nolantw.github.io/binal/
Project-URL: Documentation, https://nolantw.github.io/binal/
Project-URL: Repository, https://github.com/nolantw/binal
Project-URL: Issues, https://github.com/nolantw/binal/issues
Project-URL: Changelog, https://github.com/nolantw/binal/blob/main/CHANGELOG.md
Description-Content-Type: text/markdown

<div align="center">

<h1>binal</h1>

<p><strong>Declarative binary modeling for Python</strong></p>

<p>
Define a binary format once, as a Python class, and get encoding,<br>
decoding, validation, and layout from the same declaration.
</p>

<p>
<a href="https://pypi.org/project/binal/"><img alt="PyPI version" src="https://img.shields.io/pypi/v/binal"></a>
<a href="pyproject.toml"><img alt="Python 3.13+" src="https://img.shields.io/badge/python-3.13%2B-blue"></a>
<a href="LICENSE"><img alt="License: MIT" src="https://img.shields.io/badge/license-MIT-blue"></a>
</p>

<p>
<a href="https://nolantw.github.io/binal/"><b>Documentation</b></a>
·
<a href="#capabilities"><b>Capabilities</b></a>
·
<a href="#installation"><b>Installation</b></a>
·
<a href="#usage"><b>Usage</b></a>
·
<a href="https://nolantw.github.io/binal/docs/contributing"><b>Contributing</b></a>
</p>

</div>

<br>

## Capabilities

| Area | What it covers |
|:--|:--|
| [Binary types](docs/content/docs/binary-types/index.mdx) | Integers of any width up to 128 bits, varints (LEB128, zigzag, VLQ), binary floats from 8-bit to binary128, decimal floats (BID and DPD), fixed point, BCD, bytes, and strings (sized, prefixed, terminated, greedy) |
| [Semantic types](docs/content/docs/binary-types/semantic.mdx) | UUIDs, IP and MAC addresses, timestamps, dates, times, durations, complex and rational numbers |
| [Modeling](docs/content/docs/modeling/collections.mdx) | Nested and recursive models, bit fields, lists, tuples, enums and flags, tagged and untagged unions, conditional and dependent fields, derived lengths and counts |
| [Checksums](docs/content/docs/api/checksums.mdx) | CRC-8, CRC-16, CRC-32, CRC-32C, CRC-64, and Adler-32, verified on decode |
| [I/O](docs/content/docs/io.mdx) | File, buffer, stream, and asynchronous I/O, incremental parsing and partial decoding |
| [Validation](docs/content/docs/validation.mdx) | Field constraints, cross-field validation, and structured errors with field paths and byte offsets |
| [Introspection](docs/content/docs/advanced/schema.mdx) | Static schema and runtime layout with field offsets and sizes |
| [Low-level I/O](docs/content/docs/api/io.mdx) | `BinaryReader` and `BinaryWriter` for ad-hoc formats and custom codecs |

## Installation

binal supports Python 3.13 and newer. It depends on fastcrc (CRC checksums) and tibs (bfloat16 and 8-bit floats), both with prebuilt wheels.

Add it to a uv project:

```sh
uv add binal
```

To install it in an active Python environment:

```sh
pip install binal
```

To build from source:

```sh
git clone https://github.com/nolantw/binal
cd binal
uv sync --locked
```

## Usage

```python
from io import BytesIO
from typing import Annotated

from binal import BinaryModel, Prefixed, UInt8, UInt16


class Packet(BinaryModel, byte_order="big"):
    version: UInt8
    payload: Annotated[bytes, Prefixed(UInt16)]


packet = Packet(version=1, payload=b"Hello")
data = packet.encode()  # b"\x01\x00\x05Hello"
assert Packet.decode(data) == packet

# Several records in one stream, read one at a time.
records = list(Packet.iter_read(BytesIO(data + data)))

# One record at an offset, with the bytes it consumed.
record, consumed = Packet.decode_partial(data + b"trailing")

# Where each field landed in the encoding.
print(packet.layout().total_bytes)  # 8
```

## Project status

binal is pre-1.0: the API may change between minor releases.

## License

MIT. See [LICENSE](LICENSE).
