workon Bytes

This commit is contained in:
S Groesz 2020-10-15 04:07:34 +00:00
parent f5f6813326
commit 535ef1d42b
2 changed files with 50 additions and 7 deletions

View File

@ -361,10 +361,18 @@ class Bits:
return ret return ret
class Bytes(bytearray): class Bytes:
"""
A colletion of Bits with convenient properties for working with binary data
"""
def __init__(self, b, *args, **kwargs): def __init__(self, var=None, byteorder="big"):
self.__raw = bytearray(b) self.__raw = bytearray(b'')
self.__small = False
if byteorder.lower() in ["small", "little"]:
self.__small = True
if var is not None:
self.__raw = self.__to_bytearray(var)
def __bytes__(self): def __bytes__(self):
return bytes(self.__raw) return bytes(self.__raw)
@ -433,6 +441,40 @@ class Bytes(bytearray):
def __str__(self): def __str__(self):
return self.bin() return self.bin()
def __to_bytearray(self, var):
retvalue = bytearray(b'')
byteorder="little" if self.__small else "big"
if isinstance(var, int):
retvalue = bytearray(self.from_int(var, byteorder))
elif isinstance(var, bytes):
retvalue = bytearray(var)
elif isinstance(var, str):
if len(var) % 8 > 0:
var = ("0" * (8 - (len(var) % 8))) + var
bitelist = []
for i in range(0, len(var), 8):
bitelist.append(Bits(var[i:i+8]))
retvalue = bytearray(bitelist)
elif isinstance(var, bytearray):
retvalue = var
elif isinstance(var, Bytes):
retvalue = bytearray(var)
elif isinstance(var, list):
# test first item in list to see if it is Bit
if len(var) > 0 and isinstance(var[0], (bool, Bit)):
bitstring = ""
for bit in var:
bitstring = bitstring + str(Bit(bit))
retvalue += bytearray(Bytes(bitstring))
else:
for item in var:
if isinstance(item, (int, Bits, Bytes)):
retvalue += bytearray(self.from_int(int(item),
byteorder))
elif isinstance(item, bytes):
retvalue += bytearray(item)
return retvalue
def int(self, _bytes=None, byteorder="big", signed=False): def int(self, _bytes=None, byteorder="big", signed=False):
if _bytes is None: if _bytes is None:
_bytes = bytes(self) _bytes = bytes(self)

View File

@ -19,7 +19,7 @@ class TestBytes(TestCase):
test_list_of_ints = [[0]] test_list_of_ints = [[0]]
test_list_of_str = [["0"] * 8] test_list_of_str = [["0"] * 8]
for i in range(0, 100): for i in range(0, 100):
max_value = int("9" * randint(0, 25)) max_value = int("9" * randint(1, 25))
test_value = randint(1, max_value) test_value = randint(1, max_value)
bitesize = ceil(test_value.bit_length() / 8) bitesize = ceil(test_value.bit_length() / 8)
pad_bits = "0" * ((bitesize * 8) - test_value.bit_length()) pad_bits = "0" * ((bitesize * 8) - test_value.bit_length())
@ -35,8 +35,8 @@ class TestBytes(TestCase):
list_of_str = [] list_of_str = []
for bite in bites: for bite in bites:
list_of_Bits.append(Bits(bite)) list_of_Bits.append(Bits(bite))
list_of_bytes.append(bite) list_of_bytes.append(bytes([bite]))
list_of_int.append(ord(bite)) list_of_ints.append(bite)
list_of_str.append(Bits(bite).bin()) list_of_str.append(Bits(bite).bin())
for bit in Bits(bite).bin(): for bit in Bits(bite).bin():
list_of_Bit.append(Bit(bit)) list_of_Bit.append(Bit(bit))
@ -45,6 +45,7 @@ class TestBytes(TestCase):
test_list_of_bytes.append(list_of_bytes) test_list_of_bytes.append(list_of_bytes)
test_list_of_Bits.append(list_of_Bits) test_list_of_Bits.append(list_of_Bits)
test_list_of_Bit.append(list_of_Bit) test_list_of_Bit.append(list_of_Bit)
test_list_of_ints.append(list_of_ints)
test_list_of_str.append(list_of_str) test_list_of_str.append(list_of_str)
self.testcases = {"int": test_ints, self.testcases = {"int": test_ints,
"str": test_str, "str": test_str,
@ -86,7 +87,7 @@ class TestBytes(TestCase):
Test the comparison operators with Bytes objects Test the comparison operators with Bytes objects
""" """
with self.subTest("Bytes Type"): with self.subTest("Bytes Type"):
self.assertEqual(Bytes( pass
with self.subTest("int Type"): with self.subTest("int Type"):
self.assertEqual(Bytes(1234), 1234, self.assertEqual(Bytes(1234), 1234,
"Bytes(1234) == 1234") "Bytes(1234) == 1234")