Skip to main content

Biterator

Biterator is a Python library for dealing with bits. One at a time. But in a nice way.

The name biterator is a portmanteau of the words 'bit' and 'iterator' which hints at the core utility of this package. Two of the most useful tools are:

  1. biterator.biterate()
  2. biterator.Bits()

The biterate() function attempts to yield the most logical sequence of bits (as Booleans) for any given iterable object (and even some objects that are not typically iterable, like integers).

The Bits() class expands upon this functionality by storing parsed bits and supporting all bitwise operators. It also defines many methods that facilitate manipulation for convenience of use.

Installation

The biterator library is pip installable:

pip install biterator

Usage

Some straightforward examples using the biterate() function:

from biterator import biterate

Iterate a list of booleans.

list(biterate([True, False, True, False]))
# [True, False, True, False]

Iterate a tuple of integer 0 and 1 literals.

list(biterate(tuple([1, 0, 1, 0])))
# [True, False, True, False]

Iterate a list of string "0" and "1" literals.

list(biterate(["1", "0", "1", "0"]))
# [True, False, True, False]

Some more advanced examples:

Iterate a string containing a prefixed binary number.

list(biterate("0b1010"))
# [True, False, True, False]

Iterate the bits of an integer, given it's total bit_length.

list(biterate(bit_values=10, bit_length=6))
# [False, False, True, False, True, False]

Iterate the bits of a raw byte-string.

list(biterate(b"U"))
# [False, True, False, True, False, True, False, True]

Iterate the bits of an integer represented as a prefixed hexadecimal string.

list(biterate("0xAF"))
# [True, False, True, False, True, True, True, True]

Iterate a generator.

list(biterate(i % 2 for i in range(4)))
# [False, True, False, True]

Examples with Bits()

The Bits() class expands upon the utility of biterate() by efficiently storing bits as they are iterated over. Bits() support all bitwise operators and handle concatenation gracefully.

from biterator import Bits

Instantiate with all the same types supported by biterate.

bits = Bits('0101')
list(bits)
# [False, True, False, True]

Supports concatenation with naked biterable types.

"0xFF" + bits + "0000"
# Bits("0b1111111101010000")

Supports all bitwise operators, also works with naked biterable types.

(Bits("1100") | Bits("0011")) & "1111"
# Bits("0b1111")

Supports slicing.

Bits('10101010')[0:8:2]
# Bits("0b1111")

Bits() supports a variety of ways to represent data.

Binary.

Bits("0xDEADBEEF").bin()
# '0b1101_1110 0b1010_1101 0b1011_1110 0b1110_1111'

Hexadecimal.

Bits("0b10111010110111000000110111100101").hex(compact=True)
# '0xBADC0DE5'

Raw bytes (instantiated from an integer with provided bit_length).

bytes(Bits(5735816763073854918203775149089, 104))
# b'Hello, World!'

Decode bytes with your favorite encoder!

Bits("0x4F682C206869204D61726B21F09F988A").decode("utf-8")
# 'Oh, hi Mark!😊'

Quickly implement complex bit operations!

Implement a symmetric XOR cypher with ease! https://en.wikipedia.org/wiki/XOR_cipher

secret_code = Bits('0xBF658DC46D3068D57F9F61DBC676666A9A689E75DBD46F31')

def xor_cypher(msg: Bits, key: Bits):
    return Bits(a ^ b for i in range(0, len(msg), len(key)) for a, b in zip(msg[i: i + len(key)], key))

decrypted = xor_cypher(secret_code, Bits("0xF100FBA11"))
bytes(decrypted)
# b'Never gonna give you up!'

Because it's symmetric, you change it back with the same key!

secret_code == xor_cypher(decrypted, Bits("0xF100FBA11"))
# True

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

Please make sure to update tests as appropriate and follow all linting procedures prescribed by the project file.

Development install

To set up the development environment, fork or clone the repo and from inside the project directory run the following is a shell separate from pycharm (Windows machines will need to run a shell with admin privileges):

sudo ./init-development.sh

On Windows, PyCharm will need to be fully closed and restarted.

This will install pyenv, poetry, and activate the git-hooks for the pre-commit config.

Poetry is used to manage the virtual environment, to add packages use: poetry add <package_name>. Please visit the poetry website for further information and instruction.

Git hooks will run linting, type checking, and pythons on commit, if any checks fail the commit will be rejected.

TODO

Milestones for Version 1.0.0 release:

  • Create UnitTest suite.
  • Code coverage in git-hooks.
  • Create a workflow that publishes on PR merge to master.
  • Workflow to bump patch on each commit, bump minor on each merge to master (major bump is left as manual)
  • Add agument parsing to init-development.sh to allow uninstalling.
  • Git hook to assert pyproject.toml matches .python-version and that version is installed
  • Git hook to update pyenv on all OS's
  • Git hook to create requrirements.txt (investigate if there is actual need for this)
  • Add more of the default pre-commit-hooks for line endings etc.
  • Add install validation to init-development.sh script.
  • In init-development.sh, validate that pyenv-win\bin and pyenv-win\shims has not already been added to path.

License

MIT License

Copyright (c) 2021 Derek Strasters

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Metadata

Release files for biterator 0.4.1

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for biterator 0.4.1
File Size Uploaded Provenance Actions
biterator-0.4.1.tar.gz 17.2 kB Without provenance Details for biterator-0.4.1.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for biterator 0.4.1
File Interpreter ABI Platform Actions
biterator-0.4.1-py3-none-any.whl Python 3 none any Details for biterator-0.4.1-py3-none-any.whl

Total release size: 34.2 kB

Release files / biterator-0.4.1.tar.gz

Download URL biterator-0.4.1.tar.gz
Size 17.2 kB
Tags Source
SHA-256 checksum
How to use checksums
1f077c442d3224378c334b8e60624d829353cabaaeeb041ff2afc912336c3822
BLAKE2b-256 checksum
How to use checksums
eb8c9f37dae1bc4479b60f82132b7cbe0f05f9e6f0ead16f992cafc98bfccad0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.2 Windows/10

Release files / biterator-0.4.1-py3-none-any.whl

Download URL biterator-0.4.1-py3-none-any.whl
Size 17.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
0ef02d6a0ccaf6c27c8b5c86df2c60826c58ebc94b410d9a5a89e410f7229e8c
BLAKE2b-256 checksum
How to use checksums
a515f2118e71a1f39aa943f617973cefec01ba4ea99d3fa3c84c82d825ae419f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via poetry/1.1.11 CPython/3.9.2 Windows/10

Security for biterator 0.4.1

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page