A modern library that simplifies packing and unpacking to a whole other level.
Project description
packer.typed
A modern library that simplifies packing and unpacking to a whole other level.
Usage
Basic Usage
from dataclasses import dataclass
from packer import *
@packable
@dataclass
class SimpleStruct:
int32_member: Pack[int32]
int8_member: Pack[int8]
float_member: OptionalPack[float]
s = SimpleStruct(3, 2, None)
s.pack() # bytearray(b'\x03\x00\x00\x00\x02')
s.unpack(bytearray(b'\x01\x00\x00\x00\x04'))
# s.int32_member == 1
# s.int8_member == 4
# s.float_member == None
Custom types
from enum import IntEnum
from dataclasses import dataclass
from packer import *
from packer.protocols import TypeDescriptor
class FileTypes(IntEnum):
UNK = 0
TXT = 1
BIN = 2
class _FileType(TypeDescriptor):
_size: int = 1
@classmethod
def pack(cls, val: FileTypes) -> FileTypes:
return val.to_bytes(1, "little")
@classmethod
def unpack(cls, data: bytes) -> FileTypes:
return FileTypes(int.from_bytes(data[:cls._size], "little"))
FileType = _FileType | FileTypes # to get the typehint of FileTypes back
@packable
@dataclass
class File:
file_type: Pack[FileType]
file_data: Pack[AllData]
file = File(FileTypes.BIN, bytearray([3]*5))
file.pack() # bytearray(b'\x02\x03\x03\x03\x03\x03')
file.unpack(bytearray(b'\x01\x03\x03\x03\x03\x03hi!'))
# file.file_type == FileTypes.TXT
# file.data == b"\x03\x03\x03\x03\x03hi!"
Packing a packable
Nested packable attributes must have default values to ensure proper instantiation
@packable
@dataclass
class Header:
data_type: Pack[int8] = None
data_size: Pack[int32] = None
@packable
@dataclass
class Packet:
header: Pack[Header]
data: Pack[AllData]
packet = Packet(Header(1, 5), b"hello")
packet.pack() # bytearray(b"\x01\x05\x00\x00\x00hello")
packet.unpack(bytearray(b"\x04\x02\x00\x00\x00hi"))
# packet.header.data_type == 4
# packet.header.data_size == 2
# packet.data == hi
Project details
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
packer_typed-0.2.0.tar.gz
(8.8 kB
view details)
Built Distribution
File details
Details for the file packer_typed-0.2.0.tar.gz
.
File metadata
- Download URL: packer_typed-0.2.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 6ac35b07cd309e2cec8be71445af1199c6e38c2a15423922e6855b2a89658dc3 |
|
MD5 | 499965abc36ab0eef55b2668995f4cd8 |
|
BLAKE2b-256 | d6fb371eb900a10e4f802891fbb76c56635ded261aaa93d27f82ac307631e647 |
File details
Details for the file packer.typed-0.2.0-py3-none-any.whl
.
File metadata
- Download URL: packer.typed-0.2.0-py3-none-any.whl
- Upload date:
- Size: 9.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/5.1.1 CPython/3.12.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 24629b4a0e36405bb069379104e6ca9066d7ac4c35606b49b6d629fa7c5361d7 |
|
MD5 | 370bce1c5cc1cb92044d8c388da6f553 |
|
BLAKE2b-256 | da26e9d9eebc7685b0ab6a437dbecde5499b91aed01a809e2dd79a15d41e9063 |