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.
See also
bpack.filed()
for a description of the attributes.- property total_size
Total size in bytes of the field (considering all item).
- 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 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) Optional[EBitOrder] [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: Optional[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, EByteOrder] = EByteOrder.DEFAULT, bitorder: Optional[Union[str, EBitOrder]] = 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.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) Field [source]
Initialize a field descriptor.
Returned object is a
Field
instance with metadata properly initialized to describe the field of a binary record.- Parameters:
size – int size of the field in
EBaseUnits
offset – 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
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[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
obj
is a descriptor or a descriptor instance.