6 Commits

Author SHA1 Message Date
741fc41f07 fix: Bits.__getitem__/__setitem__ ignore msb_last (rtl) as documented
list() always used MSB-first bit order regardless of the msb_last
constructor flag, so indexing/slicing on an instance built with
msb_last=True silently behaved identically to the default -- the
documented "swap bit order for indexing and slicing" never happened.
__setitem__ needed a matching fix: since __setvalue() always expects
a MSB-first list, a mutated LSB-first list from list() has to be
reversed back before being passed in.

Found while wiring bits.Bits into progpib's GPIB status-byte handling
(IEEE-488.2 bit numbering is LSB-first, e.g. RQS = bit 6).

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
2026-08-24 18:27:58 -05:00
49ef87f8c9 add bit_len to Bytes 2021-04-17 05:01:32 +00:00
ecc1b3bd8d update source code location 2021-04-14 23:34:23 +00:00
55ae3544d5 add dev notes 2021-04-14 03:27:53 +00:00
de7c42fe8f fix typo 2021-04-14 03:17:42 +00:00
34fa1b4502 update readme 2021-04-14 03:06:08 +00:00
4 changed files with 49 additions and 6 deletions

View File

@@ -8,6 +8,20 @@ Intended to be compatible with Python 3.6+. Written and tested against Python
If something doesn't work in Python 3.6, it's a bug. Please report any such
bugs if they are encountered.
# PyPI
View on PyPI at [https://pypi.org/project/binary-bits/](https://pypi.org/project/binary-bits/)
Install from PyPI:
python3 -m pip install binary-bits --upgrade
Use in your project:
```python
import bits
my_bytes = bits.Bytes(bytes.fromhex('deadbeef'))
```
# Similar projects
## [py-flags](https://pypi.org/project/py-flags/)

15
dev.md Normal file
View File

@@ -0,0 +1,15 @@
Steps to publish to PyPI:
1) run `make test`
2) update version in setup.py
3) Tag the current release in git to match the version in setup.py
git tag -a 1.2.0 -m 'Release 1.2.0'
4) commit any pending changes to git
git add .
git commit -m 'commit changes'
git push origin 1.2.0
5) test publish
./publish.sh test
6) publish to PyPI
./publish.sh pypi

View File

@@ -5,7 +5,7 @@ with open("README.md", "r") as fh:
setuptools.setup(
name="binary-bits",
version="1.1.0",
version="1.1.1",
author="S Groesz",
description="Provide additional methods for working with binary data",
long_description=long_description,
@@ -28,6 +28,6 @@ setuptools.setup(
'test': ['coverage'],
},
project_urls={
'Source': 'http://git.groesz.org/wp/bits/',
'Source': 'http://git.groesz.org/Groesz.org/bits/',
},
)

View File

@@ -605,6 +605,9 @@ class Bits:
l = self.list()
# str->int->bool->int : accept bool, str, int, return either "0" or "1"
l[index] = Bit(value)
if self.__r_to_l:
# list() returned LSB-first order; __setvalue expects MSB-first
l = l[::-1]
self.__setvalue(l)
def __get__(self, instance, owner):
@@ -911,10 +914,15 @@ class Bits:
"""
self.__r_to_l = bool(msb_last)
def list(self, pad=True, reverse=False):
def list(self, pad=True, reverse=None):
"""
Return self as list(of Bit)
reverse: True/False to force bit order for this call; None (the
default) uses self.rtl, so indexing/slicing on an instance
constructed with msb_last=True is LSB-first as documented.
"""
if reverse is None:
reverse = self.__r_to_l
ret = []
bits = self.bin(pad=pad, reverse=reverse)
for b in bits:
@@ -930,12 +938,12 @@ class Bits:
class Bytes:
"""
A colletion of Bits with convenient properties for working with binary data
A collection of Bits with convenience methods for working with binary data
"""
import sys
def __init__(self, var=None, byteorder="big"):
def __init__(self, var=None, byteorder="big", bits=None):
"""
var: a supported variant (object)
byteorder: notimplemented, mostly ignored
@@ -943,11 +951,17 @@ class Bytes:
self.__raw = bytearray(b'')
self.__small = False
self.__iter = None
self.__list_len = None
self.__bit_len = None
if byteorder.lower() in ["small", "little"]:
self.__small = True
if var is not None:
self.__raw = self.__to_bytearray(var)
if bits is None:
self.__bit_len = len(self.__raw)
else:
if not isinstance(bits, (int, Bytes, Bit)):
raise TypeError(f"bits argument must be int, not {type(bits)}")
self.__bit_len = int(bits)
def __bytes__(self):
"""