Skip to main content

struct serialization/deserialization with typing

Project description

typestruct

This is a Python 3 library amis to make the standard module struct easier to use.

Example

@dataclass
class ICMPPacket(Packet):
    ttype: uint8
    code: uint8
    checksum: uint16
    rest: Slice(4)


packet = ICMPPacket(1, 2, 3, b"abcd")
binary = packet.serialize()
print(binary)  # => b'\x01\x02\x03\x00abcd'

recovered = ICMPPacket.deserialize(binary)
print(recovered)  # => ICMPPacket(ttype=1, code=2, checksum=3, rest=b'abcd')

print(packet.serialize(endian=Endian.BIG_ENDIAN))  # => b'\x01\x02\x00\x03abcd'

Variable length

@dataclass
class TLV(Packet):
    tag: uint8
    length: uint8
    value: Varlength(lambda s: s.length)


tlv = TLV(tag=1, length=5, value=b"abcde")
binary = tlv.serialize()
print(binary)  # => b'\x01\x05abcde'

recovered = TLV.deserialize(binary)
print(recovered)  # => TLV(tag=1, length=5, value=b'abcde')

License

MIT

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

typestruct-0.1.1.tar.gz (3.0 kB view details)

Uploaded Source

Built Distribution

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

typestruct-0.1.1-py3-none-any.whl (3.9 kB view details)

Uploaded Python 3

File details

Details for the file typestruct-0.1.1.tar.gz.

File metadata

  • Download URL: typestruct-0.1.1.tar.gz
  • Upload date:
  • Size: 3.0 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for typestruct-0.1.1.tar.gz
Algorithm Hash digest
SHA256 5dc80eda4f6ec85ff737d5787d98cffc8b6e7d6a7f6da926f8879b14eb21404f
MD5 6a85b95be7e72eb6cdca931c48b2c0e0
BLAKE2b-256 deaa7196e84dfb27c0686eb9ae7e360de75ceddc2dffa01d5f5228ea04a2558b

See more details on using hashes here.

File details

Details for the file typestruct-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: typestruct-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 3.9 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.11.0 pkginfo/1.4.2 requests/2.19.1 setuptools/40.2.0 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for typestruct-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 215fd5f6b1dcc243b1acd5f7e9bffc9d2653678f3ef6a2c701152d40dc1c9010
MD5 9e66af2595a09c72b0df33d9bb5b3adb
BLAKE2b-256 cadfa34cf6f9b6208c030695562902a95356f17c2024e0c94a4b1d75fd319239

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