A easier way to use struct
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'
Variant 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
Release history Release notifications | RSS feed
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.0.tar.gz
(3.0 kB
view hashes)
Built Distribution
Close
Hashes for typestruct-0.1.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 024ba59401ea118df89eb21c7f9794d0af11e40c05d050041d171cb39317113e |
|
MD5 | 152391ebeb8f0ff0ad7a90d093a7a9cd |
|
BLAKE2b-256 | 669b31b7e5e822a626f34772f9c6cac50c3d2e83000455993ae29039f26c53e0 |