Skip to main content

struct serialization/deserialization with typing

Project description


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


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

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')



Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Files for typestruct, version 0.1.1
Filename, size File type Python version Upload date Hashes
Filename, size typestruct-0.1.1-py3-none-any.whl (3.9 kB) File type Wheel Python version py3 Upload date Hashes View
Filename, size typestruct-0.1.1.tar.gz (3.0 kB) File type Source Python version None Upload date Hashes View

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring DigiCert DigiCert EV certificate Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page