initial commit
This commit is contained in:
commit
163fd5e290
|
@ -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.
|
|
@ -0,0 +1 @@
|
|||
from .main import Bits
|
|
@ -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)
|
||||
|
|
@ -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',
|
||||
)
|
|
@ -0,0 +1,7 @@
|
|||
import os
|
||||
import sys
|
||||
sys.path.insert(0, os.path.abspath(os.path.join(os.path.dirname(__file__),
|
||||
'..')))
|
||||
|
||||
import Bits
|
||||
|
|
@ -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"])
|
||||
|
Loading…
Reference in New Issue