Skip to main content

A sub-byte-struct-supporting Python serialization/deserialization library for Python 3.8.

Project description

bytemaker

python-app codecov PyPI version PEP8 Licence - MIT docs Binder

What is it?

bytemaker is a Python 3.8-compatible library for bit-manipulation and byte serialization/deserialization. It brings C bitfield functionality over to Python version 3.8+. To that end, it provides methods and types for converting @dataclass-decorated classes.

What can you do with it?

  • A BitVector class analogous to Python's bytearray class, but for sub-byte bit quantities. BitVector supports all the methods you'd expect to have in a bit-centric bytearray with a few extras, to boot.
  • A set of BitTypes classes, including various-sized buffers, unsigned/signed ints, floats, and strings, that have underlying BitVector representations.
  • Support for serializing/deserializing @dataclass annotated classes, where the annotations can be ytypes, Python ctypes (c_uint8, ctypes.STRUCTURE, etc.), or Python native types pytypes (int, bool, char, float). Nested types? No problem!
  • Automagic support for handling any of the aforementioned objects via aggregate_types.to_bits_aggregate and aggregate_types.from_bits_aggregate.

How do I install it?

Run python -m pip install bytemaker.

Project intent

The main goal of the project is to ease development of projects working with compiled code (e.g. ROM hacking). As such, streaming features are currently deemphasized, although I may implement them at some later date.

Changelog

Version 0.10.1

(13 April 2026)

Breaking changes

  • Replaced reverse_endianness: bool parameter with endianness: Literal["big", "little"] = "big" across all conversion functions (to_bytes_individual, from_bytes_individual, to_bytes_aggregate, from_bytes_aggregate, bytes_to_bittype, bytes_to_pytype, ctype_to_bytes, bytes_to_ctype, ctype_to_bits, bits_to_ctype, pytype_to_bytes). The old boolean was relative and had inconsistent defaults. The new parameter is absolute, self-documenting, and matches Python conventions (int.from_bytes byteorder).
  • StructPackedBitType now stores bits in canonical big-endian order internally. Endianness is applied only at the bytes boundary by BitType.__bytes__(). This matches the stated design: "while the types have endianness, their underlying bit representations do not."
  • For ctypes, endianness reversal now checks endianness != sys.byteorder instead of a hardcoded boolean, making it correct on both little-endian and big-endian platforms.

Version 0.9.3

(12 April 2026)

Bugfixes

  • Made the conversions subpackage exposed
  • Fixed missing return statements in String.codepoint_changes and String._reverse_codepoint_changes properties (first call always returned None)
  • Fixed to_bytes_individual calling nonexistent BitType.to_bytes() method
  • Fixed from_bytes_individual passing unsupported reverse_endianness parameter to bytes_to_bittype and bytes_to_pytype
  • Fixed double endianness reversal in from_bytes_aggregate
  • Fixed from_bytes_aggregate using bit counts to slice byte objects (e.g. taking 32 bytes for a 32-bit field)
  • Fixed missing field_type assignment in from_bytes_aggregate dataclass field loop
  • Fixed count_bytes_in_unit_type using wrong ceiling division
  • Fixed StructPackedBitType.value getter ignoring its own padding for non-multiple-of-8 bit types
  • Fixed StructPackedBitType.__bytes__ applying endianness reversal on top of struct packing, which already handles endianness
  • Fixed to_bytes_aggregate crashing on certain nested dataclasses

Other

  • Replaced debug print() statements in BitVector.from_chararray with logging.debug()

Version 0.9.2

(29 August 2024)

Bugfixes

pyproject.toml did not include subpackages for PyPi, so importing from PyPi was failing to include bitvector or bittypes

Other

Relaxed typechecking of inputs in bitvector.py from Literal[0, 1] to int when in sequences. This change allows users to use e.g. [0] * 5 without typecheckers having problems.

Removed some outdated references to BitArray in BitVector.pyi.

Version 0.9.1

Added magic methods to BitTypes classes. Removed BitTypes' __hash__ functionality Modified BitTypes' __repr__ to include endianness

Version 0.9.0

Bits is now BitVector. Its API has been changed to be much more similar to bytearray. To that end, inline methods and alternative syntaxes have been winnowed where possible.

ytypes are now BitTypes, and, rather than extending from Bits, now contain BitVectors. This change was made so that, in the long run, uint:UInt8 + sint:SInt8 wouldn't be the same as concatenation, and so that str24[1] would grab the second element.

BitTypes now have full support for endianness when casting to bytes. Note that while the types have endianness, their underlying bit representations do not (because that wouldn't make much sense!). Usage of ctypes still assumes development is done on a little-endian machine. As of v0.10.1, ctypes conversions use sys.byteorder to detect the platform endianness, so they work correctly on both little-endian and big-endian machines.

Upcoming deprecations: (any BitType).to_bits() and (any BitType).from_bits(). This behavior should instead be replicated by (any BitType).bits and (any BitType)(bits)

Version 0.8.3

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

bytemaker-0.10.1.tar.gz (43.0 kB view details)

Uploaded Source

Built Distribution

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

bytemaker-0.10.1-py3-none-any.whl (47.3 kB view details)

Uploaded Python 3

File details

Details for the file bytemaker-0.10.1.tar.gz.

File metadata

  • Download URL: bytemaker-0.10.1.tar.gz
  • Upload date:
  • Size: 43.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bytemaker-0.10.1.tar.gz
Algorithm Hash digest
SHA256 494983279ffb124f6914272f16f12892751a7d968895b35cb6e2fb9f8b47c04e
MD5 039030ffde3f14b0034e7316b84607e7
BLAKE2b-256 15ecd2f7e9f64ccdf24bf88572080db5b8120848ea3be6d6d3e835dde2fb6398

See more details on using hashes here.

Provenance

The following attestation bundles were made for bytemaker-0.10.1.tar.gz:

Publisher: pypi-publish.yml on dem1995/bytemaker

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

File details

Details for the file bytemaker-0.10.1-py3-none-any.whl.

File metadata

  • Download URL: bytemaker-0.10.1-py3-none-any.whl
  • Upload date:
  • Size: 47.3 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for bytemaker-0.10.1-py3-none-any.whl
Algorithm Hash digest
SHA256 383b6a62b9e3a6619fc8a91385d74871ff2ea0130681fed2914f91fe7ea1e0be
MD5 1a025075a65f4b078655bd2d20352877
BLAKE2b-256 0bb9cd492e4cc4bd29e0ee0a78446dbd9912a88e13bce198def1f42eec5b8c6e

See more details on using hashes here.

Provenance

The following attestation bundles were made for bytemaker-0.10.1-py3-none-any.whl:

Publisher: pypi-publish.yml on dem1995/bytemaker

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