Belle II Software  release-08-01-10
ConstBitStream Class Reference
Inheritance diagram for ConstBitStream:
Collaboration diagram for ConstBitStream:

Public Member Functions

def __init__ (self, auto=None, length=None, offset=None, **kwargs)
 
def __new__ (cls, auto=None, length=None, offset=None, **kwargs)
 
def __copy__ (self)
 
def __add__ (self, bs)
 
def read (self, fmt)
 
def readlist (self, fmt, **kwargs)
 
def readto (self, bs, bytealigned=None)
 
def peek (self, fmt)
 
def peeklist (self, fmt, **kwargs)
 
def bytealign (self)
 
def __new__ (cls, auto=None, length=None, offset=None, _cache=None, **kwargs)
 
def __lt__ (self, other)
 
def __gt__ (self, other)
 
def __le__ (self, other)
 
def __ge__ (self, other)
 
def __radd__ (self, bs)
 
def __getitem__ (self, key)
 
def __len__ (self)
 
def __str__ (self)
 
def __repr__ (self)
 
def __eq__ (self, bs)
 
def __ne__ (self, bs)
 
def __invert__ (self)
 
def __lshift__ (self, n)
 
def __rshift__ (self, n)
 
def __mul__ (self, n)
 
def __rmul__ (self, n)
 
def __and__ (self, bs)
 
def __rand__ (self, bs)
 
def __or__ (self, bs)
 
def __ror__ (self, bs)
 
def __xor__ (self, bs)
 
def __rxor__ (self, bs)
 
def __contains__ (self, bs)
 
def __hash__ (self)
 
def __nonzero__ (self)
 
def unpack (self, fmt, **kwargs)
 
def find (self, bs, start=None, end=None, bytealigned=None)
 
def findall (self, bs, start=None, end=None, count=None, bytealigned=None)
 
def rfind (self, bs, start=None, end=None, bytealigned=None)
 
def cut (self, bits, start=None, end=None, count=None)
 
def split (self, delimiter, start=None, end=None, count=None, bytealigned=None)
 
def join (self, sequence)
 
def tobytes (self)
 
def tofile (self, f)
 
def startswith (self, prefix, start=None, end=None)
 
def endswith (self, suffix, start=None, end=None)
 
def all (self, value, pos=None)
 
def any (self, value, pos=None)
 
def count (self, value)
 

Properties

 pos
 
 bitpos
 
 bytepos
 
 _offset = property(_getoffset)
 
 len
 
 length
 
 bool
 
 hex
 
 bin
 
 oct
 
 bytes
 
 int
 
 uint
 
 float
 
 intbe
 
 uintbe
 
 floatbe
 
 intle
 
 uintle
 
 floatle
 
 intne
 
 uintne
 
 floatne
 
 ue
 
 se
 
 uie
 
 sie
 

Private Member Functions

def _setbytepos (self, bytepos)
 
def _getbytepos (self)
 
def _setbitpos (self, pos)
 
def _getbitpos (self)
 
def _clear (self)
 
def _initialise (self, auto, length, offset, **kwargs)
 
def _initialise_from_auto (self, auto, length, offset)
 
def _assertsanity (self)
 
def _init_with_token (cls, name, token_length, value)
 
def _setauto (self, s, length, offset)
 
def _setfile (self, filename, length, offset)
 
def _setbytes_safe (self, data, length=None, offset=0)
 
def _setbytes_unsafe (self, data, length, offset)
 
def _readbytes (self, length, start)
 
def _getbytes (self)
 
def _setuint (self, uint, length=None)
 
def _readuint (self, length, start)
 
def _getuint (self)
 
def _setint (self, int_, length=None)
 
def _readint (self, length, start)
 
def _getint (self)
 
def _setuintbe (self, uintbe, length=None)
 
def _readuintbe (self, length, start)
 
def _getuintbe (self)
 
def _setintbe (self, intbe, length=None)
 
def _readintbe (self, length, start)
 
def _getintbe (self)
 
def _setuintle (self, uintle, length=None)
 
def _readuintle (self, length, start)
 
def _getuintle (self)
 
def _setintle (self, intle, length=None)
 
def _readintle (self, length, start)
 
def _getintle (self)
 
def _setfloat (self, f, length=None)
 
def _readfloat (self, length, start)
 
def _getfloat (self)
 
def _setfloatle (self, f, length=None)
 
def _readfloatle (self, length, start)
 
def _getfloatle (self)
 
def _setue (self, i)
 
def _readue (self, pos)
 
def _getue (self)
 
def _setse (self, i)
 
def _getse (self)
 
def _readse (self, pos)
 
def _setuie (self, i)
 
def _readuie (self, pos)
 
def _getuie (self)
 
def _setsie (self, i)
 
def _getsie (self)
 
def _readsie (self, pos)
 
def _setbool (self, value)
 
def _getbool (self)
 
def _readbool (self, pos)
 
def _setbin_safe (self, binstring)
 
def _setbin_unsafe (self, binstring)
 
def _readbin (self, length, start)
 
def _getbin (self)
 
def _setoct (self, octstring)
 
def _readoct (self, length, start)
 
def _getoct (self)
 
def _sethex (self, hexstring)
 
def _readhex (self, length, start)
 
def _gethex (self)
 
def _getoffset (self)
 
def _getlength (self)
 
def _ensureinmemory (self)
 
def _converttobitstring (cls, bs, offset=0, cache=None)
 
def _copy (self)
 
def _slice (self, start, end)
 
def _readtoken (self, name, pos, length)
 
def _append (self, bs)
 
def _prepend (self, bs)
 
def _reverse (self)
 
def _truncatestart (self, bits)
 
def _truncateend (self, bits)
 
def _insert (self, bs, pos)
 
def _overwrite (self, bs, pos)
 
def _delete (self, bits, pos)
 
def _reversebytes (self, start, end)
 
def _set (self, pos)
 
def _unset (self, pos)
 
def _invert (self, pos)
 
def _invert_all (self)
 
def _ilshift (self, n)
 
def _irshift (self, n)
 
def _imul (self, n)
 
def _inplace_logical_helper (self, bs, f)
 
def _ior (self, bs)
 
def _iand (self, bs)
 
def _ixor (self, bs)
 
def _readbits (self, length, start)
 
def _validate_slice (self, start, end)
 
def _readlist (self, fmt, pos, **kwargs)
 
def _findbytes (self, bytes_, start, end, bytealigned)
 
def _findregex (self, reg_ex, start, end, bytealigned)
 

Private Attributes

 _pos
 
 _datastore
 

Static Private Attributes

tuple __slots__ = ('_pos')
 
def __bool__ = __nonzero__
 
def _setfloatne = _setfloatle
 
def _readfloatne = _readfloatle
 
def _getfloatne = _getfloatle
 
def _setuintne = _setuintle
 
def _readuintne = _readuintle
 
def _getuintne = _getuintle
 
def _setintne = _setintle
 
def _readintne = _readintle
 
def _getintne = _getintle
 

Detailed Description

A container or stream holding an immutable sequence of bits.

For a mutable container use the BitStream class instead.

Methods inherited from Bits:

all() -- Check if all specified bits are set to 1 or 0.
any() -- Check if any of specified bits are set to 1 or 0.
count() -- Count the number of bits set to 1 or 0.
cut() -- Create generator of constant sized chunks.
endswith() -- Return whether the bitstring ends with a sub-string.
find() -- Find a sub-bitstring in the current bitstring.
findall() -- Find all occurrences of a sub-bitstring in the current bitstring.
join() -- Join bitstrings together using current bitstring.
rfind() -- Seek backwards to find a sub-bitstring.
split() -- Create generator of chunks split by a delimiter.
startswith() -- Return whether the bitstring starts with a sub-bitstring.
tobytes() -- Return bitstring as bytes, padding if needed.
tofile() -- Write bitstring to file, padding if needed.
unpack() -- Interpret bits using format string.

Other methods:

bytealign() -- Align to next byte boundary.
peek() -- Peek at and interpret next bits as a single item.
peeklist() -- Peek at and interpret next bits as a list of items.
read() -- Read and interpret next bits as a single item.
readlist() -- Read and interpret next bits as a list of items.

Special methods:

Also available are the operators [], ==, !=, +, *, ~, <<, >>, &, |, ^.

Properties:

bin -- The bitstring as a binary string.
bool -- For single bit bitstrings, interpret as True or False.
bytepos -- The current byte position in the bitstring.
bytes -- The bitstring as a bytes object.
float -- Interpret as a floating point number.
floatbe -- Interpret as a big-endian floating point number.
floatle -- Interpret as a little-endian floating point number.
floatne -- Interpret as a native-endian floating point number.
hex -- The bitstring as a hexadecimal string.
int -- Interpret as a two's complement signed integer.
intbe -- Interpret as a big-endian signed integer.
intle -- Interpret as a little-endian signed integer.
intne -- Interpret as a native-endian signed integer.
len -- Length of the bitstring in bits.
oct -- The bitstring as an octal string.
pos -- The current bit position in the bitstring.
se -- Interpret as a signed exponential-Golomb code.
ue -- Interpret as an unsigned exponential-Golomb code.
sie -- Interpret as a signed interleaved exponential-Golomb code.
uie -- Interpret as an unsigned interleaved exponential-Golomb code.
uint -- Interpret as a two's complement unsigned integer.
uintbe -- Interpret as a big-endian unsigned integer.
uintle -- Interpret as a little-endian unsigned integer.
uintne -- Interpret as a native-endian unsigned integer.

Definition at line 3699 of file bitstring.py.

Constructor & Destructor Documentation

◆ __init__()

def __init__ (   self,
  auto = None,
  length = None,
  offset = None,
**  kwargs 
)
Either specify an 'auto' initialiser:
auto -- a string of comma separated tokens, an integer, a file object,
        a bytearray, a boolean iterable or another bitstring.

Or initialise via **kwargs with one (and only one) of:
bytes -- raw data as a string, for example read from a binary file.
bin -- binary string representation, e.g. '0b001010'.
hex -- hexadecimal string representation, e.g. '0x2ef'
oct -- octal string representation, e.g. '0o777'.
uint -- an unsigned integer.
int -- a signed integer.
float -- a floating point number.
uintbe -- an unsigned big-endian whole byte integer.
intbe -- a signed big-endian whole byte integer.
floatbe - a big-endian floating point number.
uintle -- an unsigned little-endian whole byte integer.
intle -- a signed little-endian whole byte integer.
floatle -- a little-endian floating point number.
uintne -- an unsigned native-endian whole byte integer.
intne -- a signed native-endian whole byte integer.
floatne -- a native-endian floating point number.
se -- a signed exponential-Golomb code.
ue -- an unsigned exponential-Golomb code.
sie -- a signed interleaved exponential-Golomb code.
uie -- an unsigned interleaved exponential-Golomb code.
bool -- a boolean (True or False).
filename -- a file which will be opened in binary read-only mode.

Other keyword arguments:
length -- length of the bitstring in bits, if needed and appropriate.
          It must be supplied for all integer and float initialisers.
offset -- bit offset to the data. These offset bits are
          ignored and this is intended for use when
          initialising using 'bytes' or 'filename'.

Reimplemented from Bits.

Reimplemented in BitStream.

Definition at line 3764 of file bitstring.py.

Member Function Documentation

◆ __add__()

def __add__ (   self,
  bs 
)
Concatenate bitstrings and return new bitstring.

bs -- the bitstring to append.

Reimplemented from Bits.

Definition at line 3844 of file bitstring.py.

◆ __and__()

def __and__ (   self,
  bs 
)
inherited
Bit-wise 'and' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '&' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1079 of file bitstring.py.

◆ __contains__()

def __contains__ (   self,
  bs 
)
inherited
Return whether bs is contained in the current bitstring.

bs -- The bitstring to search for.

Definition at line 1157 of file bitstring.py.

◆ __copy__()

def __copy__ (   self)
Return a new copy of the ConstBitStream for the copy module.

Reimplemented from Bits.

Reimplemented in BitStream.

Definition at line 3834 of file bitstring.py.

◆ __eq__()

def __eq__ (   self,
  bs 
)
inherited
Return True if two bitstrings have the same binary representation.

>>> BitArray('0b1110') == '0xe'
True

Definition at line 990 of file bitstring.py.

◆ __getitem__()

def __getitem__ (   self,
  key 
)
inherited
Return a new bitstring representing a slice of the current bitstring.

Indices are in units of the step parameter (default 1 bit).
Stepping is used to specify the number of bits in each item.

>>> print BitArray('0b00110')[1:4]
'0b011'
>>> print BitArray('0x00112233')[1:3:8]
'0x1122'

Definition at line 893 of file bitstring.py.

◆ __hash__()

def __hash__ (   self)
inherited
Return an integer hash of the object.

Definition at line 1175 of file bitstring.py.

◆ __invert__()

def __invert__ (   self)
inherited
Return bitstring with every bit inverted.

Raises Error if the bitstring is empty.

Definition at line 1012 of file bitstring.py.

◆ __len__()

def __len__ (   self)
inherited
Return the length of the bitstring in bits.

Definition at line 938 of file bitstring.py.

◆ __lshift__()

def __lshift__ (   self,
  n 
)
inherited
Return bitstring with bits shifted by n to the left.

n -- the number of bits to shift. Must be >= 0.

Definition at line 1024 of file bitstring.py.

◆ __mul__()

def __mul__ (   self,
  n 
)
inherited
Return bitstring consisting of n concatenations of self.

Called for expression of the form 'a = b*3'.
n -- The number of concatenations. Must be >= 0.

Definition at line 1055 of file bitstring.py.

◆ __ne__()

def __ne__ (   self,
  bs 
)
inherited
Return False if two bitstrings have the same binary representation.

>>> BitArray('0b111') == '0x7'
False

Definition at line 1003 of file bitstring.py.

◆ __nonzero__()

def __nonzero__ (   self)
inherited
Return True if any bits are set to 1, otherwise return False.

Definition at line 1199 of file bitstring.py.

◆ __or__()

def __or__ (   self,
  bs 
)
inherited
Bit-wise 'or' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '|' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1105 of file bitstring.py.

◆ __radd__()

def __radd__ (   self,
  bs 
)
inherited
Append current bitstring to bs and return new bitstring.

bs -- the string for the 'auto' initialiser that will be appended to.

Definition at line 884 of file bitstring.py.

◆ __rand__()

def __rand__ (   self,
  bs 
)
inherited
Bit-wise 'and' between two bitstrings. Returns new bitstring.

bs -- the bitstring to '&' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1095 of file bitstring.py.

◆ __repr__()

def __repr__ (   self)
inherited
Return representation that could be used to recreate the bitstring.

If the returned string is too long it will be truncated. See __str__().

Definition at line 969 of file bitstring.py.

◆ __rmul__()

def __rmul__ (   self,
  n 
)
inherited
Return bitstring consisting of n concatenations of self.

Called for expressions of the form 'a = 3*b'.
n -- The number of concatenations. Must be >= 0.

Definition at line 1070 of file bitstring.py.

◆ __ror__()

def __ror__ (   self,
  bs 
)
inherited
Bit-wise 'or' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '|' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1121 of file bitstring.py.

◆ __rshift__()

def __rshift__ (   self,
  n 
)
inherited
Return bitstring with bits shifted by n to the right.

n -- the number of bits to shift. Must be >= 0.

Definition at line 1039 of file bitstring.py.

◆ __rxor__()

def __rxor__ (   self,
  bs 
)
inherited
Bit-wise 'xor' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '^' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1147 of file bitstring.py.

◆ __str__()

def __str__ (   self)
inherited
Return approximate string representation of bitstring for printing.

Short strings will be given wholly in hexadecimal or binary. Longer
strings may be part hexadecimal and part binary. Very long strings will
be truncated with '...'.

Definition at line 942 of file bitstring.py.

◆ __xor__()

def __xor__ (   self,
  bs 
)
inherited
Bit-wise 'xor' between two bitstrings. Returns new bitstring.

bs -- The bitstring to '^' with.

Raises ValueError if the two bitstrings have differing lengths.

Definition at line 1131 of file bitstring.py.

◆ _append()

def _append (   self,
  bs 
)
privateinherited
Append a bitstring to the current bitstring.

Definition at line 2035 of file bitstring.py.

◆ _assertsanity()

def _assertsanity (   self)
privateinherited
Check internal self consistency as a debugging aid.

Definition at line 1206 of file bitstring.py.

◆ _clear()

def _clear (   self)
private
Reset the bitstring to an empty state.

Reimplemented from Bits.

Definition at line 3830 of file bitstring.py.

◆ _converttobitstring()

def _converttobitstring (   cls,
  bs,
  offset = 0,
  cache = None 
)
privateinherited
Convert bs to a bitstring and return it.

offset gives the suggested bit offset of first significant
bit, to optimise append etc.

Definition at line 1966 of file bitstring.py.

◆ _copy()

def _copy (   self)
privateinherited
Create and return a new copy of the Bits (always in memory).

Definition at line 2001 of file bitstring.py.

◆ _delete()

def _delete (   self,
  bits,
  pos 
)
privateinherited
Delete bits at pos.

Definition at line 2135 of file bitstring.py.

◆ _ensureinmemory()

def _ensureinmemory (   self)
privateinherited
Ensure the data is held in memory, not in a file.

Definition at line 1960 of file bitstring.py.

◆ _findbytes()

def _findbytes (   self,
  bytes_,
  start,
  end,
  bytealigned 
)
privateinherited
Quicker version of find when everything's whole byte
and byte aligned.

Definition at line 2365 of file bitstring.py.

◆ _findregex()

def _findregex (   self,
  reg_ex,
  start,
  end,
  bytealigned 
)
privateinherited
Find first occurrence of a compiled regular expression.

Note that this doesn't support arbitrary regexes, in particular they
must match a known length.

Definition at line 2392 of file bitstring.py.

◆ _getbin()

def _getbin (   self)
privateinherited
Return interpretation as a binary string.

Definition at line 1872 of file bitstring.py.

◆ _getbitpos()

def _getbitpos (   self)
private
Return the current position in the stream in bits.

Definition at line 3826 of file bitstring.py.

◆ _getbytepos()

def _getbytepos (   self)
private
Return the current position in the stream in bytes. Must be byte aligned.

Definition at line 3812 of file bitstring.py.

◆ _getbytes()

def _getbytes (   self)
privateinherited
Return the data as an ordinary string.

Definition at line 1355 of file bitstring.py.

◆ _getfloat()

def _getfloat (   self)
privateinherited
Interpret the whole bitstring as a float.

Definition at line 1586 of file bitstring.py.

◆ _getfloatle()

def _getfloatle (   self)
privateinherited
Interpret the whole bitstring as a little-endian float.

Definition at line 1625 of file bitstring.py.

◆ _gethex()

def _gethex (   self)
privateinherited
Return the hexadecimal representation as a string prefixed with '0x'.

Raises an InterpretError if the bitstring's length is not a multiple of 4.

Definition at line 1945 of file bitstring.py.

◆ _getint()

def _getint (   self)
privateinherited
Return data as a two's complement signed int.

Definition at line 1454 of file bitstring.py.

◆ _getintbe()

def _getintbe (   self)
privateinherited
Return data as a big-endian two's complement signed int.

Definition at line 1490 of file bitstring.py.

◆ _getlength()

def _getlength (   self)
privateinherited
Return the length of the bitstring in bits.

Definition at line 1956 of file bitstring.py.

◆ _getoct()

def _getoct (   self)
privateinherited
Return interpretation as an octal string.

Definition at line 1906 of file bitstring.py.

◆ _getse()

def _getse (   self)
privateinherited
Return data as signed exponential-Golomb code.

Raises InterpretError if bitstring is not a single exponential-Golomb code.

Definition at line 1698 of file bitstring.py.

◆ _getsie()

def _getsie (   self)
privateinherited
Return data as signed interleaved exponential-Golomb code.

Raises InterpretError if bitstring is not a single exponential-Golomb code.

Definition at line 1781 of file bitstring.py.

◆ _getue()

def _getue (   self)
privateinherited
Return data as unsigned exponential-Golomb code.

Raises InterpretError if bitstring is not a single exponential-Golomb code.

Definition at line 1676 of file bitstring.py.

◆ _getuie()

def _getuie (   self)
privateinherited
Return data as unsigned interleaved exponential-Golomb code.

Raises InterpretError if bitstring is not a single exponential-Golomb code.

Definition at line 1759 of file bitstring.py.

◆ _getuint()

def _getuint (   self)
privateinherited
Return data as an unsigned int.

Definition at line 1417 of file bitstring.py.

◆ _getuintbe()

def _getuintbe (   self)
privateinherited
Return data as a big-endian two's complement unsigned int.

Definition at line 1472 of file bitstring.py.

◆ _ilshift()

def _ilshift (   self,
  n 
)
privateinherited
Shift bits by n to the left in place. Return self.

Definition at line 2196 of file bitstring.py.

◆ _imul()

def _imul (   self,
  n 
)
privateinherited
Concatenate n copies of self in place. Return self.

Definition at line 2210 of file bitstring.py.

◆ _inplace_logical_helper()

def _inplace_logical_helper (   self,
  bs,
  f 
)
privateinherited
Helper function containing most of the __ior__, __iand__, __ixor__ code.

Definition at line 2224 of file bitstring.py.

◆ _insert()

def _insert (   self,
  bs,
  pos 
)
privateinherited
Insert bs at pos.

Definition at line 2081 of file bitstring.py.

◆ _invert()

def _invert (   self,
  pos 
)
privateinherited
Flip bit at pos 1<->0.

Definition at line 2184 of file bitstring.py.

◆ _invert_all()

def _invert_all (   self)
privateinherited
Invert every bit.

Definition at line 2189 of file bitstring.py.

◆ _irshift()

def _irshift (   self,
  n 
)
privateinherited
Shift bits by n to the right in place. Return self.

Definition at line 2203 of file bitstring.py.

◆ _overwrite()

def _overwrite (   self,
  bs,
  pos 
)
privateinherited
Overwrite with bs at pos.

Definition at line 2102 of file bitstring.py.

◆ _prepend()

def _prepend (   self,
  bs 
)
privateinherited
Prepend a bitstring to the current bitstring.

Definition at line 2039 of file bitstring.py.

◆ _readbin()

def _readbin (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as a binary string.

Definition at line 1855 of file bitstring.py.

◆ _readbits()

def _readbits (   self,
  length,
  start 
)
privateinherited
Read some bits from the bitstring and return newly constructed bitstring.

Definition at line 2249 of file bitstring.py.

◆ _readbytes()

def _readbytes (   self,
  length,
  start 
)
privateinherited
Read bytes and return them. Note that length is in bits.

Definition at line 1346 of file bitstring.py.

◆ _readfloat()

def _readfloat (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as a float.

Definition at line 1568 of file bitstring.py.

◆ _readfloatle()

def _readfloatle (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as a little-endian float.

Definition at line 1606 of file bitstring.py.

◆ _readhex()

def _readhex (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as a hex string.

Definition at line 1928 of file bitstring.py.

◆ _readint()

def _readint (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as a signed int

Definition at line 1444 of file bitstring.py.

◆ _readintbe()

def _readintbe (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as a big-endian signed int.

Definition at line 1483 of file bitstring.py.

◆ _readintle()

def _readintle (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as a little-endian signed int.

Definition at line 1539 of file bitstring.py.

◆ _readoct()

def _readoct (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as an octal string.

Definition at line 1891 of file bitstring.py.

◆ _readse()

def _readse (   self,
  pos 
)
privateinherited
Return interpretation of next bits as a signed exponential-Golomb code.

Advances position to after the read code.

Raises ReadError if the end of the bitstring is encountered while
reading the code.

Definition at line 1712 of file bitstring.py.

◆ _readsie()

def _readsie (   self,
  pos 
)
privateinherited
Return interpretation of next bits as a signed interleaved exponential-Golomb code.

Advances position to after the read code.

Raises ReadError if the end of the bitstring is encountered while
reading the code.

Definition at line 1795 of file bitstring.py.

◆ _readtoken()

def _readtoken (   self,
  name,
  pos,
  length 
)
privateinherited
Reads a token from the bitstring and returns the result.

Definition at line 2019 of file bitstring.py.

◆ _readue()

def _readue (   self,
  pos 
)
privateinherited
Return interpretation of next bits as unsigned exponential-Golomb code.

Raises ReadError if the end of the bitstring is encountered while
reading the code.

Definition at line 1651 of file bitstring.py.

◆ _readuie()

def _readuie (   self,
  pos 
)
privateinherited
Return interpretation of next bits as unsigned interleaved exponential-Golomb code.

Raises ReadError if the end of the bitstring is encountered while
reading the code.

Definition at line 1739 of file bitstring.py.

◆ _readuint()

def _readuint (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as an unsigned int.

Definition at line 1399 of file bitstring.py.

◆ _readuintbe()

def _readuintbe (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as a big-endian unsigned int.

Definition at line 1465 of file bitstring.py.

◆ _readuintle()

def _readuintle (   self,
  length,
  start 
)
privateinherited
Read bits and interpret as a little-endian unsigned int.

Definition at line 1501 of file bitstring.py.

◆ _reverse()

def _reverse (   self)
privateinherited
Reverse all bits in-place.

Definition at line 2043 of file bitstring.py.

◆ _reversebytes()

def _reversebytes (   self,
  start,
  end 
)
privateinherited
Reverse bytes in-place.

Definition at line 2161 of file bitstring.py.

◆ _set()

def _set (   self,
  pos 
)
privateinherited
Set bit at pos to 1.

Definition at line 2174 of file bitstring.py.

◆ _setauto()

def _setauto (   self,
  s,
  length,
  offset 
)
privateinherited
Set bitstring from a bitstring, file, bool, integer, array, iterable or string.

Definition at line 1256 of file bitstring.py.

◆ _setbin_safe()

def _setbin_safe (   self,
  binstring 
)
privateinherited
Reset the bitstring to the value given in binstring.

Definition at line 1834 of file bitstring.py.

◆ _setbin_unsafe()

def _setbin_unsafe (   self,
  binstring 
)
privateinherited
Same as _setbin_safe, but input isn't sanity checked. binstring mustn't start with '0b'.

Definition at line 1841 of file bitstring.py.

◆ _setbitpos()

def _setbitpos (   self,
  pos 
)
private
Move to absolute postion bit in bitstream.

Definition at line 3818 of file bitstring.py.

◆ _setbytepos()

def _setbytepos (   self,
  bytepos 
)
private
Move to absolute byte-aligned position in stream.

Definition at line 3808 of file bitstring.py.

◆ _setbytes_safe()

def _setbytes_safe (   self,
  data,
  length = None,
  offset = 0 
)
privateinherited
Set the data from a string.

Definition at line 1325 of file bitstring.py.

◆ _setbytes_unsafe()

def _setbytes_unsafe (   self,
  data,
  length,
  offset 
)
privateinherited
Unchecked version of _setbytes_safe.

Definition at line 1341 of file bitstring.py.

◆ _setfile()

def _setfile (   self,
  filename,
  length,
  offset 
)
privateinherited
Use file as source of bits.

Definition at line 1310 of file bitstring.py.

◆ _sethex()

def _sethex (   self,
  hexstring 
)
privateinherited
Reset the bitstring to have the value given in hexstring.

Definition at line 1910 of file bitstring.py.

◆ _setint()

def _setint (   self,
  int_,
  length = None 
)
privateinherited
Reset the bitstring to have given signed int interpretation.

Definition at line 1421 of file bitstring.py.

◆ _setintbe()

def _setintbe (   self,
  intbe,
  length = None 
)
privateinherited
Set bitstring to a big-endian signed int interpretation.

Definition at line 1476 of file bitstring.py.

◆ _setoct()

def _setoct (   self,
  octstring 
)
privateinherited
Reset the bitstring to have the value given in octstring.

Definition at line 1876 of file bitstring.py.

◆ _setse()

def _setse (   self,
  i 
)
privateinherited
Initialise bitstring with signed exponential-Golomb code for integer i.

Definition at line 1690 of file bitstring.py.

◆ _setsie()

def _setsie (   self,
  i 
)
privateinherited
Initialise bitstring with signed interleaved exponential-Golomb code for integer i.

Definition at line 1773 of file bitstring.py.

◆ _setue()

def _setue (   self,
  i 
)
privateinherited
Initialise bitstring with unsigned exponential-Golomb code for integer i.

Raises CreationError if i < 0.

Definition at line 1629 of file bitstring.py.

◆ _setuie()

def _setuie (   self,
  i 
)
privateinherited
Initialise bitstring with unsigned interleaved exponential-Golomb code for integer i.

Raises CreationError if i < 0.

Definition at line 1728 of file bitstring.py.

◆ _setuint()

def _setuint (   self,
  uint,
  length = None 
)
privateinherited
Reset the bitstring to have given unsigned int interpretation.

Definition at line 1362 of file bitstring.py.

◆ _setuintbe()

def _setuintbe (   self,
  uintbe,
  length = None 
)
privateinherited
Set the bitstring to a big-endian unsigned int interpretation.

Definition at line 1458 of file bitstring.py.

◆ _slice()

def _slice (   self,
  start,
  end 
)
privateinherited
Used internally to get a slice, without error checking.

Definition at line 2008 of file bitstring.py.

◆ _truncateend()

def _truncateend (   self,
  bits 
)
privateinherited
Truncate bits from the end of the bitstring.

Definition at line 2068 of file bitstring.py.

◆ _truncatestart()

def _truncatestart (   self,
  bits 
)
privateinherited
Truncate bits from the start of the bitstring.

Definition at line 2055 of file bitstring.py.

◆ _unset()

def _unset (   self,
  pos 
)
privateinherited
Set bit at pos to 0.

Definition at line 2179 of file bitstring.py.

◆ _validate_slice()

def _validate_slice (   self,
  start,
  end 
)
privateinherited
Validate start and end and return them as positive bit positions.

Definition at line 2253 of file bitstring.py.

◆ all()

def all (   self,
  value,
  pos = None 
)
inherited
Return True if one or many bits are all set to value.

value -- If value is True then checks for bits set to 1, otherwise
         checks for bits set to 0.
pos -- An iterable of bit positions. Negative numbers are treated in
       the same way as slice indices. Defaults to the whole bitstring.

Definition at line 2732 of file bitstring.py.

◆ any()

def any (   self,
  value,
  pos = None 
)
inherited
Return True if any of one or many bits are set to value.

value -- If value is True then checks for bits set to 1, otherwise
         checks for bits set to 0.
pos -- An iterable of bit positions. Negative numbers are treated in
       the same way as slice indices. Defaults to the whole bitstring.

Definition at line 2754 of file bitstring.py.

◆ bytealign()

def bytealign (   self)
Align to next byte and return number of skipped bits.

Raises ValueError if the end of the bitstring is reached before
aligning to the next byte.

Definition at line 3997 of file bitstring.py.

◆ count()

def count (   self,
  value 
)
inherited
Return count of total number of either zero or one bits.

value -- If True then bits set to 1 are counted, otherwise bits set
         to 0 are counted.

>>> Bits('0xef').count(1)
7

Definition at line 2776 of file bitstring.py.

◆ cut()

def cut (   self,
  bits,
  start = None,
  end = None,
  count = None 
)
inherited
Return bitstring generator by cutting into bits sized chunks.

bits -- The size in bits of the bitstring chunks to generate.
start -- The bit position to start the first cut. Defaults to 0.
end -- The bit position one past the last bit to use in the cut.
       Defaults to self.len.
count -- If specified then at most count items are generated.
         Default is to cut as many times as possible.

Definition at line 2553 of file bitstring.py.

◆ endswith()

def endswith (   self,
  suffix,
  start = None,
  end = None 
)
inherited
Return whether the current bitstring ends with suffix.

suffix -- The bitstring to search for.
start -- The bit position to start from. Defaults to 0.
end -- The bit position to end at. Defaults to self.len.

Definition at line 2717 of file bitstring.py.

◆ find()

def find (   self,
  bs,
  start = None,
  end = None,
  bytealigned = None 
)
inherited
Find first occurrence of substring bs.

Returns a single item tuple with the bit position if found, or an
empty tuple if not found. The bit position (pos property) will
also be set to the start of the substring if it is found.

bs -- The bitstring to find.
start -- The bit position to start the search. Defaults to 0.
end -- The bit position one past the last bit to search.
       Defaults to self.len.
bytealigned -- If True the bitstring will only be
               found on byte boundaries.

Raises ValueError if bs is empty, if start < 0, if end > self.len or
if end < start.

>>> BitArray('0xc3e').find('0b1111')
(6,)

Definition at line 2424 of file bitstring.py.

◆ findall()

def findall (   self,
  bs,
  start = None,
  end = None,
  count = None,
  bytealigned = None 
)
inherited
Find all occurrences of bs. Return generator of bit positions.

bs -- The bitstring to find.
start -- The bit position to start the search. Defaults to 0.
end -- The bit position one past the last bit to search.
       Defaults to self.len.
count -- The maximum number of occurrences to find.
bytealigned -- If True the bitstring will only be found on
               byte boundaries.

Raises ValueError if bs is empty, if start < 0, if end > self.len or
if end < start.

Note that all occurrences of bs are found, even if they overlap.

Definition at line 2462 of file bitstring.py.

◆ join()

def join (   self,
  sequence 
)
inherited
Return concatenation of bitstrings joined by self.

sequence -- A sequence of bitstrings.

Definition at line 2637 of file bitstring.py.

◆ peek()

def peek (   self,
  fmt 
)
Interpret next bits according to format string and return result.

fmt -- Token string describing how to interpret the next bits.

The position in the bitstring is not changed. If not enough bits are
available then all bits to the end of the bitstring will be used.

Raises ReadError if not enough bits are available.
Raises ValueError if the format is not understood.

See the docstring for 'read' for token examples.

Definition at line 3956 of file bitstring.py.

◆ peeklist()

def peeklist (   self,
  fmt,
**  kwargs 
)
Interpret next bits according to format string(s) and return list.

fmt -- One or more strings with comma separated tokens describing
       how to interpret the next bits in the bitstring.
kwargs -- A dictionary or keyword-value pairs - the keywords used in the
          format string will be replaced with their given value.

The position in the bitstring is not changed. If not enough bits are
available then all bits to the end of the bitstring will be used.

Raises ReadError if not enough bits are available.
Raises ValueError if the format is not understood.

See the docstring for 'read' for token examples.

Definition at line 3975 of file bitstring.py.

◆ read()

def read (   self,
  fmt 
)
Interpret next bits according to the format string and return result.

fmt -- Token string describing how to interpret the next bits.

Token examples: 'int:12'    : 12 bits as a signed integer
                'uint:8'    : 8 bits as an unsigned integer
                'float:64'  : 8 bytes as a big-endian float
                'intbe:16'  : 2 bytes as a big-endian signed integer
                'uintbe:16' : 2 bytes as a big-endian unsigned integer
                'intle:32'  : 4 bytes as a little-endian signed integer
                'uintle:32' : 4 bytes as a little-endian unsigned integer
                'floatle:64': 8 bytes as a little-endian float
                'intne:24'  : 3 bytes as a native-endian signed integer
                'uintne:24' : 3 bytes as a native-endian unsigned integer
                'floatne:32': 4 bytes as a native-endian float
                'hex:80'    : 80 bits as a hex string
                'oct:9'     : 9 bits as an octal string
                'bin:1'     : single bit binary string
                'ue'        : next bits as unsigned exp-Golomb code
                'se'        : next bits as signed exp-Golomb code
                'uie'       : next bits as unsigned interleaved exp-Golomb code
                'sie'       : next bits as signed interleaved exp-Golomb code
                'bits:5'    : 5 bits as a bitstring
                'bytes:10'  : 10 bytes as a bytes object
                'bool'      : 1 bit as a bool
                'pad:3'     : 3 bits of padding to ignore - returns None

fmt may also be an integer, which will be treated like the 'bits' token.

The position in the bitstring is advanced to after the read items.

Raises ReadError if not enough bits are available.
Raises ValueError if the format is not understood.

Definition at line 3854 of file bitstring.py.

◆ readlist()

def readlist (   self,
  fmt,
**  kwargs 
)
Interpret next bits according to format string(s) and return list.

fmt -- A single string or list of strings with comma separated tokens
       describing how to interpret the next bits in the bitstring. Items
       can also be integers, for reading new bitstring of the given length.
kwargs -- A dictionary or keyword-value pairs - the keywords used in the
          format string will be replaced with their given value.

The position in the bitstring is advanced to after the read items.

Raises ReadError is not enough bits are available.
Raises ValueError if the format is not understood.

See the docstring for 'read' for token examples. 'pad' tokens are skipped
and not added to the returned list.

>>> h, b1, b2 = s.readlist('hex:20, bin:5, bin:3')
>>> i, bs1, bs2 = s.readlist(['uint:12', 10, 10])

Definition at line 3911 of file bitstring.py.

◆ readto()

def readto (   self,
  bs,
  bytealigned = None 
)
Read up to and including next occurrence of bs and return result.

bs -- The bitstring to find. An integer is not permitted.
bytealigned -- If True the bitstring will only be
               found on byte boundaries.

Raises ValueError if bs is empty.
Raises ReadError if bs is not found.

Definition at line 3935 of file bitstring.py.

◆ rfind()

def rfind (   self,
  bs,
  start = None,
  end = None,
  bytealigned = None 
)
inherited
Find final occurrence of substring bs.

Returns a single item tuple with the bit position if found, or an
empty tuple if not found. The bit position (pos property) will
also be set to the start of the substring if it is found.

bs -- The bitstring to find.
start -- The bit position to end the reverse search. Defaults to 0.
end -- The bit position one past the first bit to reverse search.
       Defaults to self.len.
bytealigned -- If True the bitstring will only be found on byte
               boundaries.

Raises ValueError if bs is empty, if start < 0, if end > self.len or
if end < start.

Definition at line 2514 of file bitstring.py.

◆ split()

def split (   self,
  delimiter,
  start = None,
  end = None,
  count = None,
  bytealigned = None 
)
inherited
Return bitstring generator by splittling using a delimiter.

The first item returned is the initial bitstring before the delimiter,
which may be an empty bitstring.

delimiter -- The bitstring used as the divider.
start -- The bit position to start the split. Defaults to 0.
end -- The bit position one past the last bit to use in the split.
       Defaults to self.len.
count -- If specified then at most count items are generated.
         Default is to split as many times as possible.
bytealigned -- If True splits will only occur on byte boundaries.

Raises ValueError if the delimiter is empty.

Definition at line 2580 of file bitstring.py.

◆ startswith()

def startswith (   self,
  prefix,
  start = None,
  end = None 
)
inherited
Return whether the current bitstring starts with prefix.

prefix -- The bitstring to search for.
start -- The bit position to start from. Defaults to 0.
end -- The bit position to end at. Defaults to self.len.

Definition at line 2702 of file bitstring.py.

◆ tobytes()

def tobytes (   self)
inherited
Return the bitstring as bytes, padding with zero bits if needed.

Up to seven zero bits will be added at the end to byte align.

Definition at line 2655 of file bitstring.py.

◆ tofile()

def tofile (   self,
  f 
)
inherited
Write the bitstring to a file object, padding with zero bits if needed.

Up to seven zero bits will be added at the end to byte align.

Definition at line 2668 of file bitstring.py.

◆ unpack()

def unpack (   self,
  fmt,
**  kwargs 
)
inherited
Interpret the whole bitstring using fmt and return list.

fmt -- A single string or a list of strings with comma separated tokens
       describing how to interpret the bits in the bitstring. Items
       can also be integers, for reading new bitstring of the given length.
kwargs -- A dictionary or keyword-value pairs - the keywords used in the
          format string will be replaced with their given value.

Raises ValueError if the format is not understood. If not enough bits
are available then all bits to the end of the bitstring will be used.

See the docstring for 'read' for token examples.

Definition at line 2271 of file bitstring.py.

Property Documentation

◆ bin

bin
staticinherited
Initial value:
= property(_getbin,
doc=)

Definition at line 2835 of file bitstring.py.

◆ bitpos

bitpos
static
Initial value:
= property(_getbitpos, _setbitpos,
doc=)

Definition at line 4012 of file bitstring.py.

◆ bool

bool
staticinherited
Initial value:
= property(_getbool,
doc=)

Definition at line 2829 of file bitstring.py.

◆ bytepos

bytepos
static
Initial value:
= property(_getbytepos, _setbytepos,
doc=)

Definition at line 4015 of file bitstring.py.

◆ bytes

bytes
staticinherited
Initial value:
= property(_getbytes,
doc=)

Definition at line 2841 of file bitstring.py.

◆ float

float
staticinherited
Initial value:
= property(_getfloat,
doc=)

Definition at line 2850 of file bitstring.py.

◆ floatbe

floatbe
staticinherited
Initial value:
= property(_getfloat,
doc=)

Definition at line 2859 of file bitstring.py.

◆ floatle

floatle
staticinherited
Initial value:
= property(_getfloatle,
doc=)

Definition at line 2868 of file bitstring.py.

◆ floatne

floatne
staticinherited
Initial value:
= property(_getfloatne,
doc=)

Definition at line 2877 of file bitstring.py.

◆ hex

hex
staticinherited
Initial value:
= property(_gethex,
doc=)

Definition at line 2832 of file bitstring.py.

◆ int

int
staticinherited
Initial value:
= property(_getint,
doc=)

Definition at line 2844 of file bitstring.py.

◆ intbe

intbe
staticinherited
Initial value:
= property(_getintbe,
doc=)

Definition at line 2853 of file bitstring.py.

◆ intle

intle
staticinherited
Initial value:
= property(_getintle,
doc=)

Definition at line 2862 of file bitstring.py.

◆ intne

intne
staticinherited
Initial value:
= property(_getintne,
doc=)

Definition at line 2871 of file bitstring.py.

◆ len

len
staticinherited
Initial value:
= property(_getlength,
doc=)

Definition at line 2823 of file bitstring.py.

◆ length

length
staticinherited
Initial value:
= property(_getlength,
doc=)

Definition at line 2826 of file bitstring.py.

◆ oct

oct
staticinherited
Initial value:
= property(_getoct,
doc=)

Definition at line 2838 of file bitstring.py.

◆ pos

pos
static
Initial value:
= property(_getbitpos, _setbitpos,
doc=)

Definition at line 4009 of file bitstring.py.

◆ se

se
staticinherited
Initial value:
= property(_getse,
doc=)

Definition at line 2883 of file bitstring.py.

◆ sie

sie
staticinherited
Initial value:
= property(_getsie,
doc=)

Definition at line 2889 of file bitstring.py.

◆ ue

ue
staticinherited
Initial value:
= property(_getue,
doc=)

Definition at line 2880 of file bitstring.py.

◆ uie

uie
staticinherited
Initial value:
= property(_getuie,
doc=)

Definition at line 2886 of file bitstring.py.

◆ uint

uint
staticinherited
Initial value:
= property(_getuint,
doc=)

Definition at line 2847 of file bitstring.py.

◆ uintbe

uintbe
staticinherited
Initial value:
= property(_getuintbe,
doc=)

Definition at line 2856 of file bitstring.py.

◆ uintle

uintle
staticinherited
Initial value:
= property(_getuintle,
doc=)

Definition at line 2865 of file bitstring.py.

◆ uintne

uintne
staticinherited
Initial value:
= property(_getuintne,
doc=)

Definition at line 2874 of file bitstring.py.


The documentation for this class was generated from the following file: