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

Uploaded Source

Built Distribution

packer.typed-0.1.3-py3-none-any.whl (7.5 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: packer_typed-0.1.3.tar.gz
  • Upload date:
  • Size: 7.2 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.3.tar.gz
Algorithm Hash digest
SHA256 6eb0291d4ab2b6db6650decd16c06595b07e9b4f8882366436faecb419aded11
MD5 cc2182bb053cbbd25fa0ba0338f2abfa
BLAKE2b-256 9f2d871d1744869603d5288eef7c965d5a2b3bd771aba8f130d27c4e9f156325

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for packer.typed-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 56fd67d3639beb619b90891fa2f280ee53b37e7be8eb0ac446e2d9ceeb4f3cd7
MD5 2910fb5a8d94662da9c47b846b699572
BLAKE2b-256 7fac49d63983663675ddcb6465c7247f53ea6087868f869864e377882f76fed0

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