Reader

The Reader class allows binary data to be read and parsed as a stream of bits with a bit position.


class Reader(bits, pos=0)

Wraps a Bits or MutableBits object and a bit position to allow reading and parsing as a stream of bits.

Methods:

  • read(): Read from the current bit position, and interpret according to the given format.

  • peek(): Peek from the current bit position, and interpret according to the given format without changing the pos.

  • parse(): Parse a fieldtype from the current bit position, returning the number of bits parsed.

Properties:

  • bits: The Bits or MutableBits object.

  • pos: The current bit position to read and parse from.

parse(f, /)

Parse a fieldtype from the current bit position, returning the number of bits parsed.

Parameters:

f (FieldType) – The fieldtype to parse.

Return type:

int

Returns:

The number of bits parsed.

Raises:

TypeError – If f is not a subclass of FieldType.

peek(dtype, /)

Peek from the current bit position, and interpret according to the given Dtype.

Param:

dtype: The Dtype to interpret the bits as, or a string or int that can be converted to a Dtype.

Return type:

Any | tuple[Any] | list[Any | tuple[Any]]

Returns:

The value interpreted as the given dtype without changing the current position.

read(dtype, /)

Read from the current bit position, and interpret according to the given Dtype.

Return type:

Any | tuple[Any] | list[Any | tuple[Any]]

property bits: Bits | MutableBits

Get or set the Bits or MutableBits object associated with the Reader.

Changing this object may invalidate the bit position, but it’s left to the user to manage this.

Returns:

The current bits object.

Raises:

ValueError – If the provided value is not a valid type.

property pos: int

Get or set the current bit position.

This should be a positive int, but no attempt is made to check if the position is valid before it is used.

Returns:

The current bit position.

Raises:

ValueError – If set to a value that is not an integer.