Skip to content

shustinm/hydration

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

93 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hydration

Tests PyPI


This software has been designed for you, with much joy, by Michael Shustin


You can find the complete documentation here


What is Hydration?

Hydration is a library used to define python objects that can be converted to (and from) bytes.

Installation

pip install hydration

Introduction

This guide assumes you are familiar with low-level primitive data types (like signed/unsigned int, int64, etc.)

Field

Fields are primitive objects that hold data, and can be converted to (and from) bytes:

>>> from hydration import *
>>> f = UInt16(1512)
>>> bytes(f)
b'\xe8\x05'
>>> UInt16().from_bytes(b'\xe8\x05')
UInt16(1512)

Struct

A struct is a composite of fields. To create a struct, Inherit from Struct:

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'
>>> print(MyStruct.from_bytes(b'\n\x03'))
MyStruct:
	a:	UInt8(10)
	b:	UInt8(3)

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 documentation.

Support

Want to report a bug? Request a feature? Please do so here

Maintainers

About

Define python objects that can be safely converted to (and from) bytes.

Topics

Resources

License

Code of conduct

Stars

Watchers

Forks

Packages

No packages published

Contributors 2

  •  
  •  

Languages