Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def __setitem__(self, index, value):
if isinstance(value, BitSequence):
if issubclass(value.__class__, self.__class__) and \
value.__class__ != self.__class__:
raise BitSequenceError("Cannot set item with instance of a "
"subclass")
if isinstance(index, slice):
value = self.__class__(value, length=len(self._seq[index]))
self._seq[index] = value.sequence()
else:
if not isinstance(value, BitSequence):
value = self.__class__(value)
val = value.tobit()
if index > len(self._seq):
raise BitSequenceError("Cannot change the sequence size")
self._seq[index] = val
def tobyte(self, msb=False):
raise BitSequenceError("Type %s cannot be converted to byte" %
type(self))
def tobyte(self, msb=False):
"""Convert the sequence into a single byte value, if possible"""
if len(self) > 8:
raise BitSequenceError("Cannot fit into a single byte")
byte = 0
pos = not msb and -1 or 0
# copy the sequence
seq = self._seq[:]
while seq:
byte <<= 1
byte |= seq.pop(pos)
return byte
def __and__(self, other):
if not isinstance(other, self.__class__):
raise BitSequenceError('Need a BitSequence to combine')
if len(self) != len(other):
raise BitSequenceError('Sequences must be the same size')
return self.__class__(value=list(map(lambda x, y: x and y,
self._seq, other.sequence())))
def tobytes(self, msb=False, msby=False):
raise BitSequenceError("Type %s cannot be converted to bytes" %
type(self))
def __init__(self, value=None, msb=False, length=0, bytes_=None,
msby=True):
"""Instanciate a new bit sequence.
"""
self._seq = array('B')
seq = self._seq
if value and bytes_:
raise BitSequenceError("Cannot inialize with both a value and "
"bytes")
if bytes_:
provider = msby and list(bytes_).__iter__() or reversed(bytes_)
for byte in provider:
if isinstance(byte, str):
byte = ord(byte)
elif byte > 0xff:
raise BitSequenceError("Invalid byte value")
b = []
for x in range(8):
b.append(bool(byte & 0x1))
byte >>= 1
if msb:
b.reverse()
seq.extend(b)
else:
value = self._tomutable(value)
if isinstance(value, int):
self._init_from_integer(value, msb, length)
elif isinstance(value, BitSequence):
self._init_from_sibling(value, msb)
elif is_iterable(value):
self._init_from_iterable(value, msb)
elif value is None:
def matches(self, other):
if not isinstance(self, BitSequence):
raise BitSequenceError('Not a BitSequence instance')
# the bit sequence should be of the same length
ld = len(self) - len(other)
if ld:
return ld
for (x, y) in zip(self._seq, other.sequence()):
if BitZSequence.Z in (x, y):
continue
if x is not y:
return False
return True
def __or__(self, other):
if not isinstance(self, BitSequence):
raise BitSequenceError('Need a BitSequence-compliant object to '
'combine')
if len(self) != len(other):
raise BitSequenceError('Sequences must be the same size')
def orz(x, y):
"""Compute the boolean OR operation for a tri-state boolean"""
if BitZSequence.Z in (x, y):
return BitZSequence.Z
else:
return x or y
return self.__class__(value=list(map(orz, self._seq,
other.sequence())))
def __or__(self, other):
if not isinstance(self, BitSequence):
raise BitSequenceError('Need a BitSequence-compliant object to '
'combine')
if len(self) != len(other):
raise BitSequenceError('Sequences must be the same size')
def orz(x, y):
"""Compute the boolean OR operation for a tri-state boolean"""
if BitZSequence.Z in (x, y):
return BitZSequence.Z
else:
return x or y
return self.__class__(value=list(map(orz, self._seq,
other.sequence())))