bpack.descriptors module
Descriptors for binary records.
- class bpack.descriptors.BinFieldDescriptor(type: Optional[Type] = None, size: Optional[int] = None, offset: Optional[int] = None, signed: Optional[bool] = None, repeat: Optional[int] = None)[source]
Bases:
object
Descriptor for bpack fields.
- property total_size
Total size in bytes of the field (considering all item).
- type: Optional[Type] = None
- class bpack.descriptors.Field(default, default_factory, init, repr, hash, compare, metadata)[source]
Bases:
object
- compare
- default
- default_factory
- hash
- init
- metadata
- name
- repr
- type
- bpack.descriptors.asdict(obj, *, dict_factory=<class 'dict'>) dict [source]
Return the fields of a record as a new dictionary.
The returned dictionary maps field names to field values.
If given, ‘dict_factory’ will be used instead of built-in dict. The function applies recursively to field values that are dataclass instances. This will also look into built-in containers: tuples, lists, and dicts.
- bpack.descriptors.astuple(obj, *, tuple_factory=<class 'tuple'>) Sequence [source]
Return the fields of a dataclass instance as a new tuple of field values.
If given, ‘tuple_factory’ will be used instead of built-in tuple. The function applies recursively to field values that are dataclass instances. This will also look into built-in containers: tuples, lists, and dicts.
- bpack.descriptors.baseunits(obj) bpack.enums.EBaseUnits [source]
Return the base units of a binary record descriptor.
- bpack.descriptors.bitorder(obj) Optional[bpack.enums.EBitOrder] [source]
Return the bit order of a binary record descriptor.
- bpack.descriptors.byteorder(obj) bpack.enums.EByteOrder [source]
Return the byte order of a binary record descriptor (endianess).
- bpack.descriptors.calcsize(obj, units: Optional[bpack.enums.EBaseUnits] = None) int [source]
Return the size of the
obj
record.If the units parameter is not specified (default) then the returned size is expressed in the same base units of the descriptor.
- bpack.descriptors.descriptor(cls, *, size: Optional[int] = None, byteorder: Union[str, bpack.enums.EByteOrder] = EByteOrder.DEFAULT, bitorder: Optional[Union[str, bpack.enums.EBitOrder]] = None, baseunits: bpack.enums.EBaseUnits = EBaseUnits.BYTES, **kwargs)[source]
Class decorator to define descriptors for binary records.
It converts a dataclass into a descriptor object for binary records.
ensures that all fields are
bpack.descriptor.Field
descriptorsoffsets are automatically computed if necessary
consistency checks on offsets and sizes are performed
- Parameters
cls – class to be decorated
size – the size (expressed in base units) of the binary record
byteorder – the byte-order of the binary record
bitorder – the bit-order of the binary record (must be
None
if the base units are bytes). If set to none in bit-based records it is assumedbpack.enums.EBitOrder.DEFAULT
which corresponds tobpack.enums.EBitOrder.MSB
in all decoders currently implemented.baseunits – the base units (
bpack.enums.EBaseUnits.BITS
orbpack.enums.EBaseUnits.BYTES
) used to specify the binary record descriptor
It is also possible to specify as additional keyword arguments all the parameters accepted by
dataclasses.dataclass()
.
- bpack.descriptors.field(*, size: Optional[int] = None, offset: Optional[int] = None, signed: Optional[bool] = None, repeat: Optional[int] = None, metadata=None, **kwargs) dataclasses.Field [source]
Initialize a field descriptor.
Returned object is a
Field
instance with metadata properly initialized to describe the field of a binary record.
- bpack.descriptors.field_descriptors(descriptor, pad: bool = False) Iterable[bpack.descriptors.BinFieldDescriptor] [source]
Return the list of field descriptors for the input record descriptor.
Items are instances of the
BinFieldDescriptor
class describing characteristics of each field of the input binary record descriptor.If the
pad
parameter is set to True then also generate dummy field descriptors for padding elements necessary to take into account offsets between fields.
- bpack.descriptors.fields(obj) Sequence[dataclasses.Field] [source]
Return a tuple describing the fields of this descriptor.
- bpack.descriptors.get_field_descriptor(field: dataclasses.Field, validate: bool = True) bpack.descriptors.BinFieldDescriptor [source]
Return the field descriptor attached to a
Field
.
- bpack.descriptors.is_descriptor(obj) bool [source]
Return true if
obj
is a descriptor or a descriptor instance.
- bpack.descriptors.is_field(obj) bool [source]
Return true if an
obj
can be considered is a field descriptor.
- bpack.descriptors.set_field_descriptor(field: dataclasses.Field, descriptor: bpack.descriptors.BinFieldDescriptor, validate: bool = True) dataclasses.Field [source]
Set the field metadata according to the specified descriptor.