Working with APIs in Python? Use Pydantic
HTML-код
- Опубликовано: 11 сен 2024
- Thanks for IPRoyal for sponsoring this video! Get 30% off any Royal Residential Proxies by following this link and using code "CARBERRA" at checkout!
iproyal.club/c...
-
A while ago I covered TypedDict in a video and got A LOT of comments talking about how amazing Pydantic is. Turns out they were right, so I thought I'd show it off!
-
If you enjoy my content, consider supporting me on Patreon or becoming a member!
patreon.carber...
join.carberra.xyz
If you need help with anything, feel free to join the Discord server:
discord.carber...
I get a lot of people asking, so here's my Visual Studio Code setup!
• My Visual Studio Code ...
-
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
Thanks for IPRoyal for sponsoring this video! Get 30% off any Royal Residential Proxies by following this link and using code *"CARBERRA"* at checkout!
iproyal.club/carberra
To convert the field name, you can use the simplest way. Use 'Field(alias="....")'
from pydantic import BaseModel, Field
class Example(BaseModel):
pythonic_field: str = Field(alias="CamelCaseField")
resp = {"CamelCaseField": "cool"}
p = Example(**resp)
print(p)
Honestly this looks like you have to do this for every field but here its generalised, if the data keys are less then it would be fine but if you have a lot of keys, its a headache
You can make gt, ge etc. validators automatically by using =Field()
that basemodel class is borrowed from typescript Interfaces
I wish I had learned this a month ago...
ngl some things are just op
Interesting, I miss ur hairs 😢
Maybe it'll make a return one day!
@@Carberra