Skip to main content

Deserialize to objects while staying DRY

Project description

Desert generates serialization schemas for dataclasses and attrs classes. Writing code that’s DRY (“don’t repeat yourself”) helps avoid bugs and improve readability. Desert helps you write code that’s DRY.

Installation

pip install desert

or with Poetry

poetry add desert

Usage

A simple example models two Person objects in a Car.

from dataclasses import dataclass

# Or using attrs
# from attr import dataclass

from typing import List

import desert

@dataclass
class Person:
    name: str
    age: int

@dataclass
class Car:
    passengers: List[Person]

# Load some simple data types.
data = {'passengers': [{'name': 'Alice', 'age': 21}, {'name': 'Bob', 'age': 22}]}


# Create a schema for the Car class.
schema = desert.schema(Car)

# Load the data.
car = schema.load(data)
assert car == Car(passengers=[Person(name='Alice', age=21), Person(name='Bob', age=22)])

Documentation

https://desert.readthedocs.io/

Limitations

String annotations and forward references inside of functions are not supported.

Acknowledgements

2020.01.06 (2020-01-06)

Changes

  • Additional metadata are supported in ib() and fields(). #28


2020.01.05 (2020-01-05)

Changes

  • Add support for attrs factories that take self (attr.Factory(..., takes_self=True)). #27


2020.01.04 (2020-01-04)

Changes


2020.01.03

Changes

  • Optional fields allow None. #11. Thanks to @sveinse for reporting and testing.

2019.12.18

Changes

  • Improve error message for unknown generics. #10

2019.12.10

Changes

  • Add UnknownType exception with better error message for types that should be generic. #8

2019.12.09

Changes

  • Marshmallow schema Meta arguments are accepted, allowing exclusion of unknown fields and other options. #3

2019.11.06 (2019-11-06)

Changes

  • Add twine and wheel development dependencies. #2


2019.11.06 (2019-11-06)

Changes

  • Switch to calver

Backward-incompatible Changes

  • Non-optional fields without a default or factory now have required=True so raise marshmallow.exceptions.ValidationError when missing. #1


0.1.0 (2019-06-22)

Changes

  • First release on PyPI.

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

desert-2020.1.6.tar.gz (31.1 kB view details)

Uploaded Source

Built Distribution

desert-2020.1.6-py2.py3-none-any.whl (13.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file desert-2020.1.6.tar.gz.

File metadata

  • Download URL: desert-2020.1.6.tar.gz
  • Upload date:
  • Size: 31.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.9

File hashes

Hashes for desert-2020.1.6.tar.gz
Algorithm Hash digest
SHA256 e64cd61e16607bb3096ab1b1763c9f229ebcf8b3f871f4db52fb803e1c000385
MD5 b480fecf5c3bc0628ce3f56cc8df9f67
BLAKE2b-256 196bd03e8204ccc58d02413a77ac58c45f6679db34d8ac67ca3cd988c7a61363

See more details on using hashes here.

File details

Details for the file desert-2020.1.6-py2.py3-none-any.whl.

File metadata

  • Download URL: desert-2020.1.6-py2.py3-none-any.whl
  • Upload date:
  • Size: 13.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.13.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/39.0.1 requests-toolbelt/0.9.1 tqdm/4.35.0 CPython/3.6.9

File hashes

Hashes for desert-2020.1.6-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 190ab1c690472ab1c1ef7614f9a73171c1e911cef42d7b45a67f9b7d6900763d
MD5 654ed8ca81edde23ec6ffc8fed6ed44c
BLAKE2b-256 85debc71eff26801df701c91e30320f53a7e4d212aebcac9890f9bf33d6a3adc

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page