From d111d402db0e9442dd64df243e595a5a02c50954 Mon Sep 17 00:00:00 2001 From: S Groesz Date: Wed, 14 Apr 2021 00:59:02 +0000 Subject: [PATCH] compatibility update for Python 3.6 --- publish.sh | 10 ++++++---- src/bits.py | 19 +++++++++++++++++-- tests/test_bytes.py | 32 ++++++++++++++++++++++++++++++-- 3 files changed, 53 insertions(+), 8 deletions(-) diff --git a/publish.sh b/publish.sh index 4b79f4c..d95cea3 100755 --- a/publish.sh +++ b/publish.sh @@ -8,22 +8,24 @@ username="__token__" test_token=`cat test.token` pypi_token=`cat pypi.token` -#------------------------------------------------------------------------------------------------------- +#-------------------------------------------------------------------------------------------------- # Handle command line arguments -#------------------------------------------------------------------------------------------------------- +#-------------------------------------------------------------------------------------------------- _USERNAME="__token__" _TOKEN="" _TESTREPO=$_FALSE PTYPE="test" -#--- FUNCTION ---------------------------------------------------------------------------------------- +#--- FUNCTION ----------------------------------------------------------------------------------- # NAME: __usage # DESCRIPTION: Display usage information. -#------------------------------------------------------------------------------------------------------- +#-------------------------------------------------------------------------------------------------- __usage() { cat << EOT Usage : ${__ScriptName} [options] + Options: + -h Display this help Publish types: - pypi Publish to pypi - test Publish to test.pypi.org diff --git a/src/bits.py b/src/bits.py index a719129..1203e20 100644 --- a/src/bits.py +++ b/src/bits.py @@ -933,6 +933,8 @@ class Bytes: A colletion of Bits with convenient properties for working with binary data """ + import sys + def __init__(self, var=None, byteorder="big"): """ var: a supported variant (object) @@ -1135,10 +1137,23 @@ class Bytes: """ Return the hex-string representation of self """ + hexvalue = bytes(self).hex() if sep is None or bytes_per_sep is None or bytes_per_sep == 0: - return bytes(self).hex() + return hexvalue else: - return bytes(self).hex(sep=sep, bytes_per_sep=bytes_per_sep) + if bytes_per_sep > 1: + hexvalue = hexvalue[::-1] # reverse the hex string + sep = sep[::-1] + i = 0 + retvalue = "" + while i < len(hexvalue): + if i > 0: + retvalue = retvalue + sep + retvalue = retvalue + hexvalue[i:(abs(bytes_per_sep * 2) + i)] + i += abs(bytes_per_sep * 2) + if bytes_per_sep > 1: + retvalue = retvalue[::-1] + return retvalue @property def bytes(self): diff --git a/tests/test_bytes.py b/tests/test_bytes.py index a54adad..2112bf1 100644 --- a/tests/test_bytes.py +++ b/tests/test_bytes.py @@ -19,6 +19,8 @@ class TestBytes(TestCase): test_list_of_ints = [] test_list_of_str = [] for i in range(0, 100): + # Generate 100 random test cases, each with max_value between + # 9 and 9,999,999,999,999,999,999,999,999 (25 9's) max_value = int("9" * randint(1, 25)) test_value = randint(1, max_value) bitesize = ceil(test_value.bit_length() / 8) @@ -74,7 +76,8 @@ class TestBytes(TestCase): # print(f"\t\t{testcase}") try: self.assertIsInstance(eval(test), eval(compare), - f"{test} is instance of {compare}") + "{test} is instance of {compare}" + ) except: import pdb pdb.set_trace() @@ -95,13 +98,38 @@ class TestBytes(TestCase): """ Test conversion to hex string """ - with self.subTest("Test hex() function"): + with self.subTest("Test Bytes.hex() using random values"): + for testcase in self.testcases["bytes"]: + self.assertEqual(Bytes(testcase).hex(), testcase.hex(), + f"Bytes({testcase}).hex() == {testcase}.hex()" + ) + with self.subTest("Test hex() function with known values"): self.assertEqual(Bytes(1234).hex(), "04d2", f"Bytes(1234).hex() == '04d2'") self.assertEqual(Bytes(1234).hex(":"), "04:d2", f"Bytes(1234).hex(':') == '04:d2'") self.assertEqual(Bytes(1234).hex(":", 1), "04:d2", f"Bytes(1234).hex(':', 1) == '04:d2'") + with self.subTest("Advanced hex() test"): + testcase = b'UUDDLRLRAB' + self.assertEqual(Bytes(testcase).hex(), testcase.hex(), + f"Bytes({testcase}).hex() == {testcase}.hex()") + tests = [['55:55:44:44:4c:52:4c:52:41:42', ":", 1], + ['55:55:44:44:4c:52:4c:52:41:42', ":", -1], + ['5555:44444c52:4c524142', ":", 4], + ['55554444:4c524c52:4142', ":", -4], + ['55 - 55 - 44 - 44 - 4c - 52 - 4c - 52 - 41 - 42', " - ", + 1], + ['55, 55, 44, 44, 4c, 52, 4c, 52, 41, 42', ", ", 1], + ['5555, 44444c52, 4c524142', ", ", 4], + ['55554444, 4c524c52, 4142', ", ", -4] + ] + for subt in tests: + self.assertEqual(Bytes(testcase).hex(sep=subt[1], + bytes_per_sep=subt[2]), + subt[0], + f"Bytes({testcase}).hex(sep='{subt[1]}', " + + f"bytes_per_sep={subt[2]}) == {subt[0]}") def test_comparison_operators(self): """