Skip to main content

A module used to define python objects that can be converted to (and from) bytes.

Project description

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

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

hydration-4.0.0.tar.gz (13.7 kB view details)

Uploaded Source

Built Distribution

hydration-4.0.0-py3-none-any.whl (27.6 kB view details)

Uploaded Python 3

File details

Details for the file hydration-4.0.0.tar.gz.

File metadata

  • Download URL: hydration-4.0.0.tar.gz
  • Upload date:
  • Size: 13.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7

File hashes

Hashes for hydration-4.0.0.tar.gz
Algorithm Hash digest
SHA256 5873af8d2854f94f4837ae9e0ba2c541ff72c6ec39bc725b5935e2c2ccaa88a5
MD5 fb2c2a3df1b5763dedc206a99eaf6bef
BLAKE2b-256 608eb60d41a0c3cd7bffe25f4991e2a2b7f15830f957ee580f9ca69d221cd3e1

See more details on using hashes here.

File details

Details for the file hydration-4.0.0-py3-none-any.whl.

File metadata

  • Download URL: hydration-4.0.0-py3-none-any.whl
  • Upload date:
  • Size: 27.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.3.0 pkginfo/1.7.0 requests/2.25.1 setuptools/49.2.1 requests-toolbelt/0.9.1 tqdm/4.58.0 CPython/3.8.7

File hashes

Hashes for hydration-4.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1c153b9e0864303359a32d407cb43b36f603dd378215b2da1230eb5c43af2551
MD5 f4d007203916bdb50d8a890f394ae23a
BLAKE2b-256 71de1202c689ed1e1e1e53a91e82a03b6c1bb5a7f87663ec36d2882acefb754c

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page