Reader#
- class Reader(bits: Bits | BitArray, pos: int = 0)#
Wraps a
BitsorBitArrayobject with a bit position for sequential reading.The wrapped bitstring is exposed directly as
bits. If it is aBitArraythen it can be mutated through the reader, for exampler.bits.append('0xff'). Mutating the wrapped object never updatesposautomatically.
Reading and peeking#
The Reader.read and Reader.read_list methods interpret bits starting at
Reader.pos and advance the position. The matching Reader.peek and
Reader.peek_list methods return the same values without changing the position.
For example:
>>> r = Reader(Bits('0x160120f'))
>>> r.read(12).hex
'160'
>>> r.pos = 0
>>> r.read('u12')
352
>>> r.read_list('u12, bin3')
[288, '111']
If a read fails then Reader.pos is restored to its value before the read.
The position is deliberately lax: assigning to Reader.pos, Reader.bitpos or
Reader.bytepos stores the integer value without checking it against the wrapped
bitstring length. Reading methods will raise an error if the position is not
usable when they need it.
Methods#
- Reader.read(fmt: str | int | Dtype) int | float | str | Bits | bool | bytes | None#
Reads from
Reader.posaccording to fmt and advances the position.If fmt is an integer then that many bits are returned as a bitstring. If it is a string or
Dtypethen the corresponding dtype is read.
- Reader.read_list(fmt: str | list[str | int | Dtype], **kwargs) list[int | float | str | Bits | bool | bytes | None]#
Reads one or more format tokens and returns a list of values.
- Reader.peek(fmt: str | int | Dtype) int | float | str | Bits | bool | bytes | None#
Like
Reader.read, but leavesReader.posunchanged.
- Reader.peek_list(fmt: str | list[str | int | Dtype], **kwargs) list[int | float | str | Bits | bool | bytes | None]#
Like
Reader.read_list, but leavesReader.posunchanged.
- Reader.read_to(bs: BitsType, *, bytealigned: bool = False) Bits#
Reads up to and including the next occurrence of bs.
- Reader.byte_align() int#
Aligns
Reader.posto the next byte boundary and returns the number of bits skipped.
- Reader.find(bs: BitsType, start: int | None = None, end: int | None = None, *, bytealigned: bool = False) int | None#
Searches the wrapped bitstring and sets
Reader.posto the match position if bs is found. Returns the match position, orNoneif not found.
- Reader.rfind(bs: BitsType, start: int | None = None, end: int | None = None, *, bytealigned: bool = False) int | None#
Searches backwards and sets
Reader.posto the match position if bs is found. Returns the match position, orNoneif not found.
- Reader.__len__() int#
len(r)returns the length in bits of the wrapped bitstring.
Properties#
- Reader.bits: Bits | BitArray#
The wrapped bitstring object. This is the original object, not a copy.
- Reader.pos: int#
The current bit position. Stores any integer value.
- Reader.bitpos: int#
A deprecated compatibility alias for
Reader.pos.
- Reader.bytepos: int#
The current byte position. Reading this property requires
Reader.posto be byte aligned and raisesByteAlignErrorotherwise.