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

gjedlicska/pydantic-introduction

 
 

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

16 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Hultnér Technologies

Hultnér Technologies AB | @ahultner | Blog | Slides | Nordisk Python Community | GitHub


⠠⠵ Introduction to pydantic

Introduction to the Python Pydantic library.
Take your data classes to the next level! Showcasing run time type checking, data serialization and deserialization, custom validators and of course data class integration. Make it easy to go from standard data classes to pydantic models.

⠠⠵ Quick reference

# Minimal usage example
from pydantic import BaseModel, validator, root_validator
from enum import Enum

class Topping(str, Enum):
    mozzarella = 'mozzarella'
    tomato_sauce = 'tomato sauce'
    prosciutto = 'prosciutto'
    basil = 'basil'
    rucola = 'rucola'


class Pizza(BaseModel):
    style: str
    toppings: Tuple[Topping, ...]
    

class BakedPizza(Pizza):
    # For simplicity in the example we use int for temperature
    oven_temperature: int
        
    # A validator looking at a single property
    @validator('style')
    def check_style(cls, style):
        house_styles = ("Napoli", "Roman", "Italian")
        if style not in house_styles:
            raise ValueError(f"We only cook the following styles: {house_styles}, given: {style}")
        return style
    
    # Root validators check the entire model
    @root_validator
    def check_temp(cls, values):
        style, temp = values.get("style"), values.get("oven_temperature")
        
        if style != "Napoli":
            # We don't have any special rules yet for the other styles
            return values

        if 350 <= temp <= 400: 
            # Target temperature 350 - 400°C, ideally around 375°C
            return values

        raise ValueError(f"Napoli pizzas require a oven_temperature in the range of 350 - 400°C, given: {temp}°C")

Talks

⠠⠵ Python Pizza, 2020 April 25 (Remote)

Give your data classes super powers with pydantic, 25 April 2020, 14:12 UTC
The speaker schedule for the next Python Pizza conference is up, and I'm one of the speakers. The conference will be held remotely, tickets are €30 and all the proceeds will go to Doctors Without Borders!

If you or your company can't afford the ticket there's also a financial aid program.

Speaker at Python Pizza 2020, Alexander Hultnér talks about pydantic

⠠⠵ FAQ, pydantic

Is the pydantic type-checker strict?
No, pydantic currently favours parsing and will coerce the type if possible. A strict-mode is being worked on.

Can pydantics runtime type-checker be used on functions?
Yes, through the @validate_arguments decorator. But the feature is at the time of writing still in beta (2020-04-25).

Do settings managment support .env?
Yes it does!

I have a question not covered here, where can I ask it?
I'm @ahultner on twitter, otherwise you can also email me (address in slides).

⠠⠵ Links

About

Introduction to the Python Pydantic library

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published