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 Pypi status Test status

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[float32]

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

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

packer_typed-0.2.7-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: packer_typed-0.2.7.tar.gz
  • Upload date:
  • Size: 9.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for packer_typed-0.2.7.tar.gz
Algorithm Hash digest
SHA256 4ef7a1e4b57c3a3efa56df3f824c0808dc40af16d79dc0f4acc2c5767788994c
MD5 5a4f52c859c194eb05a1be1d2f425e1e
BLAKE2b-256 a0772b69815f005aa910a5766359e124060a85d2f40acf2ab60d267ac7bf4bdd

See more details on using hashes here.

File details

Details for the file packer_typed-0.2.7-py3-none-any.whl.

File metadata

  • Download URL: packer_typed-0.2.7-py3-none-any.whl
  • Upload date:
  • Size: 9.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.2

File hashes

Hashes for packer_typed-0.2.7-py3-none-any.whl
Algorithm Hash digest
SHA256 fdca04872f3a16d09c65629166f9567e191bf6d7e680ef830ba4bab2f5a5a358
MD5 20402e0b906658aa61d55c2ce8f507d8
BLAKE2b-256 3aae018d73e99822a0dd3f0bbd8e6595ab620d6d5b1d8b08da60c1d7473781d3

See more details on using hashes here.

Supported by

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