[plum] Utility Reference: pack_and_dump()

plum.pack_and_dump(fmt, *args, **kwargs)

Pack values as bytes and produce bytes summary following a format.

Parameters:
  • fmt (Plum, tuple/list of Plum, or dict of Plum) – byte format of values
  • args (tuple) – packable values
  • kwargs – packable values
Returns:

bytes buffer, packed bytes summary

Return type:

bytearray, Dump

For example:

>>> from plum import pack_and_dump
>>> from plum.int.little import UInt8, UInt16
>>>
>>> buffer, dump = pack_and_dump(UInt8, 1)
>>> buffer
bytearray(b'')
>>> print(dump)
+--------+-------+-------+-------+
| Offset | Value | Bytes | Type  |
+--------+-------+-------+-------+
| 0      | 1     | 01    | UInt8 |
+--------+-------+-------+-------+
>>>
>>> buffer, dump = pack_and_dump((UInt8, UInt8), 1, 2)
>>> buffer
bytearray(b'')
>>> print(dump)
+--------+--------+-------+-------+-------+
| Offset | Access | Value | Bytes | Type  |
+--------+--------+-------+-------+-------+
| 0      | [0]    | 1     | 01    | UInt8 |
| 1      | [1]    | 2     | 02    | UInt8 |
+--------+--------+-------+-------+-------+
>>>
>>> buffer, dump = pack_and_dump({'a': UInt8, 'b': UInt8}, a=1, b=2)
>>> buffer
bytearray(b'')
>>> print(dump)
+--------+--------+-------+-------+-------+
| Offset | Access | Value | Bytes | Type  |
+--------+--------+-------+-------+-------+
| 0      | ['a']  | 1     | 01    | UInt8 |
| 1      | ['b']  | 2     | 02    | UInt8 |
+--------+--------+-------+-------+-------+