Provides easy interface for serialization of arbitrary Python objects into dicts
Switch branches/tags
Nothing to show
Clone or download
Pull request Compare This branch is 11 commits ahead, 35 commits behind nickswebsite:master.
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
beerializer
docs
.gitignore
LICENSE
README.rst
requirements.txt
runtime.txt
setup.py
tox.ini

README.rst

Beerializer

Documentation PyPI

A fork of awesome R2DTO by nickswebsite.

Beerializer provides easy interface for transformation and validation of arbitrary python objects into DTOs suitable for receiving from and delivering to other services.

Quick Start

Let's start by creating a simple model class:

class Simpson(object):
    def __init__(self):
        self.first_name = ""
        self.last_name = ""

    def __str__(self):
        return self.first_name + " " + self.last_name

To create a serializer, we need to map attributes to fields of our DTO:

class SimpsonSerializer(Serializer):
    class Meta:
        model = Simpson

    first_name = fields.StringField(name="firstName")
    last_name = fields.StringField(name="lastName")

When you get a payload that requires one of these serializers, call Serializer.load(data).

>>> data = {
...     "firstName": "Homer",
...     "lastName": "Simpson",
... }
>>> s = SimpsonSerializer.load(data)
>>> s
<class '__main__.Simpson'>
>>> str(s)
'Homer Simpson'

To go the other way. Pass the object you want to transfer into the dump method:

>>> homer = Simpson()
>>> homer.first_name = "Homer"
>>> homer.last_name = "Simpson"
>>> s = SimpsonSerializer.dump(homer)
>>> s
{'firstName': 'Homer', 'lastName': 'Simpson'}

Ready for the journey? Install it, then check out the docs:

$ pip install -U beerializer