Andrew Barnert added the comment:
For what it's worth, I looked at some old code (a clean re-implementation of a
crypto algorithm in Python, used as a unit test for production code in C++) and
found this:
class BytesBits(bytes):
# from a quick test, 1.12us in NumPy, 1.39us in C, 2.55us this way, 46.1us
with bytes(genexpr), so we don't need numpy
def _bitwise(self, other, op):
iself = int.from_bytes(self, 'little')
iother = int.from_bytes(other, 'little')
b = op(iself, iother)
return b.to_bytes(len(self), 'little')
def __or__(self, other):
return self._bitwise(other, int.__or__)
__ror__ = __or__
def __and__(self, other):
return self._bitwise(other, int.__and__)
__rand__ = __and__
def __xor__(self, other):
return self._bitwise(other, int.__xor__)
__rxor__ = __xor__
It doesn't do as much error checking as you want, but it was good enough for my
purposes.
At any rate, the fact that it's trivial to wrap this up yourself (and even more
so if you just write functions called band/bor/bxor instead of wrapping them up
in a subclass) implies to me that, if it's not used all that often, it doesn't
need to be on the builtin types.
----------
nosy: +abarnert
_______________________________________
Python tracker <[email protected]>
<http://bugs.python.org/issue19251>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe:
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com