From 092e38733718d1327eeced0388d3841246413f96 Mon Sep 17 00:00:00 2001 From: S Groesz Date: Sun, 4 Oct 2020 18:11:46 +0000 Subject: [PATCH] implement repr dunder --- bits/main.py | 5 +++-- tests/context.py | 2 +- tests/test_bits.py | 28 ++++++++++++++-------------- 3 files changed, 18 insertions(+), 17 deletions(-) diff --git a/bits/main.py b/bits/main.py index e126245..5b1e7e3 100644 --- a/bits/main.py +++ b/bits/main.py @@ -22,8 +22,9 @@ class Bits: def __hash__(self): return hash(bytes(self)) - def __str__(self): - return "{:08b}".format(int(self)) + def __repr__(self): + return (f'{self.__class__.__name__}({bytes(self)})') + #return "{:08b}".format(int(self)) def __eq__(self, compare): return hash(self) == hash(compare) diff --git a/tests/context.py b/tests/context.py index 74f796d..8c1f6a1 100644 --- a/tests/context.py +++ b/tests/context.py @@ -3,5 +3,5 @@ import sys sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), '..'))) -import Bits +from bits import Bits diff --git a/tests/test_bits.py b/tests/test_bits.py index 0549459..16b0e98 100644 --- a/tests/test_bits.py +++ b/tests/test_bits.py @@ -1,15 +1,15 @@ from unittest import TestCase -import .context import Bits +from .context import Bits class TestInt(TestCase): def setUp(self): - self.bitsObject = - Bits(b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00') + self.bitsObject = \ + Bits(b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00') self.testObjects = [ - {"hash"=3156473132868910681, - "data"=b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00', - "databits"="01011010" + \ + {"hash": -3172158928563556465, + "data": b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00', + "databits": "1011010" + \ "00000000" + \ "10100010" + \ "11010101" + \ @@ -21,8 +21,8 @@ class TestInt(TestCase): "11111111" + \ "00000000" + \ "00000000", - "data_int"=27854419854894512841598894080, - "bitsObject"=Bits( + "data_int": 27854419854894512841598894080, + "bitsObject": Bits( 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: b = bytes(testcase["bitsObject"]) self.assertTrue(isinstance(b, bytes)) - self.assertEquals(b, testcase["data"]) + self.assertEqual(b, testcase["data"]) def test_int(self): #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: i = int(testcase["bitsObject"]) self.assertTrue(isinstance(i, int)) - self.assertEquals(i, testcase(["data_int"])) + self.assertEqual(i, testcase["data_int"]) def test_str(self): #s = str(self.bitsObject) @@ -58,10 +58,10 @@ class TestInt(TestCase): #self.assertEqual(s, # "10110100000000010100010110101011111111111111111111111111111111111111111111111110000000000000000", # "String returned is " + s) - for testcase in self.TestObjects: + for testcase in self.testObjects: s = str(testcase["bitsObject"]) self.assertTrue(isinstance(s, str)) - self.assertEquals(s, testcase(["test_str"])) + self.assertEqual(s, testcase["databits"]) def test_hash(self): #h = hash(self.bitsObject) @@ -69,8 +69,8 @@ class TestInt(TestCase): #self.assertEqual(h, # 3156473132868910681, # "Hashed value is " + str(h)) - for testcase in self.TestObjects: + for testcase in self.testObjects: h = hash(testcase["bitsObject"]) self.assertTrue(isinstance(h, int)) - self.assertEquals(h, testcase["hash"]) + self.assertEqual(h, testcase["hash"])