commit 163fd5e290309855ec65ad4a0a24477a2ae33211 Author: S Groesz Date: Sun Oct 4 01:35:46 2020 +0000 initial commit diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..5db7ba3 --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2020 SGroesz + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..b6ac0bf --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# Bits + +Helps you manage your bits! diff --git a/bits/__init__.py b/bits/__init__.py new file mode 100644 index 0000000..8b5eac5 --- /dev/null +++ b/bits/__init__.py @@ -0,0 +1 @@ +from .main import Bits diff --git a/bits/main.py b/bits/main.py new file mode 100644 index 0000000..e126245 --- /dev/null +++ b/bits/main.py @@ -0,0 +1,45 @@ +"""The Bits class makes bit level operations easier to write. +(c) 2020 S Groesz +""" + +class Bits: + def __init__(self, data=None): + if data is None: + self.__raw = b'' + else: + self.__raw = bytes(data) + + def __bytes__(self): + return self.__raw + + def __int__(self): + #return int.from_bytes(bytes(self), "big") # alternative Py > 3.1 + i = 0 + for b in bytes(self): + i = i * 256 + int(b) # Python 2: int(b) -> ord(b) + return i + + def __hash__(self): + return hash(bytes(self)) + + def __str__(self): + return "{:08b}".format(int(self)) + + def __eq__(self, compare): + return hash(self) == hash(compare) + + def __ne__(self, compare): + return hash(self) != hash(compare) + + def __lt__(self, compare): + return int(self) < int(compare) + + def __le__(self, compare): + return int(self) <= int(compare) + + def __gt__(self, compare): + return int(self) > int(compare) + + def __ge__(self, compare): + return int(self) >= int(compare) + diff --git a/setup.py b/setup.py new file mode 100644 index 0000000..fc5bb8e --- /dev/null +++ b/setup.py @@ -0,0 +1,21 @@ +import setuptools + +with open("README.md", "r") as fh: + long_description = fh.read() + +setuptools.setup( + name="bits", + version="0.0.1", + author="S Groesz", + description="Make your bits easier to handle", + long_description=long_description, + long_description_content_type="text/markdown", + url="https://github.com/wolfpackmars2/bits", + packages=setuptools.find_packages(), + classifiers=[ + "Programming Language :: Python :: 3", + "License :: OSI Approved "" MIT License", + "Operating System :: OS Independent", + ], + python_requires='>=3.6', +) diff --git a/tests/context.py b/tests/context.py new file mode 100644 index 0000000..74f796d --- /dev/null +++ b/tests/context.py @@ -0,0 +1,7 @@ +import os +import sys +sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__), + '..'))) + +import Bits + diff --git a/tests/test_bits.py b/tests/test_bits.py new file mode 100644 index 0000000..0549459 --- /dev/null +++ b/tests/test_bits.py @@ -0,0 +1,76 @@ +from unittest import TestCase + +import .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.testObjects = [ + {"hash"=3156473132868910681, + "data"=b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00', + "databits"="01011010" + \ + "00000000" + \ + "10100010" + \ + "11010101" + \ + "11111111" + \ + "11111111" + \ + "11111111" + \ + "11111111" + \ + "11111111" + \ + "11111111" + \ + "00000000" + \ + "00000000", + "data_int"=27854419854894512841598894080, + "bitsObject"=Bits( + b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00' + ) + } + ] + + def test_bytes(self): + # b = bytes(self.bitsObject) + # self.assertTrue(isinstance(b, bytes)) + #self.assertEquals(b, + # b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00', + # "Bytes object returned is :" + b) + for testcase in self.testObjects: + b = bytes(testcase["bitsObject"]) + self.assertTrue(isinstance(b, bytes)) + self.assertEquals(b, testcase["data"]) + + def test_int(self): + #i = int(thebits.bits(b'Z\x00\xa2\xd5\xff\xff\xff\xff\xff\xff\x00\x00')) + #i = int(self.bitsObject) + #self.assertTrue(isinstance(i, int)) + #self.assertEqual(i, + # 27854419854894512841598894080, + # "Type is " + type(i) + # ) + for testcase in self.testObjects: + i = int(testcase["bitsObject"]) + self.assertTrue(isinstance(i, int)) + self.assertEquals(i, testcase(["data_int"])) + + def test_str(self): + #s = str(self.bitsObject) + #self.assertTrue(isinstance(s, str)) + #self.assertEqual(s, + # "10110100000000010100010110101011111111111111111111111111111111111111111111111110000000000000000", + # "String returned is " + s) + for testcase in self.TestObjects: + s = str(testcase["bitsObject"]) + self.assertTrue(isinstance(s, str)) + self.assertEquals(s, testcase(["test_str"])) + + def test_hash(self): + #h = hash(self.bitsObject) + #self.assertTrue(isinstance(h, int)) + #self.assertEqual(h, + # 3156473132868910681, + # "Hashed value is " + str(h)) + for testcase in self.TestObjects: + h = hash(testcase["bitsObject"]) + self.assertTrue(isinstance(h, int)) + self.assertEquals(h, testcase["hash"]) +