Release 0.9

pyserializer Documentation

Release 0.9.1 Joel James

January 30, 2018

1 pyserializer 2 Indices and tables

Contents

1 23

i

ii

CHAPTER 1

pyserializer

pyserializer is a simple Python serialization/deserialization library. It is an ORM agnostic library for converting Python objects to native Python datatypes, and vice versa.

Installation

pyserializer is available on PyPI, so to use it you can use pip: To install pyserializer, simply run: $ pip install pyserializer Alternatively, if you don't have setuptools installed, download it from PyPi and run: $ python setup.py install Also, you can get the source from GitHub and install it as above: $ git clone $ cd pyserializer $ python setup.py install Serialization A quick serialization example. Shows examples on how to define your serializer class and serialize a

python object to native datatype. Deserialization A quick deserialization example. Shows examples on how to define your deserializer class and

deserialize a native datatype to python object. Validation A quick validation example. Shows examples on how to define validators on your field and check errors

encountered diring validation process. Fields A list of currently supported fields. Custom Error Messages A quick example on how to set custom error messages on fields. Custom Fields A quick example on how you can create custom fields. Custom Validators A quick example on how you can create custom validators. API Reference The complete API documentation. Changelog A documentation for changelogs between versions.

1

pyserializer Documentation, Release 0.9.1

Contribute

Always looking for contributions, additions and improvements. The source is available on GitHub and contributions are always encouraged. To contribute, fork the project on GitHub and submit a pull request. If you have notices bugs or issues with the library, you can add it to the Issue Tracker. Install development requirements. It is highly recommended that you use a virtualenv, and activate the virtualenv before installing the requirements. Install project requirements: $ pip install -r requirements.txt Run tests with coverage: $ make unit_test Run test Using Tox (Runs the tests in different supported python interpreter): $ tox Run Lint: $ make lint Create a new local branch to submit a pull request: $ git checkout -b name-of-feature Commit your changes: $ git commit -m "Detailed commit message" Push up your changes: $ git push origin name-of-feature Submit a pull request. Before submitting a pull request, make sure pull request add the functionality, it is tested and docs are updated.

Changes

See the Changelog for a full list of changes to pyserializer.

Note: Always read changelog documentation before putting updates live in production.

Offline Documentation

Download the docs in pdf or epub formats for offline reading.

2

Chapter 1. pyserializer

pyserializer Documentation, Release 0.9.1

License

The project is licensed under the MIT license.

Serialization

Serializers allow Python objects to be converted to native Python datatypes. The serialized data can then be easily rendered to json.

Getting started If you haven't installed pyserializer, simply use pip to install it like so: $ pip install pyserializer

Example: Simple Serialization

Defining our serailizer

Let's start by creating a python object which we can use to demonstrate our serializer. Let's assume we have a user object: class User(object):

def __init__(self, email, username): self.email = email self.username = username

Now let's define a serializer which we can use to serialize data that corresponds to User object: from pyserializer.serializers import Serializer from pyserializer import fields class UserSerializer(Serializer):

email = fields.CharField() username = fields.CharField()

Serailize the object

Get the serialized data: users = [User(email='foo_1@', username='foo_1'), User(email='foo_2@', username='foo_2') serializer = UserSerializer(users, many=True) serializer.data # [OrderedDict([('email', 'foo_1@'), ('username', 'foo_1')]), OrderedDict([('email', 'foo_2@ba Get in json serialized format: import json json.dumps(serializer.data) # '[{"email": "foo_1@", "username": "foo_1"}, {"email": "foo_2@", "username": "foo_2"}]

1.5. License

3

pyserializer Documentation, Release 0.9.1

Example: Serialization With Meta Fields Defined

Defining our serailizer

Let's start by creating a python object which we can use to demonstrate our serializer. Let's assume we have a user object: class User(object):

def __init__(self, email, username): self.email = email self.username = username

Let's assume we only want to include the email field when serializing the User object. We can do this my defining a Meta class with fields property, within the serializer. Now let's define the serializer: from pyserializer.serializers import Serializer from pyserializer import fields

class UserSerializer(Serializer): email = fields.CharField() username = fields.CharField()

class Meta: fields = ( 'email', )

Serailize the object

Get the serialized data: users = [User(email='foo_1@', username='foo_1'), User(email='foo_2@', username='foo_2')

serializer = UserSerializer(users, many=True) serializer.data # [OrderedDict([('email', 'foo_1@')]), OrderedDict([('email', 'foo_2@')])]

Get in json serialized format: import json json.dumps(serializer.data) # '[{"email": "foo_1@"}, {"email": "foo_2@"}]'

Example: Serialization With Meta Exclude Defined

Defining our serailizer

Let's start by creating a python object which we can use to demonstrate our serializer. Let's assume we have a user object: class User(object):

def __init__(self, email, username): self.email = email self.username = username

4

Chapter 1. pyserializer

................
................

In order to avoid copyright disputes, this page is only a partial summary.

Google Online Preview   Download