Skip to content
This repository has been archived by the owner on Apr 10, 2023. It is now read-only.

Commit

Permalink
Rework tags
Browse files Browse the repository at this point in the history
  • Loading branch information
rossmacarthur committed Aug 29, 2019
1 parent 6c0ab7e commit d1146e1
Show file tree
Hide file tree
Showing 15 changed files with 2,129 additions and 1,460 deletions.
6 changes: 4 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,10 @@ Easily serialize and deserialize arbitrary data to and from Python objects.
'Paris Hilton'
>>> owner.birthday
datetime.date(1981, 2, 17)
>>> owner.dog
Dog(name='Tinkerbell', hates_cats=True)
>>> owner.dog.name
'Tinkerbell'
>>> owner.dog.hates_cats
True
View the latest usage and API documentation
`here <https://ross.macarthur.io/project/serde/api.html>`_.
Expand Down
67 changes: 28 additions & 39 deletions src/serde/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,10 +94,12 @@
>>> group.name
'The Lonely Island'
>>> group.lead
Person(name='Andy Samberg')
>>> group.members
[Person(name='Akiva Schaffer'), Person(name='Jorma Taccone')]
>>> group.lead.name
'Andy Samberg'
>>> group.members[0].name
'Akiva Schaffer'
>>> group.members[1].name
'Jorma Taccone'
Serializing a ``Group`` would serialize the entire nested structure.
Expand Down Expand Up @@ -125,8 +127,8 @@
>>> group.name
'One-man Wolf Pack'
>>> group.lead
Person(name='Alan Garner')
>>> group.lead.name
'Alan Garner'
>>> group.members
[]
Expand Down Expand Up @@ -169,7 +171,7 @@
>>> class Person(Model):
... class Meta:
... tag = 'kind'
... tag = tags.Internal(tag='kind')
...
... name = fields.Str()
... birthday = fields.Optional(fields.Date)
Expand Down Expand Up @@ -207,15 +209,18 @@
>>> class Pet(Model):
... class Meta:
... tag = True
... tag = tags.External()
...
... name = fields.Str()
>>> class Dog(Pet):
... hates_cats = fields.Bool()
>>> Pet.from_dict({'Dog': {'name': 'Max', 'hates_cats': True}})
Dog(name='Max', hates_cats=True)
>>> dog = Pet.from_dict({'Dog': {'name': 'Max', 'hates_cats': True}})
>>> dog.name
'Max'
>>> dog.hates_cats
True
Internally tagged
~~~~~~~~~~~~~~~~~
Expand All @@ -228,15 +233,18 @@
>>> class Pet(Model):
... class Meta:
... tag = 'species'
... tag = tags.Internal(tag='species')
...
... name = fields.Str()
>>> class Dog(Pet):
... hates_cats = fields.Bool()
>>> Pet.from_dict({'species': 'Dog', 'name': 'Max', 'hates_cats': True})
Dog(name='Max', hates_cats=True)
>>> dog = Pet.from_dict({'species': 'Dog', 'name': 'Max', 'hates_cats': True})
>>> dog.name
'Max'
>>> dog.hates_cats
True
Adjacently tagged
~~~~~~~~~~~~~~~~~
Expand All @@ -249,37 +257,18 @@
>>> class Pet(Model):
... class Meta:
... tag = 'species'
... content = 'data'
...
... name = fields.Str()
>>> class Dog(Pet):
... hates_cats = fields.Bool()
>>> Pet.from_dict({'species': 'Dog', 'data': {'name': 'Max', 'hates_cats': True}})
Dog(name='Max', hates_cats=True)
Untagged
~~~~~~~~
Untagged data will try to deserialize each variant in turn. The first variant
that succeeds deserialization will be returned. You can enable this by setting
the ``tag`` field to ``False``.
::
>>> class Pet(Model):
... class Meta:
... tag = False
... tag = tags.Adjacent(tag='species', content='data')
...
... name = fields.Str()
>>> class Dog(Pet):
... hates_cats = fields.Bool()
>>> Pet.from_dict({'name': 'Max', 'hates_cats': True})
Dog(name='Max', hates_cats=True)
>>> dog = Pet.from_dict({'species': 'Dog', 'data': {'name': 'Max', 'hates_cats': True}})
>>> dog.name
'Max'
>>> dog.hates_cats
True
Abstract Models
~~~~~~~~~~~~~~~
Expand Down Expand Up @@ -357,7 +346,7 @@
from serde.model import Model


__all__ = ['Model', 'exceptions', 'fields', 'validate']
__all__ = ['Model', 'exceptions', 'fields', 'tags', 'validate']
__title__ = 'serde'
__version__ = '0.6.2'
__url__ = 'https://github.com/rossmacarthur/serde'
Expand Down

0 comments on commit d1146e1

Please sign in to comment.