compatibility update for Python 3.6

This commit is contained in:
S Groesz 2021-04-14 00:59:02 +00:00
parent c63d2dd4bb
commit d111d402db
3 changed files with 53 additions and 8 deletions

View File

@ -8,22 +8,24 @@ username="__token__"
test_token=`cat test.token` test_token=`cat test.token`
pypi_token=`cat pypi.token` pypi_token=`cat pypi.token`
#------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
# Handle command line arguments # Handle command line arguments
#------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
_USERNAME="__token__" _USERNAME="__token__"
_TOKEN="" _TOKEN=""
_TESTREPO=$_FALSE _TESTREPO=$_FALSE
PTYPE="test" PTYPE="test"
#--- FUNCTION ---------------------------------------------------------------------------------------- #--- FUNCTION -----------------------------------------------------------------------------------
# NAME: __usage # NAME: __usage
# DESCRIPTION: Display usage information. # DESCRIPTION: Display usage information.
#------------------------------------------------------------------------------------------------------- #--------------------------------------------------------------------------------------------------
__usage() { __usage() {
cat << EOT cat << EOT
Usage : ${__ScriptName} [options] <publish-type> Usage : ${__ScriptName} [options] <publish-type>
Options:
-h Display this help
Publish types: Publish types:
- pypi Publish to pypi - pypi Publish to pypi
- test Publish to test.pypi.org - test Publish to test.pypi.org

View File

@ -933,6 +933,8 @@ class Bytes:
A colletion of Bits with convenient properties for working with binary data A colletion of Bits with convenient properties for working with binary data
""" """
import sys
def __init__(self, var=None, byteorder="big"): def __init__(self, var=None, byteorder="big"):
""" """
var: a supported variant (object) var: a supported variant (object)
@ -1135,10 +1137,23 @@ class Bytes:
""" """
Return the hex-string representation of self 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: if sep is None or bytes_per_sep is None or bytes_per_sep == 0:
return bytes(self).hex() return hexvalue
else: 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 @property
def bytes(self): def bytes(self):

View File

@ -19,6 +19,8 @@ class TestBytes(TestCase):
test_list_of_ints = [] test_list_of_ints = []
test_list_of_str = [] test_list_of_str = []
for i in range(0, 100): 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)) 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)
@ -74,7 +76,8 @@ class TestBytes(TestCase):
# print(f"\t\t{testcase}") # print(f"\t\t{testcase}")
try: try:
self.assertIsInstance(eval(test), eval(compare), self.assertIsInstance(eval(test), eval(compare),
f"{test} is instance of {compare}") "{test} is instance of {compare}"
)
except: except:
import pdb import pdb
pdb.set_trace() pdb.set_trace()
@ -95,13 +98,38 @@ class TestBytes(TestCase):
""" """
Test conversion to hex string 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", self.assertEqual(Bytes(1234).hex(), "04d2",
f"Bytes(1234).hex() == '04d2'") f"Bytes(1234).hex() == '04d2'")
self.assertEqual(Bytes(1234).hex(":"), "04:d2", self.assertEqual(Bytes(1234).hex(":"), "04:d2",
f"Bytes(1234).hex(':') == '04:d2'") f"Bytes(1234).hex(':') == '04:d2'")
self.assertEqual(Bytes(1234).hex(":", 1), "04:d2", self.assertEqual(Bytes(1234).hex(":", 1), "04:d2",
f"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): def test_comparison_operators(self):
""" """