Skip to main content

A simple pure python Bit Vector class for Humans™.

Project description

Pytest 3.7

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

  1. Address sub-byte bits in a less error prone way.
  2. Minimize subdependencies.
  3. 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

  1. Python builtin ctypes.Structure allows sub-byte bit fields
  2. Python builtin struct provides extensive support for byte manipulations
  3. Python3 IntEnums can be used to build bit field masks
  4. The plain int will serve admirably with bitwise operators
  5. Provide cffi bindings to existing bit-twiddling libraries
  6. Use Numpy bool arrays as the "backing store"
  7. Other good ideas I overlooked, forgot about or just plain don't know.

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

bitvector-for-humans-0.14.1.tar.gz (10.7 kB view details)

Uploaded Source

Built Distribution

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

bitvector_for_humans-0.14.1-py3-none-any.whl (10.5 kB view details)

Uploaded Python 3

File details

Details for the file bitvector-for-humans-0.14.1.tar.gz.

File metadata

  • Download URL: bitvector-for-humans-0.14.1.tar.gz
  • Upload date:
  • Size: 10.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.3 CPython/3.8.6 Darwin/20.2.0

File hashes

Hashes for bitvector-for-humans-0.14.1.tar.gz
Algorithm Hash digest
SHA256 7e9fe5e3d82f3c12c9da7271870564c721be18949bc42113ba46e16811da40c7
MD5 d282e3c8d4d83903f1dd1da53c57e229
BLAKE2b-256 af224ff53a67b9a2deb3a134fbc805dfd902f9dfc14b8f16df40b22560051272

See more details on using hashes here.

File details

Details for the file bitvector_for_humans-0.14.1-py3-none-any.whl.

File metadata

File hashes

Hashes for bitvector_for_humans-0.14.1-py3-none-any.whl
Algorithm Hash digest
SHA256 41bd430cc698f3712e36b32c1ac0d4420ceb230d8fdbf6c3489243f470457305
MD5 0aa1edf13f8e42d546ace8e6dc897a00
BLAKE2b-256 fbd2343bfccdc20e8e9c0e00fb8357befd42f177e52b421d389dbf7cdcbceb18

See more details on using hashes here.

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