View and MutableView#
Views wrap bit data with interpretation settings such as byte order and bit
order. They do not change the meaning of normal Tibs or Mutibs indexing;
they only affect conversions such as integer, float, bytes and labelled field
access.
See Views in the manual for more details.
View#
- class View#
A view of a
Tibswith different interpretation settings.A
Viewdoes not change the underlying bits. It records how operations such as integer conversion, byte conversion and field extraction should interpret those bits.Views are usually created from
Tibsinstances using thele,be,lsb0,msb0orview()helpers.Passing a
Mutibsto the directViewconstructor stores aTibssnapshot. Later changes to the originalMutibsare not reflected in the view. UseMutableViewfor a live mutable view.>>> t = Tibs('0x0100') >>> t.le.u 1 >>> t.lsb0.hex '8000'
- field(a, b)#
Extract a field using inclusive bit labels.
This is intended for specifications that describe fields using inclusive bit labels such as
31:26.aandbmust be zero or positive bit labels. The endpoints may be provided in either order.For an LSB0 view, labels are interpreted within each byte with bit 0 at the least significant bit. For an MSB0 view, labels match normal Python slice positions.
Labels are selected in ascending order after endpoint normalization. The returned view has
BitOrder.Msb0because the selected bits have been materialized. The current byte order is kept for whole-byte fields and dropped for non-whole-byte fields.- Parameters:
a (int) – One non-negative inclusive field endpoint.
b (int) – The other non-negative inclusive field endpoint.
- Returns:
A new
Viewcontaining the field.
>>> t = Tibs('0x88040410') >>> t.lsb0.field(31, 26).u 8
- from_indices()#
Create a view by materializing selected source bit positions.
indicesmay be arangeor any iterable of integers. It maps each viewed bit to a physical bit position in the source. Passing aMutibssource stores an immutable snapshot.This is a low-level reconstruction API. Use
field()for normal specification-labelled fields.>>> View.from_indices(Tibs('0xf0'), range(0, 4)).bin '1111' >>> View.from_indices(Tibs('0xf0'), [7, 6, 5, 4]).bin '0000'
- to_bin()#
Return the viewed bits as a binary string.
- Returns:
The binary representation as a string.
- to_bytes()#
Return the viewed bits as bytes.
The viewed length must be a whole number of bytes.
- Returns:
A
bytesvalue.
- to_f()#
Interpret the viewed bits as an IEEE floating point value.
The viewed length must be 16, 32 or 64 bits.
- Returns:
The floating point value.
- to_hex()#
Return the viewed bits as a hexadecimal string.
- Returns:
The hexadecimal representation as a string.
>>> Tibs('0x0100').le.to_hex() '0001'
- to_i()#
Interpret the viewed bits as a signed integer.
- Returns:
The signed integer value.
- to_oct()#
Return the viewed bits as an octal string.
- Returns:
The octal representation as a string.
- to_u()#
Interpret the viewed bits as an unsigned integer.
- Returns:
The unsigned integer value.
>>> Tibs('0x0100').le.to_u() 1
- view(byte_order=None, bit_order=None)#
Return a view with updated interpretation settings.
Any setting left as
Nonekeeps its current value.Byte-oriented views must have a whole-byte length. This applies when using little-endian or big-endian byte order, or when using
BitOrder.Lsb0.- Parameters:
byte_order (Endianness) – The byte order to use, or
Noneto keep the current byte order.bit_order (BitOrder) – The bit order to use, or
Noneto keep the current bit order.
- Returns:
A new
View.
>>> Tibs('0x0100').view(byte_order=Endianness.Little).u 1
- be#
Return a big-endian byte-order view.
Equivalent to
view(byte_order=Endianness.Big).The view length must be a whole number of bytes.
- bit_order#
Return the bit-order interpretation setting for this view.
- byte_order#
Return the byte-order interpretation setting for this view.
- bytes#
Return the viewed bits as bytes.
Equivalent to using
to_bytes().
- le#
Return a little-endian byte-order view.
Equivalent to
view(byte_order=Endianness.Little).The view length must be a whole number of bytes.
- lsb0#
Return an LSB0 bit-order view.
BitOrder.Lsb0means that field labels are counted from the least significant bit of each byte. The view length must be a whole number of bytes.Equivalent to
view(bit_order=BitOrder.Lsb0).
- msb0#
Return an MSB0 bit-order view.
BitOrder.Msb0means that field labels are counted from the most significant bit of each byte. This is the default bit order.Equivalent to
view(bit_order=BitOrder.Msb0).
MutableView#
- class MutableView#
A live mutable view of a
Mutibswith different interpretation settings.MutableViewrecords how operations such as integer conversion, byte conversion and field extraction should interpret the source bits. UnlikeView, it keeps a live reference to the sourceMutibs.Assigning through
u,iorfmutates the sourceMutibswithout changing its length.- field(a, b)#
Extract a field using inclusive bit labels.
aandbmust be zero or positive bit labels. The two endpoints are inclusive and may be provided in either order. The returnedMutableViewis a live view onto the selected source bits.
- from_indices()#
Create a live mutable view from source bit positions.
indicesmay be arangeor any iterable of integers. It maps each viewed bit to a physical bit position in the sourceMutibs.This is a low-level reconstruction API. Use
field()for normal specification-labelled fields.>>> m = Mutibs('0x00') >>> view = MutableView.from_indices(m, range(0, 8, 2)) >>> view.bin = '1111' >>> m.bin '10101010'
- to_bin()#
Return the viewed bits as a binary string.
- to_bytes()#
Return the viewed bits as bytes.
- to_f()#
Interpret the viewed bits as an IEEE floating point value.
- to_hex()#
Return the viewed bits as a hexadecimal string.
- to_i()#
Interpret the viewed bits as a signed integer.
- to_oct()#
Return the viewed bits as an octal string.
- to_u()#
Interpret the viewed bits as an unsigned integer.
- view(byte_order=None, bit_order=None)#
Return a mutable view with updated interpretation settings.
Any setting left as
Nonekeeps its current value.
- write_bin(s, /)#
Write the viewed bits from a binary string without changing the view length.
- write_bytes(data, /)#
Write the viewed bits from a bytes-like object without changing the view length.
- write_f(f, /)#
Write the viewed bits from a floating point number without changing the source length.
- write_hex(s, /)#
Write the viewed bits from a hexadecimal string without changing the view length.
- write_i(i, /)#
Write the viewed bits from a signed integer without changing the source length.
- write_oct(s, /)#
Write the viewed bits from an octal string without changing the view length.
- write_u(u, /)#
Write the viewed bits from an unsigned integer without changing the source length.
- be#
Return a big-endian byte-order mutable view.
- bin#
Return the viewed bits as a binary string.
- bit_order#
Return the bit-order interpretation setting for this mutable view.
- byte_order#
Return the byte-order interpretation setting for this mutable view.
- bytes#
Return the viewed bits as bytes.
- f#
Interpret the viewed bits as an IEEE floating point value.
- hex#
Return the viewed bits as a hexadecimal string.
- i#
Interpret the viewed bits as a signed integer.
- le#
Return a little-endian byte-order mutable view.
- lsb0#
Return an LSB0 bit-order mutable view.
- msb0#
Return an MSB0 bit-order mutable view.
- oct#
Return the viewed bits as an octal string.
- u#
Interpret the viewed bits as an unsigned integer.