BINARY PACKER
Light and fast packer python dataclasses to bytes
Install
How it works
It's a wrap! A wrap over stdlib struct
So it converts values into bytes using struct.pack().
But it does not save keys and values' types in bytes, so be sure about how you configure packer for packing and unpacking.
Why and when should it be used?
We all like to use json/xml/pickle and other popular libraries for dumping data, but sometimes they are too slow or result's size ot too large.
This library works much faster than other packers like json and makes result a lot more compact.
Example of Usage
import uuid
from typing import Optional
from dataclasses import dataclass
from binary_packer import PackerFactory, FieldStruct
@dataclass
class Person:
id: uuid.UUID
name: Optional[str] = None # max length is 20 bytes
age: Optional[int] = None
person = Person(id=uuid.uuid4(), name='vaschevsky', age=33)
factory = PackerFactory(
Person,
id=FieldStruct[uuid.UUID, bytes](
'16s', # '16s' is for bytearray length of 16, uuid as bytearray also 16 bytes
encoder=lambda uid: uid.bytes,
decoder=lambda uid_as_bytes: uuid.UUID(bytes=uid_as_bytes),
),
name=FieldStruct[str, bytes](
'20s', # '20s' is for bytearray length of 20, each byte is ascii char
encoder=lambda name: name.encode(),
decoder=lambda name_as_bytes: name_as_bytes.decode().strip('\x00'),
),
age=FieldStruct[int, int](
'B', # 'B' is for unsigned tiny int (1 byte) => age can be any value from 0 to 255,
# no need for custom encoder/decoder
)
)
packer_1 = factory.make_packer('id', 'name', 'age')
packer_2 = factory.make_packer('id', 'name')
packer_3 = factory.make_packer('id')
for packer in (packer_1, packer_2, packer_3):
data_as_bytes = packer.pack(person)
print(f'{len(data_as_bytes)=}')
unpacked_person = packer.unpack(data_as_bytes)
print(f'{unpacked_person=}')
# will be printed:
# len(data_as_bytes)=37
# unpacked_person=Person(id=UUID('a72decb7-7f9e-497b-ac91-692e316a7580'), name='vaschevsky', age=33)
# len(data_as_bytes)=36
# unpacked_person=Person(id=UUID('a72decb7-7f9e-497b-ac91-692e316a7580'), name='vaschevsky', age=None)
# len(data_as_bytes)=16
# unpacked_person=Person(id=UUID('a72decb7-7f9e-497b-ac91-692e316a7580'), name=None, age=None)
Metadata
Release files for binary-packer 1.0.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| binary_packer-1.0.1.tar.gz | 5.4 kB | Without provenance | Details for binary_packer-1.0.1.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| binary_packer-1.0.1-py3-none-any.whl | Python 3 | none | any | Details for binary_packer-1.0.1-py3-none-any.whl |
Total release size: 11.0 kB
Release files / binary_packer-1.0.1.tar.gz
| Download URL | binary_packer-1.0.1.tar.gz |
|---|---|
| Size | 5.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
d72af2dc6de8653d6f9bddfb0741589db9322c0ad1199769f70f60c9bfd019ca
|
|
BLAKE2b-256 checksum How to use checksums |
3a6916e53f8e0c5cfb24adb47b627928b6e1119f4a5b1f830f650971ddcf0d44
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Release files / binary_packer-1.0.1-py3-none-any.whl
| Download URL | binary_packer-1.0.1-py3-none-any.whl |
|---|---|
| Size | 5.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
60063d4c32c6c8e6de35c4aa5f7d5d23ba099934a620b76d9083939722409f2c
|
|
BLAKE2b-256 checksum How to use checksums |
20c87e79c3e27f5ef14408159043af43adfc6ac790bdc56bb0cb4fca4b278366
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/4.0.1 CPython/3.9.13
|
Security for binary-packer 1.0.1
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.