Pydantic is OP, here's why

  Рет қаралды 15,291

Carberra

Carberra

Күн бұрын

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/conc...
[3] docs.pydantic.dev/latest/conc...
[4] https:://www.gov.uk/bank-holidays.json
[5] docs.pydantic.dev/latest/api/...
-
If you want to see more of me, join my Discord server!
/ discord
I get a lot of people asking, so here's my setup!
• Visual Studio Code: • My Visual Studio Code ...
• Terminal: • Make your terminal loo...
• 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=...
• 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

Пікірлер: 52
@DuncanBooth
@DuncanBooth 4 күн бұрын
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.
@Carberra
@Carberra 3 күн бұрын
I've never much liked Annotated, just looks really messy and ugly. That looks neat though -- where in the docs did you find that? Pretty sure it's not in the Validators section.
@VictorOliveira-lg2qg
@VictorOliveira-lg2qg 3 күн бұрын
Even better, Pydantic already provides types like that for convenience, which uses annotations. In this case, you can import PositiveInt like this: from pydantic import PositiveInt
@DuncanBooth
@DuncanBooth 3 күн бұрын
@@Carberra I replied with the link but can't see my reply so perhaps youtube cut out the link. It's under concepts/validators, literally the first thing under the Validators heading.
@greob
@greob 3 күн бұрын
Pydantic is very interesting, and this makes me want to try using it more. Thanks for sharing!
@Carberra
@Carberra 3 күн бұрын
You're very welcome!
@santiagohal6747
@santiagohal6747 4 күн бұрын
I love this. This is brilliant.
@ButchCassidyAndSundanceKid
@ButchCassidyAndSundanceKid 5 күн бұрын
The new version of Pydantic is re-written in Rust, should be a lot faster than before.
@Carberra
@Carberra 5 күн бұрын
I've seen benchmarks claiming it's about 7x as fast as V1, didn't know that was the reason though!
@ButchCassidyAndSundanceKid
@ButchCassidyAndSundanceKid 5 күн бұрын
@@Carberra Holy crap ! I thought it was fast, I didn't know it'd be that fast. I suppose the only other framework it can beat that is the gPRC and protobuf. v1 was written in Python, but v2 was written in Rust which is in the same league as C++, that probably explains about the superior performance.
@irlshrek
@irlshrek 4 күн бұрын
i think if people have the time they should learn Rust!
@ButchCassidyAndSundanceKid
@ButchCassidyAndSundanceKid 4 күн бұрын
@@irlshrek The question is "IF people have the time".
@gabriellevesque2185
@gabriellevesque2185 2 күн бұрын
​@@ButchCassidyAndSundanceKid Same League as C 😅
@pythonwithjames
@pythonwithjames 4 күн бұрын
Really nice!
@HerozTech
@HerozTech 5 күн бұрын
I was recently learning golang and coming back to python i missed struct but pydantic more than makes up for that Great video👍
@HerozTech
@HerozTech 5 күн бұрын
one more thing What color scheme are you using, it looks awesome
@squishy-tomato
@squishy-tomato 5 күн бұрын
see the video linked in the description
@Boot3011
@Boot3011 5 күн бұрын
Whats the plugin that autofills your variables and stuff? Thats awesome
@danielandreasen2293
@danielandreasen2293 5 күн бұрын
Looks like github copilot
@Carberra
@Carberra 5 күн бұрын
Almost -- it's a competitor called Codeium. I made a video on it: kzbin.info/www/bejne/h3a4d2B3mpdgmck
@KonstantinUb
@KonstantinUb 4 күн бұрын
What's the difference between Pydantic and Marshmallow? Can they be used to accomplish the same things?
@Carberra
@Carberra 4 күн бұрын
I'm not sure how functionally different they are, but Marshmallow is certainly designed to used to define ORM-agnostic schemas for databases and convert to and from JSON data. I believe you can do the same with Pydantic, but I haven't used enough of Marshmallow to delve into the differences.
@davidmurphy563
@davidmurphy563 5 күн бұрын
3:33 It took you a month to release this video? Are they better when they age? ;)
@Carberra
@Carberra 5 күн бұрын
Like a fine wine 🍷
@ayehavgunne
@ayehavgunne 3 күн бұрын
I like the library called apischema which lets you use dataclasses to achieve the same thing.
@illiasukonnik9966
@illiasukonnik9966 Күн бұрын
Excellent video, thanks! Please use caching when playing with external APIs, be polite ))
@basb3603
@basb3603 3 күн бұрын
What theme is that?
@Carberra
@Carberra 3 күн бұрын
Link to setup is in the description (:
@ruslanoid
@ruslanoid 4 күн бұрын
TIL about `kebab-case` (until now just called it dashes or hyphens)
@gownerjones
@gownerjones 4 күн бұрын
Isn't the whole point of it being spelled "pydantic" that you pronounce it "pie-dantic"? :D
@Carberra
@Carberra 4 күн бұрын
It may well be, I actually don't know, never heard anyone else say it 😅
@gownerjones
@gownerjones 4 күн бұрын
@@Carberra Well I'll be pedantic and pronounce it pie-dantic just for the irony lol
@christianlinux8283
@christianlinux8283 3 күн бұрын
I thought subclass config was deprecated
@Carberra
@Carberra 3 күн бұрын
Huh, so it is. Why did they not remove that in 2.0? I just went with it cos it worked so I thought it hadn't changed.
@ChrisAdaline
@ChrisAdaline 4 күн бұрын
Who else had to look up what bunting is?
@murphygreen8484
@murphygreen8484 4 күн бұрын
This guy
@Carberra
@Carberra 3 күн бұрын
How very unpatriotic of you! /j
@damonguzman
@damonguzman 3 сағат бұрын
Explainer is better than code along.
@shadmansudipto7287
@shadmansudipto7287 3 күн бұрын
Lol. Use a non scripting language.
Python's contextlib is a HIDDEN GEM
14:55
Carberra
Рет қаралды 9 М.
Pydantic Tutorial • Solving Python's Biggest Problem
11:07
pixegami
Рет қаралды 247 М.
ОСКАР ИСПОРТИЛ ДЖОНИ ЖИЗНЬ 😢 @lenta_com
01:01
МАМА И STANDOFF 2 😳 !FAKE GUN! #shorts
00:34
INNA SERG
Рет қаралды 4,4 МЛН
Khó thế mà cũng làm được || How did the police do that? #shorts
01:00
The moment we stopped understanding AI [AlexNet]
17:38
Welch Labs
Рет қаралды 224 М.
new SSH exploit is absolutely wild
11:59
Low Level Learning
Рет қаралды 259 М.
The End Of Jr Engineers
30:58
ThePrimeTime
Рет қаралды 201 М.
This cli component was trickier to build than I thought
17:34
Dreams of Code
Рет қаралды 40 М.
ACADEMIA IS BROKEN! Stanford Nobel-Prize Scandal Explained
9:41
`const` was a mistake
31:50
Theo - t3․gg
Рет қаралды 125 М.
Why You Should Use Pydantic in 2024 | Tutorial
13:56
ArjanCodes
Рет қаралды 62 М.
Insane Vulnerability In OpenSSH Discovered
1:06:56
ThePrimeTime
Рет қаралды 151 М.
Will Python SKIP these versions?
9:58
Carberra
Рет қаралды 3,4 М.
*Next-door 10x Software Engineer* [FULL]
4:50
Programmers are also human
Рет қаралды 260 М.
ОСКАР ИСПОРТИЛ ДЖОНИ ЖИЗНЬ 😢 @lenta_com
01:01