Skip to main content

pavlova is a library that assists in mapping an unknown input into a dataclass.

from datetime import datetime
from dataclasses import dataclass

from pavlova import Pavlova


@dataclass
class Input:
    id: int
    name: str
    date: datetime


Pavlova().from_mapping({
    'id': 10,
    'name': 100
    'date': '2018-08-10',
}, Input)
# Input(id=10, name='100', date=datetime.datetime(2018, 8, 10, 0, 0))

Pavlova was born out of frustration with the lack of typing support for existing deserialization libraries. With the introduction of dataclasses in Python 3.7, they seemed like the perfect use for defining a deserialization schema.

Supported functionality

Parsing of booleans, datetimes, floats, ints, strings, decimals, dictionaries, enums, lists are currently supported.

There are more parsers to come, however to implement your own custom parser, simply implement PavlovaParser in pavlova.parsers, and register it with the Pavlova object with the register_parser method.

Installation

pip install pavlova

Usage with Flask

from dataclasses import dataclass, asdict

from flask import Flask, jsonify
from pavlova.flask import FlaskPavlova

pavlova = FlaskPavlova()
app = Flask(__name__)

@dataclass
class SampleInput:
    id: int
    name: str

@app.route('/post', methods=['POST'])
@pavlova.use(SampleInput)
def data(data: SampleInput):
    data.id = data.id * len(data.name)
    return jsonify(asdict(data))


app.run()

Adding Custom Types

There are a couple of different ways to implement new types for parsing in pavlova. In general, the process is to add a parser a specific type. For validation you should raise a TypeError or ValueError.

The first one, is creating a new type that extends an existing base type. Here is an example on how to implement an Email type, which is a string but performs validation.

from pavlova import Pavlova
from pavlova.parsers import GenericParser

class Email(str):
    def __new__(cls, input_value: typing.Any) -> str:
        if isinstance(input_value, str):
            if '@' in input_value:
                return str(input_value)
            raise ValueError()
        raise TypeError()

pavlova = Pavlova()
pavlova.register_parser(Email, GenericParser(pavlova, Email))

Another way, is to implement your own pavlova parser, rather than using your the built in GenericParser parser.

import datetime
from typing import Any, Tuple

import dateparser
from pavlova import Pavlova
from pavlova.parsers import PavlovaParser

class DatetimeParser(PavlovaParser[datetime.datetime]):
    "Parses a datetime"

    def parse_input(self,
                    input_value: Any,
                    field_type: Type,
                    path: Tuple[str, ...]) -> datetime.datetime:
        return dateparser.parse(input_value)

pavlova = Pavlova()
pavlova.register_parser(datetime.DateTime, DatetimeParser(pavlova))

Requirements

Pavlova is only supported on Python 3.6 and higher. With Python 3.6, it will install the dataclasses module. With Python 3.7 and higher, it will use the built-in dataclasses module.

License

GNU LGPLv3. Please see LICENSE and COPYING.LESSER.

Release files for pavlova 0.1.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pavlova 0.1.3
File Size Uploaded
pavlova-0.1.3.tar.gz 6.2 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for pavlova 0.1.3
File Interpreter ABI Platform
pavlova-0.1.3-py3-none-any.whl Python 3 none any Details

Total release size: 28.6 kB

Release files / pavlova-0.1.3.tar.gz

Download URL pavlova-0.1.3.tar.gz
Size 6.2 kB
Tags Source
SHA-256 checksum
How to use checksums
baf36f3f0d5a1cc6dc3829492b3cbd3f0ddfd3601d85ab2a1e47c2ccd457e5d4
BLAKE2b-256 checksum
How to use checksums
aed0493a65b4f57c0f1034d4592958cfa1709fcaf1da3ddb9af3bec8bc029d6f
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1

Release files / pavlova-0.1.3-py3-none-any.whl

Download URL pavlova-0.1.3-py3-none-any.whl
Size 22.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
1a5acb53c288dc50b2eeb594dfc07a3af3bb8b0a7066e195d257d05c7c2ee1c8
BLAKE2b-256 checksum
How to use checksums
c1b1e90445013016ebdb2adc9c62ae4e67479e4b3d9f97cabb372015444fb2f1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.12.1 pkginfo/1.4.2 requests/2.20.1 setuptools/40.6.2 requests-toolbelt/0.8.0 tqdm/4.28.1 CPython/3.7.1
Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page