Simple and efficient binary (de)serialization using type annotations.
Project description
bstruct
Simple and efficient binary parsing using type annotations.
Supports easy fallback to Python's built-in struct library for maximum performance.
Getting Started
pip install bstruct
from typing import Annotated
from dataclasses import dataclass
import bstruct
@dataclass
class Measurement:
timestamp: bstruct.u32 # shorthand for: Annotated[int, bstruct.Encodings.u32]
values: Annotated[list[bstruct.u8], bstruct.Array(3)]
MeasurementEncoding = bstruct.derive(Measurement)
measurement = Measurement(
timestamp=1672764049,
values=[1, 2, 3],
)
encoded = MeasurementEncoding.encode(measurement)
decoded = MeasurementEncoding.decode(encoded)
assert decoded == measurement
See the documentation for more information.
Benchmarks
Please see the source of the benchmarks in the benchmarks directory.
Feel free to create an issue or PR should there be a problem with the methodology.
The benchmarks where executed with
pyperf
using Python 3.11.1 and
construct 2.10.68
on a MacBook Pro 2018 with a 2.3GHz i5 processor.
benchmarks/builtins.py
| Name | decode | encode |
|---|---|---|
| struct | 0.54 us | 0.23 us |
| bstruct | 2.51 us | 1.64 us |
| construct (compiled) | 9.49 us | 10.00 us |
benchmarks/native_list.py
| Name | decode | encode |
|---|---|---|
| struct | 0.17 us | 0.33 us |
| bstruct | 1.70 us | 0.59 us |
| construct (compiled) | 4.04 us | 6.61 us |
benchmarks/class_list.py
| Name | decode | encode |
|---|---|---|
| bstruct | 7.37 us | 4.81 us |
| construct (compiled) | 34.5 us | 36.6 us |
benchmarks/nested.py
| Name | decode | encode |
|---|---|---|
| bstruct | 6.05 us | 4.42 us |
| construct (compiled) | 27.6 us | 29.5 us |
Issues and Contributing
I am very happy to receive any kind of feedback or contribution. Just open an issue and let me know.
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 bstruct-0.5.0.tar.gz.
File metadata
- Download URL: bstruct-0.5.0.tar.gz
- Upload date:
- Size: 6.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.11.1 Linux/5.15.0-1024-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
af4504fcc9447ac8f4f800991a117d3a81fd5a8423374a25ef1ab649acbea512
|
|
| MD5 |
1bef7effcfb748a93173ea7ac012b594
|
|
| BLAKE2b-256 |
c22b2df78239b60527a152449e17ab16f12eef01b1762f063f7fb0f67c00e4b8
|
File details
Details for the file bstruct-0.5.0-py3-none-any.whl.
File metadata
- Download URL: bstruct-0.5.0-py3-none-any.whl
- Upload date:
- Size: 6.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.3.1 CPython/3.11.1 Linux/5.15.0-1024-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a24c4ead78379f45ee7cc85f6ac4aabb7c989a1098450c310cbda1fe892de111
|
|
| MD5 |
9eede9491eb88f4a02356c1fa8e54fd4
|
|
| BLAKE2b-256 |
eae90cc615ed7b548cb31d66bcdb670ccd26425b5fc96021cf5291ca1e6a995a
|