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
$ pip install bitvector-for-humans
$ pydoc bitvector
...
Or directly from github:
$ pip install git+https://github.com/JnyJny/bitvector.git
Motivation
- Address sub-byte bits in a less error prone way.
- Minimize subdependencies.
- 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.Structure
allows sub-byte bit fields - Python builtin
struct
provides extensive support for byte manipulations - Python3 IntEnums can be used to build bit field masks
- The plain
int
will 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
Close
Hashes for bitvector-for-humans-0.8.0.tar.gz
Algorithm | Hash digest | |
---|---|---|
SHA256 | a42469c29317f6c15b66003b04871fb253ea821faa2387747bd643d05a8106c9 |
|
MD5 | db87706f6561663df6d4b27e5cfb83ae |
|
BLAKE2b-256 | 0144f883292bb5d78ac507b16c139ec22f5580cdee0694313d5d195c124274e4 |
Close
Hashes for bitvector_for_humans-0.8.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 9c86c940bfa2a7c16e0c63af1a1e180be612cb242f8b84ef4aca55e9891e0193 |
|
MD5 | d5663995f7d4b842c33fd3c273c8b272 |
|
BLAKE2b-256 | 180a6681b917c1f2ab2aa6d4f6757994c2d6b9bbdff3c315aac638704d028f0b |