Message257730
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. |
|
Date |
User |
Action |
Args |
2016-01-08 00:42:11 | abarnert | set | recipients:
+ abarnert, gvanrossum, georg.brandl, rhettinger, terry.reedy, pitrou, vstinner, christian.heimes, Ramchandra Apte, martin.panter, serhiy.storchaka, josh.r |
2016-01-08 00:42:11 | abarnert | set | messageid: <1452213731.3.0.984520347521.issue19251@psf.upfronthosting.co.za> |
2016-01-08 00:42:11 | abarnert | link | issue19251 messages |
2016-01-08 00:42:10 | abarnert | create | |
|