Skip to main content

ridiculously fast object serialization

Project description

Travis-CI Coveralls Documentation Status Downloads

serpy is a super simple object serialization framework built for speed. serpy serializes complex datatypes (Django Models, custom classes, …) to simple native types (dicts, lists, strings, …). The native types can easily be converted to JSON or any other format needed.

The goal of serpy is to be able to do this simply, reliably, and quickly. Since serializers are class based, they can be combined, extended and customized with very little code duplication. Compared to other popular Python serialization frameworks like marshmallow or Django Rest Framework Serializers serpy is at least an order of magnitude faster.

Source

Source at: https://github.com/clarkduvall/serpy

If you want a feature, send a pull request!

Documentation

Full documentation at: http://serpy.readthedocs.org/en/latest/

Installation

$ pip install serpy

Examples

Simple Example

import serpy

class Foo(object):
    """The object to be serialized."""
    y = 'hello'
    z = 9.5

    def __init__(self, x):
        self.x = x


class FooSerializer(serpy.Serializer):
    """The serializer schema definition."""
    # Use a Field subclass like IntField if you need more validation.
    x = serpy.IntField()
    y = serpy.Field()
    z = serpy.Field()

f = Foo(1)
FooSerializer(f).data
# {'x': 1, 'y': 'hello', 'z': 9.5}

fs = [Foo(i) for i in range(100)]
FooSerializer(fs, many=True).data
# [{'x': 0, 'y': 'hello', 'z': 9.5}, {'x': 1, 'y': 'hello', 'z': 9.5}, ...]

Nested Example

import serpy

class Nestee(object):
    """An object nested inside another object."""
    n = 'hi'


class Foo(object):
    x = 1
    nested = Nestee()


class NesteeSerializer(serpy.Serializer):
    n = serpy.Field()


class FooSerializer(serpy.Serializer):
    x = serpy.Field()
    # Use another serializer as a field.
    nested = NesteeSerializer()

f = Foo()
FooSerializer(f).data
# {'x': 1, 'nested': {'n': 'hi'}}

Complex Example

import serpy

class Foo(object):
    y = 1
    z = 2
    super_long_thing = 10

    def x(self):
        return 5


class FooSerializer(serpy.Serializer):
    w = serpy.Field(attr='super_long_thing')
    x = serpy.Field(call=True)
    plus = serpy.MethodField()

    def get_plus(self, obj):
        return obj.y + obj.z

f = Foo()
FooSerializer(f).data
# {'w': 10, 'x': 5, 'plus': 3}

Inheritance Example

import serpy

class Foo(object):
    a = 1
    b = 2


class ASerializer(serpy.Serializer):
    a = serpy.Field()


class ABSerializer(ASerializer):
    """ABSerializer inherits the 'a' field from ASerializer.

    This also works with multiple inheritance and mixins.
    """
    b = serpy.Field()

f = Foo()
ASerializer(f).data
# {'a': 1}
ABSerializer(f).data
# {'a': 1, 'b': 2}

License

serpy is free software distributed under the terms of the MIT license. See the LICENSE file.

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

serpy-0.0.3.tar.gz (6.1 kB view details)

Uploaded Source

Built Distribution

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

serpy-0.0.3-py2.py3-none-any.whl (8.6 kB view details)

Uploaded Python 2Python 3

File details

Details for the file serpy-0.0.3.tar.gz.

File metadata

  • Download URL: serpy-0.0.3.tar.gz
  • Upload date:
  • Size: 6.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for serpy-0.0.3.tar.gz
Algorithm Hash digest
SHA256 5c52c4fd1f3b0068e8ef47d60a65179a8d79c6af05e0d0788e96c556dc414dac
MD5 7bce5e64c8a86f2ae90ef778a3a5fc0d
BLAKE2b-256 fec3a64a55b08f7e82e1ed83725e5106fdb08f11f0ddcbc56d877dddd06366bf

See more details on using hashes here.

File details

Details for the file serpy-0.0.3-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for serpy-0.0.3-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 0ffd3e525252119a38a2959899adfc233e02dd40e08e9dfc73668a472d6396d1
MD5 eaaad1d6d6ceb45983f18b795053f351
BLAKE2b-256 11b1bd79186275406f9db66cc26557c39ab4cd29246fea4f4f011eb51863bafd

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