Skip to main content

A simple binary serialization/deserialization library for Python

Project description

(Py)Bran

(Py)Bran - Boring, plain and simple python binary serialization/deserialization library

Its called (Py)Bran because Bran was taken. It does (cereal)ization. Yes. Also because Bran (the ceareal) is boring like this library.

MIT license PyPI Version PyPI Versions PyPI Status

CI Status security: bandit PyLint Score Coverage %

Installing

pip install pybran

Using

Registering your Schemas

In order for Bran to work with complex objects, it needs to know the object Schema. Schemas can be defined in two ways:

  • Automatically with Decorators
  • Manually

Automatically with Decorators

from pybran.decorators import schema, field

@schema
class MyClass:
    test = field(1)
    other = field(MyClass2())

@schema
class MyClass2:
    test2 = field(2)

Manually

from pybran.decorators import register_class

class MyClass:
    test = 1
    other = MyClass2()

class MyClass2:
    test2 = 2
    
register_class(MyClass2, {"test2": MyClass2.test2})
register_class(MyClass, {"test": MyClass.test, "other": MyClass.other})

Serializing

Registering Serializers

Bespoke Serializers can be registered with the Loader instance using the Loader.register method.

from pybran.loaders import Loader
from pybran.serializers import DefaultSerializer

loader = Loader()

# Register the type MyClass to use the serializer DefaultSerializer 
loader.register(MyClass, DefaultSerializer)

Bran automatically comes configured with a serializer registry that maps the following types:

from pybran.serializers import *

{
    bool: BoolSerializer,
    int: IntSerializer,
    float: FloatSerializer,
    str: StringSerializer,
    set: SetSerializer,
    dict: MappingSerializer,
    list: ArraySerializer,
    tuple: ArraySerializer
}

When importing the Loader, these are already preconfigured. If you wish to specify your own serializer registry to use upon instantiation, then you can override it when creating a new Loader object.

from pybran.loaders import Loader
from pybran.exceptions import BranSerializerException

serializers = {
    int: MyCustomSerializer
}

loader = Loader(serializers)

# Using MyCustomSerializer
try:
    serialized = loader.serialize(1)
except BranSerializerException as e:
    print(e)

Serializing Directly

Serializing an object is as simple as calling the Loader.serialize method

Any serialization errors will raise a BranSerializerException

from pybran.loaders import Loader
from pybran.exceptions import BranSerializerException

loader = Loader()

try:
    serialized = loader.serialize(1)
except BranSerializerException as e:
    print(e)

Deserializing Directly

Deserializing is just as simple

Any deserialization errors will raise a BranSerializerException

import io

from pybran.loaders import Loader
from pybran.exceptions import BranSerializerException

loader = Loader()

serialized = b'\x00\x00\x00\x00'
try:
    myint = loader.deserialize(io.BytesIO(serialized), int)
except BranSerializerException as e:
    print(e)

Serializing with Automatic Type Tagging

When serializing/deserializing a type, type information can be configured to be stored so the deserializer automatically knows which object to deserialize to.

This can be done by passing the tagging = True kwarg when calling serialize/deserialize

import io

from pybran.serializers import DefaultSerializer
from pybran.loaders import Loader

loader = Loader()
loader.register(MyClass, DefaultSerializer)

myobj = MyClass()

serialized = loader.serialize(myobj, tagging=True)
myobj_deserialized = loader.deserialize(io.BytesIO(serialized), tagging=True)

Reading from a file

The Loader is also capable of reading and writing to a file

Can raise a BranFileException

from pybran.loaders import Loader
from pybran.serializers import DefaultSerializer
from pybran.exceptions import BranSerializerException, BranFileException

loader = Loader()
loader.register(MyClass, DefaultSerializer)

try:
    myobject = loader.read("path/my_file", MyClass)
except (BranSerializerException, BranFileException) as e:
    print(e)

Writing to a file

Can raise a BranFileException

from pybran.loaders import Loader
from pybran.serializers import DefaultSerializer
from pybran.exceptions import BranSerializerException, BranFileException

loader = Loader()
loader.register(MyClass, DefaultSerializer)

myobject = MyClass()

try:
    loader.write("path/my_file", myobject)
except (BranSerializerException, BranFileException) as e:
    print(e)

Writing a Custom Serializer

Writing a custom serializer can be done by extending the Serializer class, and registering the class with your Loader instance.

from pybran.serializers import Serializer
from pybran.loaders import Loader

class MyCustomSerializer(Serializer):
    def serialize(self, loader, obj, **kwargs):
        # Implement custom serialization logic here

    def deserialize(self, loader, cls, data, **kwargs):
        # Implement custom deserialization logic here

loader = Loader()
loader.register(MyClass, MyCustomSerializer)

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

pybran-0.0.4.tar.gz (11.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pybran-0.0.4-py3-none-any.whl (10.8 kB view details)

Uploaded Python 3

File details

Details for the file pybran-0.0.4.tar.gz.

File metadata

  • Download URL: pybran-0.0.4.tar.gz
  • Upload date:
  • Size: 11.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for pybran-0.0.4.tar.gz
Algorithm Hash digest
SHA256 c6cc3ec8af9ca6abb0ed061da5cba3f1978090919894b0edaae2366ccbb5c299
MD5 0896a8b2eaf0303315ee9589cdefa049
BLAKE2b-256 d63a302eca63fb87855ae95919521a3c949a3af7294da607e22b42f197c639df

See more details on using hashes here.

File details

Details for the file pybran-0.0.4-py3-none-any.whl.

File metadata

  • Download URL: pybran-0.0.4-py3-none-any.whl
  • Upload date:
  • Size: 10.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.2 importlib_metadata/4.8.1 pkginfo/1.7.1 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.2 CPython/3.9.5

File hashes

Hashes for pybran-0.0.4-py3-none-any.whl
Algorithm Hash digest
SHA256 ba772fea28c1dc8a24d12d0331eea22a8aed0d11276bbe16cebc87757d303ac8
MD5 e7bf248745550db17e5c94f12da39484
BLAKE2b-256 ea3b25f68b1d3e2dac36a57a90b28f73958f8bae8f43081640acb2d1df6f52b3

See more details on using hashes here.

Supported by

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