Compare commits
6 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| 741fc41f07 | |||
| 49ef87f8c9 | |||
| ecc1b3bd8d | |||
| 55ae3544d5 | |||
| de7c42fe8f | |||
| 34fa1b4502 |
14
README.md
14
README.md
@@ -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
15
dev.md
Normal 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
|
||||
|
||||
4
setup.py
4
setup.py
@@ -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/',
|
||||
},
|
||||
)
|
||||
|
||||
22
src/bits.py
22
src/bits.py
@@ -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):
|
||||
"""
|
||||
|
||||
Reference in New Issue
Block a user