MongoDB with Async Python Transcripts
Chapter: Foundations: Pydantic
Lecture: Our First Pydantic Model

Login or purchase this course to watch this video and the rest of the course contents.
0:00 So what we want to do is we want to work with some data. This is the goal of Pydantic. It takes some input data,
0:08 and then it is going to convert that into a class. Now in our world, we want to model some kind of API response. Like we have here, we have an item ID,
0:19 we have a created date, pages visited, and a price. So we're going to go and model this with Pydantic by creating a class that's called an item.
0:30 And in order for this to actually be a Pydantic model, not a regular one, it can't inherit from object,
0:35 but rather it has to inherit from Pydantic.baseModel. And then we just specify the type. So I see an item ID down there. And what kind is that?
0:46 That is an integer. Now, notice the quotes, it's submitted to us as a string, but really what we intend here is for that to be an integer in our API.
0:56 So we'll say that. We have a created date. This is a date time. Not date time like this. We have pages visited, which is a list of int.
1:16 Again, those are integers. That's a string, but could it become an integer? Yeah, probably. This right here, by the way,
1:26 instead of being capital L list, being lowercase list, there's your Python 3.9 dependency we talked about at the start of the class.
1:33 And finally, we have a price, which is a float. So now we have a class that represents what we expect from our inputs to our API
1:44 or to reading this data. And we have data down here that's not exactly the right shape. For example, this is not actually an integer,
1:54 but it could be parsed to this is a list of integers, same thing here. So let's go ahead
2:00 and try to use pydantic to parse this. So we come down here, we can say item equals
2:05 item. Now see this star star star kW args, come back to that in a minute. What I want
2:14 to do is I want to say item ID equals the value from data created date equals like this
2:20 created date equals data of created date. Now that's not fun, is it? Comma, and so on. So with
2:31 a dictionary, we can write code that says for every key, write this expression by just saying
2:38 star star the dictionary. So those key value, key value, key value as parameters. And then let's
2:44 print out item. So this tells pedantic to ingest that data using this model, and to
2:52 parse it according to the rules that the types declare, for example, item ID must be an integer.
2:59 And it must be there because that's not an optional integer, it's a concrete integer.
3:04 Let's run it and see what we get. Oh, look at that. Here's our printout. And a couple
3:13 things to note that this is a actual integer, not just the string 123. Because if we had
3:22 a, let's put name, which is a string just so you can see the difference here, I'll put
3:31 name is Michael, something like that. Here you can see the quotes indicating that's actually
3:38 the string type, right? So this is not a string. This is the number 123. Here's our created
3:43 date parsed correctly. Here's our pages visited and notice that that three that was not accurate,
3:50 actually also got parsed, as well as our price. How awesome is that? But there's more to it
3:56 than just this. Suppose here that we want to make sure there's an item ID. If our data
4:01 doesn't contain it, it says item ID field is required, it's missing. Another thing that
4:10 could happen is this could be ABC 123. And up here in our data model, we say it's an
4:16 integer, could that be parsed to an integer? No. So item ID now has this problem that its
4:22 value is not an integer. How cool is that? Right? All that is really complicated work. Similarly, if this was A3, even better.
4:33 It says there's a problem trying to parse this data into item.
4:37 The problem is the pages visited the number two index, so third thing in the list, because it's zero based, is not an integer. Look at that reporting.
4:47 How excellent is that? So really, really cool. Now if we wanted to say, you know, now if the name is missing, final one, it's going
4:55 to crash and say name is missing. If we want that to be something you could submit, but is not required, we could make this optional.
5:04 Now you can express optional in recent Python like string or none. I don't really like that syntax very much because I don't really want it to be none.
5:15 I want it to be a string or maybe it's missing, right? So I'd rather express this as optional, which you get from typing.
5:23 I know other people have different opinions and you're welcome to those. You can do String Pipe None or you can do Optional String.
5:29 Either way, this is telling Pydantic if it's there it's got to be a string, but if it's missing, no longer is that an error. It's okay, it's just None.
5:39 We can even set a default value. Jane Doe if it's missing, or if you pass in a name, it's Michael.
5:52 really cool, right? So excellent, excellent that we can create this model. And this is
5:58 our first pass one of many we have for working with pydantic. We create a class, it derives
6:05 from base model, we specify the types, those types have all sorts of meanings, what is
6:10 required, item ID is required, because it's not an optional int. We have default values, It does all the parsing if it can. Really really great.


Talk Python's Mastodon Michael Kennedy's Mastodon