Decimal to bytes and bytes to decimal transform.
[plum.decimal] Module Reference¶
The plum.decimal
module provides the DecimalX
transform for converting
an fixed point number into bytes and bytes into an fixed point number with
given precision. This reference page demonstrates creating and using a DecimalX
transform as well as provides API details.
Note
When packing, the given value may be rounded to fit into the given precision.
The examples shown on this page require the following setup:
>>> from plum.array import ArrayX
>>> from plum.decimal import DecimalX
>>> from plum.utilities import pack, unpack
Basic Use¶
The DecimalX
transform accepts the following arguments:
nbytes: format size in bytes (any positive integer) precision: number of digits of the fractional part byteorder: "big"
or"little"
(default)signed: True
orFalse
(default)name: transform name (for representations including dump format column)
For example:
>>> u16p1 = DecimalX(name='u16p1', nbytes=2, precision=1, byteorder='big', signed=False)
Use the transform to specify a format when using the pack()
and unpack()
utility
functions or when using other high level transforms:
>>> bindata = pack(value='25.8', fmt=u16p1)
>>> bindata.hex()
'0102'
>>>
>>> unpack(fmt=u16p1, buffer=bindata)
Decimal('25.8')
API Reference¶
-
class
plum.decimal.
DecimalX
(nbytes: int, precision: int, byteorder: Union[Literal[little], Literal[big]] = 'little', *, signed: bool = False, name: Optional[str] = None)¶ Decimal to bytes and bytes to decimal transform.
-
byteorder
¶ Byte order (“little” or “big”).
-
name
¶ Transform format name (for repr and dump “Format” column).
-
nbytes
¶ Transform format size in bytes.
-
precision
¶ Precision.
-
signed
¶ Signed decimal.
-
pack
(value: Any) → bytes¶ Pack value as formatted bytes.
Raises: PackError
if type error, value error, etc.
-
pack_and_dump
(value: Any) → Tuple[bytes, plum.dump.Dump]¶ Pack value as formatted bytes and produce bytes summary.
Raises: PackError
if type error, value error, etc.
-
unpack
(buffer: bytes) → Any¶ Unpack value from formatted bytes.
Raises: UnpackError
if insufficient bytes, excess bytes, or value error
-
unpack_and_dump
(buffer: bytes) → Tuple[Any, plum.dump.Dump]¶ Unpack value from bytes and produce packed bytes summary.
Raises: UnpackError
if insufficient bytes, excess bytes, or value error
-