Skip to main content
Join the official 2019 Python Developers SurveyStart the survey!

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)])

Limitations

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

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.

Files for desert, version 2019.11.6
Filename, size File type Python version Upload date Hashes
Filename, size desert-2019.11.6-py2.py3-none-any.whl (12.9 kB) File type Wheel Python version py2.py3 Upload date Hashes View hashes

Supported by

Elastic Elastic Search Pingdom Pingdom Monitoring Google Google BigQuery Sentry Sentry Error logging AWS AWS Cloud computing DataDog DataDog Monitoring Fastly Fastly CDN SignalFx SignalFx Supporter DigiCert DigiCert EV certificate StatusPage StatusPage Status page