implement repr dunder

This commit is contained in:
S Groesz 2020-10-04 18:11:46 +00:00
parent 9edeed7f5c
commit 092e387337
3 changed files with 18 additions and 17 deletions

View File

@ -22,8 +22,9 @@ class Bits:
def __hash__(self): def __hash__(self):
return hash(bytes(self)) return hash(bytes(self))
def __str__(self): def __repr__(self):
return "{:08b}".format(int(self)) return (f'{self.__class__.__name__}({bytes(self)})')
#return "{:08b}".format(int(self))
def __eq__(self, compare): def __eq__(self, compare):
return hash(self) == hash(compare) return hash(self) == hash(compare)

View File

@ -3,5 +3,5 @@ import sys
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
'..'))) '..')))
import Bits from bits import Bits

View File

@ -1,15 +1,15 @@
from unittest import TestCase from unittest import TestCase
import .context import Bits from .context import Bits
class TestInt(TestCase): class TestInt(TestCase):
def setUp(self): def setUp(self):
self.bitsObject = self.bitsObject = \
Bits(b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00') Bits(b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00')
self.testObjects = [ self.testObjects = [
{"hash"=3156473132868910681, {"hash": -3172158928563556465,
"data"=b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00', "data": b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00',
"databits"="01011010" + \ "databits": "1011010" + \
"00000000" + \ "00000000" + \
"10100010" + \ "10100010" + \
"11010101" + \ "11010101" + \
@ -21,8 +21,8 @@ class TestInt(TestCase):
"11111111" + \ "11111111" + \
"00000000" + \ "00000000" + \
"00000000", "00000000",
"data_int"=27854419854894512841598894080, "data_int": 27854419854894512841598894080,
"bitsObject"=Bits( "bitsObject": Bits(
b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00' b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00'
) )
} }
@ -37,7 +37,7 @@ class TestInt(TestCase):
for testcase in self.testObjects: for testcase in self.testObjects:
b = bytes(testcase["bitsObject"]) b = bytes(testcase["bitsObject"])
self.assertTrue(isinstance(b, bytes)) self.assertTrue(isinstance(b, bytes))
self.assertEquals(b, testcase["data"]) self.assertEqual(b, testcase["data"])
def test_int(self): def test_int(self):
#i = int(thebits.bits(b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00')) #i = int(thebits.bits(b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00'))
@ -50,7 +50,7 @@ class TestInt(TestCase):
for testcase in self.testObjects: for testcase in self.testObjects:
i = int(testcase["bitsObject"]) i = int(testcase["bitsObject"])
self.assertTrue(isinstance(i, int)) self.assertTrue(isinstance(i, int))
self.assertEquals(i, testcase(["data_int"])) self.assertEqual(i, testcase["data_int"])
def test_str(self): def test_str(self):
#s = str(self.bitsObject) #s = str(self.bitsObject)
@ -58,10 +58,10 @@ class TestInt(TestCase):
#self.assertEqual(s, #self.assertEqual(s,
# "10110100000000010100010110101011111111111111111111111111111111111111111111111110000000000000000", # "10110100000000010100010110101011111111111111111111111111111111111111111111111110000000000000000",
# "String returned is " + s) # "String returned is " + s)
for testcase in self.TestObjects: for testcase in self.testObjects:
s = str(testcase["bitsObject"]) s = str(testcase["bitsObject"])
self.assertTrue(isinstance(s, str)) self.assertTrue(isinstance(s, str))
self.assertEquals(s, testcase(["test_str"])) self.assertEqual(s, testcase["databits"])
def test_hash(self): def test_hash(self):
#h = hash(self.bitsObject) #h = hash(self.bitsObject)
@ -69,8 +69,8 @@ class TestInt(TestCase):
#self.assertEqual(h, #self.assertEqual(h,
# 3156473132868910681, # 3156473132868910681,
# "Hashed value is " + str(h)) # "Hashed value is " + str(h))
for testcase in self.TestObjects: for testcase in self.testObjects:
h = hash(testcase["bitsObject"]) h = hash(testcase["bitsObject"])
self.assertTrue(isinstance(h, int)) self.assertTrue(isinstance(h, int))
self.assertEquals(h, testcase["hash"]) self.assertEqual(h, testcase["hash"])