https://github.com/frankurcrazy/fpack
A simple pure-python message (de)serializer for fun.
https://github.com/frankurcrazy/fpack
deserializer message packer python serializer struct unpacker
Last synced: 5 months ago
JSON representation
A simple pure-python message (de)serializer for fun.
- Host: GitHub
- URL: https://github.com/frankurcrazy/fpack
- Owner: frankurcrazy
- License: bsd-3-clause
- Created: 2020-08-13T02:34:32.000Z (almost 6 years ago)
- Default Branch: main
- Last Pushed: 2023-02-23T18:14:44.000Z (over 3 years ago)
- Last Synced: 2025-09-18T04:27:59.076Z (9 months ago)
- Topics: deserializer, message, packer, python, serializer, struct, unpacker
- Language: Python
- Homepage:
- Size: 48.8 KB
- Stars: 2
- Watchers: 1
- Forks: 0
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# fpack
[](https://badge.fury.io/py/fpack)
[](https://travis-ci.com/frankurcrazy/fpack)
[](https://coveralls.io/github/frankurcrazy/fpack?branch=master)
***fpack*** is a simple message (de)serializer created for fun and educational purpose.
fpack hasn't been widely deployed, so use it at your own risk.
## Requirements
- python >= 3.7
## Installation
### Install with pip
```bash
pip install fpack
```
### Download latest version from git
```bash
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.
### Primitive types
***fpack*** supports primitive types:
- Uint8
- Uint16
- Uint32
- Uint64
- Int8
- Int16
- Int32
- Int64
- Bytes
- String
### Message declaration
```python
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
```python
>>> helloMsg = Hello()
>>> helloMsg.MsgID = 100
>>> helloMsg.Greetings = "Helloworld!"
>>> helloMsg
>>> 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`:
```python
# using the byte-stream from previous example
>>> decodedMsg, decodedLength = Hello.from_bytes(b'd\x00\x0bHelloworld!')
>>> decodedMsg
```
Decode with instance method `unpack`:
```python
>>> decodedMsg = Hello()
>>> decodedMsg.unpack(b'd\x00\x0bHelloworld!')
16
>>> decodedMsg
```
### Nested message (from 0.0.5 and beyond)
Nested message is supported.
Declaring an nested message:
```python
import fpack
class MailHeader(fpack.Message):
Fields = [
fpack.field_factory("Subject", fpack.String),
fpack.field_factory("From", fpack.String),
fpack.field_factory("To", fpack.String),
]
class MailBody(fpack.Message):
Fields = [
fpack.field_factory("Body", fpack.String),
fpack.field_factory("Signature", fpack.String),
]
class Mail(fpack.Message):
Fields = [
fpack.field_factory("Header", MailHeader),
fpack.field_factory("Body", MailBody),
]
>>> mail = Mail()
>>> header = mail.Header
>>> header.Subject = "this is a mail"
>>> header.From = "John Doe"
>>> header.To = "Jane Doe"
>>> body = mail.Body
>>> body.Text = "mail body"
>>> body.Signature = "by John doe"
>>> mail
Body=>
>>> mail.pack()
b'\x00\x0ethis is a mail\x00\x08John Doe\x00\x08Jane Doe\x00\tmail body\x00\x0bby John doe'
```
### Array (from 1.0.0 and beyond)
Array field is supported and can be created via ```array_field_factory(name, type)```.
```python
import fpack
class Item(fpack.Message):
Fields = [
fpack.field_factory("Name", fpack.String),
fpack.field_factory("Price", fpack.Uint32),
]
class Catalog(fpack.Message):
Fields = [
fpack.field_factory("CatalogID", fpack.Uint8),
fpack.array_field_factory("Items", Item),
]
items = [
Item(Name="Camera", Price=10),
Item(Name="Computer", Price=12),
Item(Name="Dildo", Price=5),
]
>>> catalog = Catalog(CatalogID=1, Items=items)
,,]>>
>>> catalog.pack()
b'\x01\x00\x03\x00\x06Camera\x00\x00\x00\n\x00\x08Computer\x00\x00\x00\x0c\x00\x05Dildo\x00\x00\x00\x05'
```
## License
BSD