Skip to main content

Annotate fields with bitstruct un/packing instructions

Project description

bitstruct-annotated - Annotate fields with bitstruct un/packing instructions

This package provides metadata classes that can be used to annotate fields with un/packing instructions for the bitstruct package. In particular, this package is intended to be used in conjunction with either dataclasses or Pydantic's BaseModel.

Besides metadata classes for each format type supported by bitstruct, this package also provides a metadata class to indicate nested structures.

Functions are provided to un/pack instances by calling bitstruct with the format string, that is assembled from the metadata annotations.

Installation

bitstruct-annotated can be installed from PyPI using pip:

pip install bitstruct-annotated

Usage

The metadata classes provided by this package can be used in conjunction with Python's built-in Annotated type annotation:

from dataclasses import dataclass
from typing import Annotated
import bitstruct_annotated as ba

@dataclass
class MyDataClass:
    other_field: int
    integer_field: Annotated[int, ba.Unsigned(size=8, msb_first=True)]
    padding_field: Annotated[int, ba.PaddingZeros(size=7, order='first')]
    boolean_field: Annotated[bool, ba.Bool(size=1, order='after:padding_field')]

mdc = MyDataClass(12, 34, 0, True)
packed = ba.pack(mdc)
# packed == b'\x01\x22'

In the example above, Annotated is used to include the un/packing instructions as additional metadata in the field's type annotations. Fields without annotation are ignored by the bitstruct_annotated.pack and bitstruct_annotated.unpack functions.

While the size argument indicates the numbers of bits to be used for the field, the order argument indicates the order in which the fields should be packed. Here, the padding_field will be packed first, followed by the boolean_field.

The msb_first argument (default is True) can be set to False to reverse the bit order of a field. Currently, the bitstruct package does not support alternate byte orders. If required, the user has to swap bytes after packing by themselves.

The bitstruct_annotated.format_string function can be used to generate and inspect the resulting bitstruct format string. To obtain values to be packed from an instance, the bitstruct_annotated.field_values function can be used:

# ... continued from the previous example
format_string = ba.format_string(MyDataClass)
# format_string == '>p7b1u8'
values = ba.field_values(mdc)
# values == (True, 34)

A similar example using Pydantic's BaseModel:

from pydantic import BaseModel
from typing import Annotated
import bitstruct_annotated as ba

class MyPydanticModel(BaseModel):
    other_field: int
    integer_field: Annotated[int, ba.Unsigned(size=8, msb_first=True)]
    padding_field: Annotated[int, ba.PaddingZeros(size=7, order='first')]
    boolean_field: Annotated[bool, ba.Bool(size=1, order='after:padding_field')]


m = MyPydanticModel(other_field=0, integer_field=34, padding_field=0, boolean_field=True)
packed = ba.pack(m)
# packed == b'\x01\x22'

The following metadata classes are provided by this package:

  • Bool: Boolean field.
  • Float: To be used for floating-point field.
  • PaddingOnes: A padding field filled with ones.
  • PaddingZeros: A padding field filled with zeros.
  • Raw: Contains raw data (bytes).
  • Signed: Signed integer field.
  • Text: Used for text fields (strings).
  • Unsigned: Unsigned integer field.
  • Nested: Nested structure - bitstruct_annotated will recurse into the field's type to look for additional annotations.

The following functions are provided by this package:

  • format_string: Generate a bitstruct format string from a class.
  • field_values: Extract values from an instance to be packed.
  • pack: Pack an instance into a byte string.
  • unpack: Unpack a byte string into an instance.

Note that the instance has to have been initialized first when unpacking. This is necessary, because the bitstruct_annotated.unpack function does not make any assumptions about the class' constructor.

For further information, refer to the docstrings of the functions and metadata classes.

Examples

Runnable example scripts are provided in the examples/ directory:

  • basic_usage.py: Minimal dataclass with packing & unpacking.
  • nesting.py: Demonstrates nested structures via Nested().
  • reordering.py: Shows ordering constraints (first, last, before, after).
  • pydantic_model.py: Use with a Pydantic BaseModel.

Run an example (after editable install):

python -m pip install -e .[dev]
python examples/basic_usage.py

Quick reference:

Goal How
Generate format string ba.format_string(MyCls)
Get values to pack ba.field_values(instance)
Pack instance ba.pack(instance)
Unpack bytes ba.unpack(instance, data)
Nested structure Annotate field with ba.Nested()
Ordering constraint `order="first"
Padding ba.PaddingZeros(bits) / ba.PaddingOnes(bits)

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

bitstruct_annotated-0.1.3.tar.gz (24.1 kB view details)

Uploaded Source

Built Distribution

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

bitstruct_annotated-0.1.3-py3-none-any.whl (19.8 kB view details)

Uploaded Python 3

File details

Details for the file bitstruct_annotated-0.1.3.tar.gz.

File metadata

  • Download URL: bitstruct_annotated-0.1.3.tar.gz
  • Upload date:
  • Size: 24.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.12.9

File hashes

Hashes for bitstruct_annotated-0.1.3.tar.gz
Algorithm Hash digest
SHA256 e0fbdf2cf9cb56a15ae6a589151737b7f80e916387dc3d4295c8b04acf23ec7a
MD5 6958475b12009dc3b794c4981dded49f
BLAKE2b-256 777fd23a04fb91ddddd32bb307b99ad946f13d10ca83697eba0528c0eff111c8

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitstruct_annotated-0.1.3.tar.gz:

Publisher: release.yml on okleinke/bitstruct-annotated

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file bitstruct_annotated-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for bitstruct_annotated-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 beedae0e4f9441c76b4f82983a253d2a9bb401536cea81fdfdf8363ecf1904c0
MD5 420f261d9d2b3257747983a9e4614e6d
BLAKE2b-256 be931e6c42b019c843313b78b15d7a1367bf21cb5185b1b04b84ae62126a72d1

See more details on using hashes here.

Provenance

The following attestation bundles were made for bitstruct_annotated-0.1.3-py3-none-any.whl:

Publisher: release.yml on okleinke/bitstruct-annotated

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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