Skip to main content

fpack is a simple message (de)seriealizer in pure python

Project description

fpack

fpack is a simple message (de)serializer created for fun and educational purpose. fpack hasn't been widely deployed, and neither has it been fully tested. So use it at your own risk.

Requirements

  • python >= 3.7

Installation

Install with pip

pip install fpack

Download latest version from git

git clone https://github.com/frankurcrazy/fpack
cd fpack && python setup.py install

Guide

The following shows an example that uses fpack to declare and pack/unpack a message.

Message declaration

import fpack

# Declare a Hello message, with MsgID (`Uint8`) and Greeting (`String`) field.
class Hello(fpack.Message):
    Fields = [
        fpack.field_factory("MsgID", fpack.Uint8),
        fpack.field_factory("Greeting", fpack.String), 
    ]

Message serialization

>>> helloMsg = Hello()
>>> helloMsg.MsgID = 100
>>> helloMsg.Greetings = "Helloworld!"
>>> helloMsg
<Hello MsgID=100 Greetings="Helloworld!">
>>> helloMsg.pack()
b'd\x00\x0bHelloworld!'

Message deserialization

Message deserialization can be done by calling class method from_bytes, or by calling instance method unpack

Decode with class method from_bytes:

>>> decodedMsg, decodedLength = Hello.from_bytes(b'd\x00\x0bHelloworld!')   # using the byte-stream from previous example
>>> decodedMsg
<Hello MsgID=100 Greetings=Helloworld!>

Decode with instance method unpack:

>>> decodedMsg = Hello()
>>> decodedMsg.unpack(b'd\x00\x0bHelloworld!')
16
>>> decodedMsg
<Hello MsgID=100 Greetings="Helloworld!">

License

BSD

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

fpack-0.0.5.tar.gz (3.7 kB view hashes)

Uploaded Source

Built Distribution

fpack-0.0.5-py3-none-any.whl (5.3 kB view hashes)

Uploaded Python 3

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