def strip(a, mode='right'): """strip(bitarray, mode='right', /) -> bitarray Strip zeros from left, right or both ends. Allowed values for mode are the strings: `left`, `right`, `both` """ if not isinstance(a, bitarray): raise TypeError("bitarray expected, got '%s'" % type(a).__name__) if not isinstance(mode, str): raise TypeError("str expected for mode") if mode not in ('left', 'right', 'both'): raise ValueError("allowed values 'left', 'right', 'both', got: %r" % mode) first = 0 if mode in ('left', 'both'): try: first = a.index(1) except ValueError: return bitarray(0, a.endian()) last = len(a) - 1 if mode in ('right', 'both'): try: last = rindex(a) except ValueError: return bitarray(0, a.endian()) return a[first:last + 1]
def strip(a, mode='right'): """strip(bitarray, mode='right', /) -> bitarray Return a new bitarray with zeros stripped from left, right or both ends. Allowed values for mode are the strings: `left`, `right`, `both` """ if not isinstance(a, bitarray): raise TypeError("bitarray expected, got '%s'" % type(a).__name__) if not isinstance(mode, str): raise TypeError("str expected for mode, got '%s'" % type(a).__name__) if mode not in ('left', 'right', 'both'): raise ValueError("mode must be 'left', 'right' or 'both', got: %r" % mode) first = 0 if mode in ('left', 'both'): try: first = a.index(1) except ValueError: return a[:0] last = len(a) - 1 if mode in ('right', 'both'): try: last = rindex(a) except ValueError: return a[:0] return a[first:last + 1]
def int2ba(i, length=None, endian='big'): """int2ba(int, /, length=None, endian='big') -> bitarray Convert the given integer into a bitarray (with given endianness, and no leading (big-endian) / trailing (little-endian) zeros). If length is provided, the result will be of this length, and an `OverflowError` will be raised, if the integer cannot be represented within length bits. """ if not isinstance(i, (int, long) if _is_py2 else int): raise TypeError("integer expected") if i < 0: raise ValueError("non-negative integer expected") if length is not None: if not isinstance(length, int): raise TypeError("integer expected for length") if length <= 0: raise ValueError("integer larger than 0 expected for length") if not isinstance(endian, str): raise TypeError("string expected for endian") if endian not in ('big', 'little'): raise ValueError("endian can only be 'big' or 'little'") if i == 0: # there a special cases for 0 which we'd rather not deal with below return zeros(length or 1, endian=endian) big_endian = bool(endian == 'big') if _is_py2: c = bytearray() while i: i, r = divmod(i, 256) c.append(r) if big_endian: c.reverse() b = bytes(c) else: # py3 b = i.to_bytes(bits2bytes(i.bit_length()), byteorder=endian) a = bitarray(endian=endian) a.frombytes(b) la = a.length() if la == length: return a if length is None: return strip(a, 'left' if big_endian else 'right') if la > length: size = (la - a.index(1)) if big_endian else (rindex(a) + 1) if size > length: raise OverflowError("cannot represent %d bit integer in " "%d bits" % (size, length)) a = a[la - length:] if big_endian else a[:length - la] if la < length: if big_endian: a = zeros(length - la, 'big') + a else: a += zeros(length - la, 'little') assert a.length() == length return a