Pydantic is OP, here's why

22,096
0
Publicado 2024-07-01
I've made a video on Pydantic already, but version 2 has since been released, so I thought I would come back to it! You can think of Pydantic as a supercharged dataclasses, with all sorts of functionality even attrs doesn't have. It's great for APIs too!

00:00 - Intro
00:44 - Installing Pydantic [1]
01:12 - Creating models [2]
04:15 - Validating inputs [3]
08:10 - Using Pydantic with an API [4-5]
17:33 - Outro

[1] docs.pydantic.dev/latest/install
[2] docs.pydantic.dev/latest/concepts/models
[3] docs.pydantic.dev/latest/concepts/validators
[4] https:://www.gov.uk/bank-holidays.json
[5] docs.pydantic.dev/latest/api/aliases



If you want to see more of me, join my Discord server!
discord.gg/5vVgQpK

I get a lot of people asking, so here's my setup!
• Visual Studio Code:    • My Visual Studio Code setup! (2024)  
• Terminal:    • Make your terminal look AMAZING  
• Desk/recording gear: kit.co/Carberra
• PC build: uk.pcpartpicker.com/list/TmzGYN

Affiliations (I may earn a commission):
• Codecrafters -- 40% off any plan: app.codecrafters.io/join?via=parafoxia
• Keeper Password Manager -- 30% off your first year: keeper.io/r/EX4FB1C



If you have any questions, don't hesitate to ask in the comments! I'll try and answer as soon as I can, providing someone else hasn't already done so.

#python #coding #howto

Todos los comentarios (21)
  • @DuncanBooth
    The newer version of Pydantic encourages you to write validators using Annotated. For example: from pydantic import BaseModel, AfterValidator from typing import Annotated def validate_positive(value: int) -> int: assert value > 0, "must be > 0" PositiveInt = Annotated[int, AfterValidator(validate_positive)] class Order(BaseModel): id: int item_name: str quantity: PositiveInt and now you have a reusable type annotation for positive integers.
  • @ayehavgunne
    I like the library called apischema which lets you use dataclasses to achieve the same thing.
  • @HerozTech
    I was recently learning golang and coming back to python i missed struct but pydantic more than makes up for that Great video👍
  • @Boot3011
    Whats the plugin that autofills your variables and stuff? Thats awesome
  • Excellent video, thanks! Please use caching when playing with external APIs, be polite ))
  • @HerozTech
    one more thing What color scheme are you using, it looks awesome
  • @KonstantinUb
    What's the difference between Pydantic and Marshmallow? Can they be used to accomplish the same things?
  • 3:33 It took you a month to release this video? Are they better when they age? ;)
  • @ruslanoid
    TIL about `kebab-case` (until now just called it dashes or hyphens)
  • @greob
    Pydantic is very interesting, and this makes me want to try using it more. Thanks for sharing!
  • @gownerjones
    Isn't the whole point of it being spelled "pydantic" that you pronounce it "pie-dantic"? :D