Skip to main content

A modern library that simplifies packing and unpacking to a whole other level.

Project description

packer.typed

PyPI version Python versions

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

Uploaded Source

Built Distribution

packer.typed-0.1.4-py3-none-any.whl (7.6 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: packer_typed-0.1.4.tar.gz
  • Upload date:
  • Size: 7.4 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.4.tar.gz
Algorithm Hash digest
SHA256 fef4d1098e484f63da190fb5e3aa6f8bdec6c707c011d8c4c03260973fc6882b
MD5 c312f303d1bfe711e898b5b52482a81f
BLAKE2b-256 9b252706cdf5176047ab8ca312439ba5d723fd4ce01deb41f8b486275f01f93e

See more details on using hashes here.

File details

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

File metadata

File hashes

Hashes for packer.typed-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 d6048363819e3dcd4ab12622112449f890fd6699f4cf475e66b3890404476b0e
MD5 a78a973b5d5bd1641aadb19b62fea98f
BLAKE2b-256 b494b7ea3b40ccf1775771b6037d4212b31036d6206357c0a6daffcebf9e761c

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