nneonneo / fixedint Goto Github PK
View Code? Open in Web Editor NEWFixed-width integers for Python
License: Other
Fixed-width integers for Python
License: Other
Hey, I've gone through the fixedint codebase and I've recently come up with a (hopefully) refactored version. I think it is more straightforward, and I'd appreciate it if you could spend some time looking at the (very small) codebase.
It looks like there's something not quite right with the way maxval is accessed that leads to help(x), where x is a fixedint, raising an exception.
steve% python3
Python 3.7.3 (default, Mar 27 2019, 09:23:39)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> from fixedint import *
>>> help(FixedInt(4)(3))
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/_sitebuiltins.py", line 103, in __call__
return pydoc.help(*args, **kwds)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pydoc.py", line 1893, in __call__
self.help(request)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pydoc.py", line 1952, in help
else: doc(request, 'Help on %s:', output=self._output)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pydoc.py", line 1672, in doc
pager(render_doc(thing, title, forceload))
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pydoc.py", line 1665, in render_doc
return title % desc + '\n\n' + renderer.document(object, name)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pydoc.py", line 386, in document
if inspect.isclass(object): return self.docclass(*args)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pydoc.py", line 1311, in docclass
for name, kind, cls, value in classify_class_attrs(object)
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/pydoc.py", line 209, in classify_class_attrs
for (name, kind, cls, value) in inspect.classify_class_attrs(object):
File "/usr/local/Cellar/python/3.7.3/Frameworks/Python.framework/Versions/3.7/lib/python3.7/inspect.py", line 434, in classify_class_attrs
srch_obj = getattr(srch_cls, name, None)
File "/usr/local/lib/python3.7/site-packages/fixedint/base.py", line 19, in __get__
prop = obj.__dict__[self.name]
KeyError: 'maxval'
In my copy, installed using 'pip3 install fixedint' line 19 of base.py
relates to:
class FixedMetaProperty(object):
def __init__(self, name):
self.name = name
def __get__(self, obj, type=None):
prop = obj.__dict__[self.name] #<-- this line
return prop.__get__(obj)
def __set__(self, obj, value):
raise AttributeError("property %s is read-only" % self.name)
Hello @nneonneo, would it be possible to add an unsigned right shift operator? >>>
to the set of existing bit shifting operations? Thank you for your consideration!
It appears that assigning to a slice of a fixed-int object leads to bad behaviour. In the following example, the assignment of an 8-bit number into the bottom 8-bits of a 16-bit number causes the object to be corrupted.
>>> from fixedint import FixedInt
>>> f16 = FixedInt(16, signed=False, mutable=True)
>>> f8 = FixedInt(8, signed=False, mutable=True)
>>> x = f16( 0x0FFF )
>>> y = f8( 3 )
>>> bin(x)
'0b111111111111'
>>> bin(y)
'0b11'
>>> x[0:8] = int(y)
>>> x
MutableUInt16(3843)
>>> bin(x)
'0b111100000011'
>>> x[0:8] = y
>>> x
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python3.7/site-packages/fixedint/base.py", line 161, in __repr__
return '%s(%s)' % (type(self).__name__, self)
File "/usr/local/lib/python3.7/site-packages/fixedint/base.py", line 165, in __str__
return str(int(self))
File "/usr/local/lib/python3.7/site-packages/fixedint/base.py", line 407, in _f
return intfunc(self._val)
TypeError: descriptor '__int__' requires a 'int' object but received a 'MutableUInt8'
And in this example, we assign using a fixed-int that has been generated through a slice. This avoids the corruption but clears the bits outside the intended range.
>>> from fixedint import FixedInt
>>> f95 = FixedInt(95, signed=False, mutable=True)
>>> f40 = FixedInt(40, signed=False, mutable=True)
>>> x = f95( 0x0FFFFFFFFFFFFFFF )
>>> y = f40( 0xF )[0:40]
>>> bin(x)
'0b111111111111111111111111111111111111111111111111111111111111'
>>> bin(y)
'0b1111'
>>> x[0:40] = int(y)
>>> bin(x)
'0b111111111111111111110000000000000000000000000000000000001111'
>>> x[0:40] = y
>>> bin(x)
'0b1111'
>>>
I did this test on a Apple Mac running
steve% python3
Python 3.7.6 (default, Dec 30 2019, 19:38:36)
[Clang 10.0.0 (clang-1000.11.45.5)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
current behavior:
>>> fixedint.Int32(-2) // 3
Int32(-1)
desired behavior:
>>> fixedint.TruncDivInt32(-2) // 3
TruncDivInt32(0)
@nneonneo I was trying to port some bit-shifting Java code over to Python and found out (the hard way) that Python does not have fixed width integers like Java. Your package saved me a tonne of time and, from one open source creator to another, I just wanted to drop you a note to say "Thank You" and have a wonderful day!
[Please feel free to close this]
Hello, pyright
is giving me errors about unknown types when I try importing the aliases such as UInt8
and UInt16
. Would it be possible to add proper type hint support for these?
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.