A simple pure python Bit Vector class for Humans™.
Project description
Bit Vectors For Humans™
This simple bit vector implementation aims to make addressing single
bits a little less fiddly. It can be used by itself to work with bit
fields in an integer word, but it really starts to shine when you use
the supplied BitField descriptor with a subclass of BitVector:
> from bitvector import BitVector, BitField
>
> class IOTDeviceCommand(BitVector):
> def __init__(self):
> super().__init__(size=32)
>
> power = BitField(0, 1) # offset and size
> spin = BitField(1, 1)
> speed = BitField(2, 4)
> sense = BitField(6, 2)
> red = BitField(8, 8)
> blue = BitField(16, 8)
> green = BitField(24, 8)
>
> widget_cmd = IOTDeviceCommand()
> widget_cmd.power = 1
> widget_cmd.sense = 2
> widget_cmd.speed = 5
> widget_cmd.red = 0xaa
> widget_cmd.blue = 0xbb
> widget_cmd.green = 0xcc
> widget_cmd
IOTDeviceCommand(value=0xccbbaa95, size=32)
> widget_cmd.bytes
b'\xcc\xbb\xaa\x95'
Installation
$ python3 -m pip install bitvector-for-humans
$ pydoc bitvector
...
Or directly from github:
$ pip install git+https://github.com/JnyJny/bitvector.git
Motivations
- Address sub-byte bits in a less error prone way.
- Minimize subdependencies (zero is minimized right?).
- Learn something about descriptors: ✅.
Caveats
The tests need expanding and I got lazy when writing the multi-bit setting / getting code and it could undoubtedly be improved. Pull requests gladly accepted.
Other Ways to Implement a Bit Vector
- Python builtin
ctypes.Structureallows sub-byte bit fields - Python builtin
structprovides extensive support for byte manipulations - Python3 IntEnums can be used to build bit field masks
- The plain
intwill serve admirably with bitwise operators - Provide cffi bindings to existing bit-twiddling libraries
- Use Numpy bool arrays as the "backing store"
- Other good ideas I overlooked, forgot about or just plain don't know.
Project details
Release history Release notifications | RSS feed
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 bitvector_for_humans-0.14.4.tar.gz.
File metadata
- Download URL: bitvector_for_humans-0.14.4.tar.gz
- Upload date:
- Size: 40.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
df33ad1011c272faa11c1aa4d6bd6bee26b91438c1b59b72b762b43b4697b950
|
|
| MD5 |
10be571ada4883cc86bdef1f62eb865a
|
|
| BLAKE2b-256 |
30e7dcadb4fbb22eee67b601ded3ecf7c25a5407023897d5007cfa350bae3809
|
Provenance
The following attestation bundles were made for bitvector_for_humans-0.14.4.tar.gz:
Publisher:
release.yaml on JnyJny/bitvector
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bitvector_for_humans-0.14.4.tar.gz -
Subject digest:
df33ad1011c272faa11c1aa4d6bd6bee26b91438c1b59b72b762b43b4697b950 - Sigstore transparency entry: 216630705
- Sigstore integration time:
-
Permalink:
JnyJny/bitvector@6272e57f2f6131e901a9e9c1f079f9cdd5631f64 -
Branch / Tag:
refs/tags/v0.14.4 - Owner: https://github.com/JnyJny
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@6272e57f2f6131e901a9e9c1f079f9cdd5631f64 -
Trigger Event:
push
-
Statement type:
File details
Details for the file bitvector_for_humans-0.14.4-py3-none-any.whl.
File metadata
- Download URL: bitvector_for_humans-0.14.4-py3-none-any.whl
- Upload date:
- Size: 10.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.12.9
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
60c0eba8e5566082013006cc735eac5695fd8d89a114d39a692d108d073d7c2f
|
|
| MD5 |
e4bc90dc1e1298303f475ff71e91e4a7
|
|
| BLAKE2b-256 |
8cfb35c95328c4abd3b7f15c44fa109088504c835d98771842ba179179d4dc7c
|
Provenance
The following attestation bundles were made for bitvector_for_humans-0.14.4-py3-none-any.whl:
Publisher:
release.yaml on JnyJny/bitvector
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
bitvector_for_humans-0.14.4-py3-none-any.whl -
Subject digest:
60c0eba8e5566082013006cc735eac5695fd8d89a114d39a692d108d073d7c2f - Sigstore transparency entry: 216630713
- Sigstore integration time:
-
Permalink:
JnyJny/bitvector@6272e57f2f6131e901a9e9c1f079f9cdd5631f64 -
Branch / Tag:
refs/tags/v0.14.4 - Owner: https://github.com/JnyJny
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
release.yaml@6272e57f2f6131e901a9e9c1f079f9cdd5631f64 -
Trigger Event:
push
-
Statement type: