Skip to main content

Simple and efficient binary (de)serialization using type annotations.

Project description

bstruct

ci pypi python

Simple and efficient binary (de)serialization 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

bstruct-0.6.1.tar.gz (6.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

bstruct-0.6.1-py3-none-any.whl (6.6 kB view details)

Uploaded Python 3

File details

Details for the file bstruct-0.6.1.tar.gz.

File metadata

  • Download URL: bstruct-0.6.1.tar.gz
  • Upload date:
  • Size: 6.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.11.6 Linux/6.2.0-1015-azure

File hashes

Hashes for bstruct-0.6.1.tar.gz
Algorithm Hash digest
SHA256 390e8d3c8bade6409bbed4b6552ba24b4484e4f10d6f0498c2e9c9a0628d4093
MD5 1c48aa4ae5a7c1e74e021ba9e153ba31
BLAKE2b-256 ab930d5b39755b6970e5e6fcfe084ceac70e2647e636ffdb9b402d9691d2fe3e

See more details on using hashes here.

File details

Details for the file bstruct-0.6.1-py3-none-any.whl.

File metadata

  • Download URL: bstruct-0.6.1-py3-none-any.whl
  • Upload date:
  • Size: 6.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.7.0 CPython/3.11.6 Linux/6.2.0-1015-azure

File hashes

Hashes for bstruct-0.6.1-py3-none-any.whl
Algorithm Hash digest
SHA256 68357387131ee45e39a3c691ce600b64ec78f0312a43cb37af24aa13797ba9d0
MD5 0d649c29ee0515c13967f87f68c0fec6
BLAKE2b-256 f9d744d4e326bdc6e62bbc31c83fb7c9cf48d9c8cdfd25f81784003d821f9550

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page