Intro to Pydantic V1
ฝัง
- เผยแพร่เมื่อ 1 ส.ค. 2024
- A highly useful library for data modeling, parsing and validation (used in FastAPI and useful for many other aspects of Python software development)
For Pydantic V2 see here: • Intro to Pydantic V1
00:00 - Introduction
02:16 - Basics
14:41 - Field Aliases
22:48 - Extra Fields Behavior
27:12 - Using Config for an Alias Generator
34:29 - Field Validations
35:02 - Field Validations - Constrained Numerics
36:14 - Field Validations - Constrained Strings
40:16 - Field Validations - Custom Validators
43:50 - Field Validations - Auto Correcting Field Values using Custom Validators
46:26 - Field Validations - Multi-Field Custom Validators
56:19 - Nested Models using Model Composition
You'll need to understand Python's type hinting syntax - if you're not familiar with that, I have a video on type hinting here: • Python Type Hinting
#mathbyteacademy #python #pydantic #pythonlibraries
Code for this Video
================
Available in GitHub code repo: github.com/fbaptiste/python-blog
Direct link: tinyurl.com/2mhxrv3u
My Python Courses
=================
- Pydantic V2: Essentials
www.udemy.com/course/pydantic...
- Python 3 Fundamentals (introduction to Python)
www.udemy.com/course/python3-...
- Python 3 Deep Dive (Part 1 - Functional)
www.udemy.com/course/python-3...
- Python 3 Deep Dive (Part 2 - Iteration, Generators)
www.udemy.com/course/python-3...
- Python 3 Deep Dive (Part 3 - Hash Maps)
www.udemy.com/course/python-3...
- Python 3 Deep Dive (Part 4 - OOP)
www.udemy.com/course/python-3...
really cool to see you have youtube channel now Fred !!!, really there is no one better than you, when it comes to explaining python concepts, btw i also wrote to you linkedin about it, that i got a job in Amazon, using your material on python to prepare for interviews.
Thanks! that's great about getting your job, glad you found my Python material useful for that!
Pydantic is such great library! I'm looking forward to a content about FastAPI.
I bought all your courses in udemy. They are amazing and have helped me a lot. Thanks for uploading these videos on youtube. I hope at some point you make a video on asyncio too. Take care
Glad you like them!
same here. Dr Baptiste's Udemy course is excellent and I was happy to hear his voice in a youtube channel.
@@mathbyteacademy Could you please tell which version of pydantic have you used for demonstration in this video?
This is a fantastic intro to pydantic. Most other YT videos are too short and narrow. This covers the material in just the right amount with a great real world example at the end. Very well done, you got a new sub and potential udemy course buyer. (when I get to those topics :D)
Glad you enjoyed it!
Thanks! You’re an amazing educator. I am in the part 2 of your Udemy course, and I have already bought the other two. Thanks a lot for sharing your knowledge in this incredible simple way.
Thanks!
Really fantastic walk through/tutorial. One of the best I've seen. Thank you!
Glad you enjoyed it!
Great, thorough walkthrough. Really appreciate your expertise. Excited to start implementing pydantic.
Thanks, and best of luck!
Great work! A suggestion would be to divide the video and GitHub notebook into different sections. This makes following them easy.
thanks for the overview, Fred!
You're welcome!
Very nice and detailed explanation on using the validators in Pydantic. Thank you so much. Can you please upload a video on validating the data (using pydantic) by reading from a csv file and then inserting the data into a PostGres (Or any database) table?
thank you very much, great overview!
Glad it was helpful!
This is a great tutotial! Thanks a lot for the video!! But one thing is missing from here that is -
Relation with other table with id.
For example
books: {
title: ""
author: "objectId"
}
Thanks Mehedi - glad you liked it! I'm not following you, can you explain a bit more?
thank you sir
You're welcome!
great thank you
Welcome 😊
As good as your awesome udemy courses!
Thanks Cyril!
Thanks Fred for the awesome tutorial. I have one suggestion: please make sure you are not tying at the bottom of your screen, when I pause the video I see the YT's video control UIs over what you've just typed which makes it impossible to read...
Thanks for the tip!
Best
Thanks!
Fred, I am a big fan of your teaching from udemy and this channel. Requesting you to make a video about dbapi, sqlalchemy and alembic. There is not any good tutorial available, at least in the detailed way you teach..
Thanks, glad you like it!
However, SQLAlchemy (or other ORMs) is not a topic I will cover - I avoid it as much as possible, since I prefer actually writing SQL by hand.
Anyone know whether Pydantic can be integrated into PySpark? I tried mapping parsed data strings to a BaseModel object instead of a namedtuple object for each record and I had an error related to pickling.
Background:
I am creating a PySpark ETL on AWS for an internship.
I'm new to all three (AWS, PySpark, ETL concepts) so my mental models may be incoherent, but I'm wondering if:
Use Cases:
- Pydantic can be used to validate records on read/update
- Pydantic can be used to validate and generate test data
- other things?
No expectation of an answer. I understand that people have their own lives and things. Just thought I would ask.
P.S: I love everything Fred does. At the end of a hard day learning and working, I relax by learning from Fred's content. They are just so well put together and delivered from such a nice place of calm and thoughtfulness that it is a pleasure rather than a battle to learn from him.
Just saw a link to the Faker library. Maybe I should look into that for the 'validate and generate test data' use case.
1:10:07 why is the second person's display name None? I think it should be "Oskar M". And btw thanks for such a useful and detailed video on pydantic. I couldn't find something like this anywhere.
Yeah, it should be - not sure what happened when I was recording . If you check the notebook in GitHub you'll see the display name correctly populated. Glad you liked the video!
👍🏻
Great video! 1 question I have is where you added an empty list as the default value for an argument. If we do this in standard Python classes then all instances will share the same list, leading to some potentially unwanted side effects. Does pydantic take care of this for us?
It does - you can just try it out for yourself and test by making two instances of the same Pydantic model, and test identity of the attribute of the two different instances (using `is`) - you'll see that the result is `False`
@@mathbyteacademy thank you - looking forward to using it!
When you print ex.json() @6:18, how are you getting the output in a nice formatted manner?
For me, the print of a list is a single line with the scroll bar
I wonder if something changed in later versions of V1. Certainly things changed in Pydantic V2.x.
You can use this to print nicely indented JSON: `print(ex.json(indent=2))`
@@mathbyteacademy Yeah, giving indent worked. Also when can we expect the new Pydantic V2 complete tutorial?
@@syedabdul8509 Glad it worked! I am finalizing a full Pydantic V2 course that will be released on Udemy in early January.
Can someone please help me demistify “validation checking,” because pydantic seems like typechecking on steroids. Reason is that data in the wild might have some random letters that would pass pydantic but it’s not REALLY data validation. This may be because the way “data validation “ is used varies depending on which part of a workflow or tech stack, but I feel nontechnical audiences can’t wrap head around this nuance, and end up with still not necessarily “good data,” just better data.
Please correct me if I’m wrong
Of course a solution for example with names, is using something like the python Human Name Parser in conjunction and add some simple checks and exceptions list, but what about in numeric data that can fall in ranges for multiple IoT streams or such? I once worked somewhere that didn’t understand the many to one faultiness of type checking so their data was lots of sensor data that was scrambled and of course their models sucked … so am checking here to see if pydantic would solve something like that, which I don’t think it can in isolation but maybe I’m wrong
You can write custom validators in Pydantic. I cover the basics of that in this video. You can also create validators that can reference other fields in the structure (in case you need validation using combinations of fields) - also covered in this video.
@@mathbyteacademy can you give me an example of a custom validator that makes sure something is a name? Or are you saying, attach something like human name parser to a validator then that is the custom validator ? I’m probably just missing something here… I will also ask chatGPT for a similar example
Thanks for the response by the way!!
Yes, you need to write your own Python function (as a pydantic custom validator) that examines the bit of data and determines if it is valid - the implementation of that validation function is entirely up to you. Pydantic has some built-in validators for common things (like integers constrained to some range, text from a pre-defined collection of possible values, etc). But Pydantic isn't going to provide you with more specific validations such as checking if a string is a name - that's something you have to write your own code for.
As far as how to verify that a string is a name, I have no idea how you would do that - if I gave you a string like "drmla" or "ngoc" how would you determine (not in code, just manually inspecting) if that is a valid name?