A module for serialization of structs into binary data
Project description
Hydration
This software has been designed for you, with much joy, by Michael Shustin
What is Hydration?
Hydration is a library used to define python objects that can be converted to (and from) bytes.
Introduction
This guide assumes you are familiar with low-level primitive data types (like signed/unsigned int, int32, etc.)
Field
Fields are the primitive objects that hold data, and can be converted to (and from) bytes
>>> from hydration import *
>>> f = UInt16(1512)
>>> bytes(f)
b'\xe8\x05'
Struct
A struct is a composite of fields.
To create a struct, Inherit from Struct
to combine multiple Field
s:
from hydration import *
class MyStruct(Struct):
a = UInt8
b = UInt8(value=3) # You can set the default value
>>> st = MyStruct(a=10) # Structs can receive field values as keyword arguments
>>> print(st)
MyStruct
a: UInt8(10)
b: UInt8(3)
>>> bytes(st)
b'\n\x03'
Message
A message is a list-like composite of structs.
from hydration import *
class Header(Struct):
magic = UInt32(0xDEADBEEF)
class Body(Struct):
param1 = Float(2.875)
param2 = Int8(-128)
>>> msg = Header() / Body() # Create a message by using the division operator on structs
>>> print(msg)
Header:
magic: UInt32(3735928559)
Body:
param1: Float(2.875)
param2: Int8(-128)
>>> bytes(msg)
b'\xef\xbe\xad\xde\x00\x008@\x80'
Advanced features
For more advanced usage, be sure to check the docs/
folder.
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
Built Distribution
Hashes for hydration-1.0.1-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | ac98a1ca67ddebebcca6297df22bec9c115fa823db703fc08dbc2acd27923511 |
|
MD5 | 6f99c1e168c945b31a3022c0a34af5b5 |
|
BLAKE2b-256 | 0d8b88d6ba88e315d988022e2f374cfea1f334d877dd01b1e28849421103d70d |