bpack.descriptors module
Descriptors for binary records.
- class bpack.descriptors.BinFieldDescriptor(type: Type | None = None, size: int | None = None, offset: int | None = None, signed: bool | None = None, repeat: int | None = None)[source]
Bases:
objectDescriptor for bpack fields.
See also
bpack.filed()for a description of the attributes.- offset: int | None = None
- repeat: int | None = None
number of items
- signed: bool | None = None
- size: int | None = None
item size
- property total_size
Total size in bytes of the field (considering all item).
- type: Type | None = None
- class bpack.descriptors.Field(default, default_factory, init, repr, hash, compare, metadata, kw_only)[source]
Bases:
object- compare
- default
- default_factory
- hash
- init
- kw_only
- 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 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) EBaseUnits[source]
Return the base units of a binary record descriptor.
- bpack.descriptors.bitorder(obj) EBitOrder | None[source]
Return the bit order of a binary record descriptor.
- bpack.descriptors.byteorder(obj) EByteOrder[source]
Return the byte order of a binary record descriptor (endianess).
- bpack.descriptors.calcsize(obj, units: EBaseUnits | None = None) int[source]
Return the size of the
objrecord.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: int | None = None, byteorder: str | EByteOrder = EByteOrder.DEFAULT, bitorder: str | EBitOrder | None = None, baseunits: 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.Fielddescriptorsoffsets 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
Noneif the base units are bytes). If set to none in bit-based records it is assumedbpack.enums.EBitOrder.DEFAULTwhich corresponds tobpack.enums.EBitOrder.MSBin all decoders currently implemented.baseunits – the base units (
bpack.enums.EBaseUnits.BITSorbpack.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: int | None = None, offset: int | None = None, signed: bool | None = None, repeat: int | None = None, metadata=None, **kwargs) Field[source]
Initialize a field descriptor.
Returned object is a
Fieldinstance with metadata properly initialized to describe the field of a binary record.- Parameters:
size – int size of the field in
EBaseUnitsoffset – int offset of the field w.r.t. the beginning of the record (expressed in
EBaseUnits)signed – bool True if an int field is signed, False otherwise. This parameter must not be specified for non int fields.
repeat – int length of the sequence for sequence fields, i.e. fields consisting in multiple items having the same data type. This parameter must not be specified if the data type is not a sequence type (e.g. List).
metadata – additional metadata to be attached the the field descriptor.
kwargs – additional keyword arguments for the
dataclasses.field()function.
- bpack.descriptors.field_descriptors(descriptor, pad: bool = False) Iterable[BinFieldDescriptor][source]
Return the list of field descriptors for the input record descriptor.
Items are instances of the
BinFieldDescriptorclass describing characteristics of each field of the input binary record descriptor.If the
padparameter 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[Field][source]
Return a tuple describing the fields of this descriptor.
- bpack.descriptors.get_field_descriptor(field: Field, validate: bool = True) BinFieldDescriptor[source]
Return the field descriptor attached to a
Field.
- bpack.descriptors.is_descriptor(obj) bool[source]
Return true if
objis a descriptor or a descriptor instance.
- bpack.descriptors.is_field(obj) bool[source]
Return true if an
objcan be considered is a field descriptor.
- bpack.descriptors.set_field_descriptor(field: Field, descriptor: BinFieldDescriptor, validate: bool = True) Field[source]
Set the field metadata according to the specified descriptor.