Skip to main content

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 packer import Int8, Pack
from dataclasses import dataclass

@packable
@dataclass
class SimpleStruct:
    test1: Pack[Int8]
    test2: Pack[Int8]

test = SimpleStruct(1, 2)
test.pack() # \x01\x02

Creating & Using custom types

from packer import TypeDescriptor, packable, Pack, OptionalPack
from dataclasses import dataclass

class LengthPrefixedStr(TypeDescriptor):
    __data_size__: int = 2

    @classmethod
    def pack(cls, val: str) -> bytes:
        return int.to_bytes(len((enc := val.encode())), 2, "little") + enc

    @classmethod
    def unpack(cls, data: bytearray) -> tuple[int, str]:
        str_len = int.from_bytes(data[:2], "little")
        return str_len + 2, data[2 : 2 + str_len].decode()


@packable
@dataclass
class CustomTypesStruct:
    test1: Pack[LengthPrefixedStr]
    test2: OptionalPack[LengthPrefixedStr] = None

test = CustomTypesStruct("hi")
test.pack() # b"\x02\x00hi"

test.test2 = "hi2"
test.pack() #b"\x02\x00hi\x03\x00hi2"

Notes

If you're going to use this with a dataclass then be prepared to lose object attr typehints. A simple workaround is to declare object attributes with the types like the following:

@packable
@dataclass
class SimpleStruct:
    id: Pack[Int32] = 0
    val: OptionalPack[Float] = None

    def __post_init__(self) -> None:
        self.id: int
        self.val: float | None

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.1.0.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

packer.typed-0.1.0-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file packer_typed-0.1.0.tar.gz.

File metadata

  • Download URL: packer_typed-0.1.0.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for packer_typed-0.1.0.tar.gz
Algorithm Hash digest
SHA256 607bad634b73f924a44f6ee821a330bdbc743f02d6bc84afeb7dca58b1c3e237
MD5 397684f9e6f97f1d9670e53fc99566e7
BLAKE2b-256 ff69ec8240b1c06efb2dcf0d366631e96eb0647d62b0ca07531d76560e6814da

See more details on using hashes here.

File details

Details for the file packer.typed-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for packer.typed-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 f3556e6a93b415207208f0afef55f56f4c850b476dfb57672f74eddca6478c47
MD5 09599657e5081235d5dbc6e55f09cabb
BLAKE2b-256 462265cb123b6a0edc91cd15decbe40f25f5a2560089a2191d2340cce1b6fb1c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page