Package to convert arbitrary python objects into DTOs ready for serialization and validation.
Project description
A fork of awesome R2DTO by @nickswebsite.
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'}
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
beerializer-0.1.5.tar.gz
(4.7 kB
view hashes)
Built Distribution
Close
Hashes for beerializer-0.1.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | f441d4fd4c7bbd3fdda6a97ebd34a58ca360ca8f34967bc0582a60859b4f2236 |
|
MD5 | 3750847cdb086b76e055a06ef3fa5b1a |
|
BLAKE2b-256 | 9b64b88a6e281f181afd3ba0f54bf5dec00e74ffe30504d75a13701c4cc5b1dd |