avocato: simple and fast object serialization¶
avocato is a simple and fast ORM/framework-agnostic object serialization library for converting complex objects to and from simple Python datatypes.
Don’t be scared if you’re using an ORM/framework. It can easily be adapted to be used with any ORM/framework of your liking. Currently it supports Django ORM and peewee.
This library is heavily influenced by serpy.
Installation¶
$ pip install avocato
Documentation¶
Find documentation at avocato.rtfd.io
Example¶
import avocato
class Bar(object):
patrick = 'star'
class Foo(object):
over = 9000
spongebob = 'squarepants'
bar = Bar()
class BarSerializer(avocato.Serializer):
patrick = avocato.StrField()
class FooSerializer(avocato.Serializer):
over = avocato.IntField()
spongebob = avocato.StrField()
bar = BarSerializer()
foo = Foo()
FooSerializer(foo).data
# {'over': 9000, 'spongebob': 'squarepants', 'bar': {'patrick': 'star'}}
API Reference¶
Serializers¶
Fields¶
-
class
avocato.
Field
(attr=None, label=None, required=True, validators=None, call=False, default=None)¶ Field
handles converting between primitive values and internal datatypes. It also deals with validating input values.Parameters: - attr (str) – The attribute to get on the object. If this is not supplied, the name this field was assigned to on the serializer will be used.
- label (str) – A label to use as the name of the serialized field instead of using the attribute name of the field.
- required (bool) – Whether the field is required.
- validators (list) – List of validators to run when calling
.is_valid()
method on the serializer. - call (bool) – Whether the value should be called after it is retrieved from the object. Useful if an object has a method to be serialized.
- is_create_field (bool) – Whether the field is used to populate the instance when creating
a new object via
to_instance
method on the serializer. - call – Whether the field is used to populate the instance when updating an object
via
to_instance
method on the serializer.
-
class
avocato.
StrField
(**kwargs)¶ Converts input value to string.
Parameters: - max_length (int) – Maximum lenght of the string. If present, adds a validator for max lenght
which will be run when
is_valid
method on the serializer is called. - min_length (int) – Minimum lenght of the string. If present, adds a validator for min lenght
which will be run when
is_valid
method on the serializer is called. - choices (list) – Available choices. If present, adds a validator that checks if value is
present in choices and will be run when
is_valid
method on the serializer is called.
- max_length (int) – Maximum lenght of the string. If present, adds a validator for max lenght
which will be run when
-
class
avocato.
EmailField
(**kwargs)¶ Converts input value to email.
-
class
avocato.
IntField
(attr=None, label=None, required=True, validators=None, call=False, default=None)¶ Converts input value to integer.
-
class
avocato.
FloatField
(attr=None, label=None, required=True, validators=None, call=False, default=None)¶ Converts input value to float.
-
class
avocato.
BoolField
(attr=None, label=None, required=True, validators=None, call=False, default=None)¶ Converts input value to bool.
-
class
avocato.
DecimalField
(attr=None, label=None, required=True, validators=None, call=False, default=None)¶ Converts input value to string, so it accurately shows decimal numbers.
-
class
avocato.
DateTimeField
(attr=None, label=None, required=True, validators=None, call=False, default=None)¶ Converts input value to ISO format date.
-
class
avocato.
DictField
(attr=None, label=None, required=True, validators=None, call=False, default=None)¶ Converts input value to dict.
-
class
avocato.
MethodField
(method=None, **kwargs)¶ Calls a method on the
Serializer
to get the value.
Validators¶
-
class
avocato.
Validator
¶ Base class for validators.
-
class
avocato.
Required
(message=None)¶ Validates if value is set.
Raises exception if value is empty or None
-
class
avocato.
Email
(message=None)¶ Validates if value is in valid email format
-
class
avocato.
Length
(min_length=None, max_length=None, message=None, equal=None)¶ Validates if value is correct size.
-
class
avocato.
OneOf
(choices, message=None)¶ Validates if the value is one of the choices.
-
class
avocato.
OneOfType
(choices, message=None)¶ Validates if value type is one of the choices.
\ Sort by:\ best rated\ newest\ oldest\
\\
Add a comment\ (markup):
\``code``
, \ code blocks:::
and an indented block after blank line