Rock Solid Python with Python Typing Transcripts
Chapter: Frameworks Built on Typing
Lecture: Data-Rich Pydantic Example
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
Let's look at a more realistic example. So remember the weather data we got here, I just grabbed that little bit and said,
0:08
here's a JSON piece made up of primary data types, strings and numbers. But this whole thing is a little more complicated.
0:18
For example, weather is itself a class made up of different things. Same for the wind. Units is just a fundamental type, right?
0:28
And turns out in order to go back, I think we could go over here, just like before and go, okay, well, the data looks like such and such.
0:37
And so we're gonna start working out the classes. It turns out, even in this simple example, there are seven different classes we have to create
0:45
in order to fully parse and validate this data. So that is possible manually, but let's not do it that way.
0:52
Let's copy it and I'll show you something super cool. First of all, let's get a copy of this.
1:01
I'll call it ""Parsing Weather"" and just run it real quick so it's the next thing to run. Excellent.
1:07
And instead of this data, I'm going to take that data over here and give me just a sec to format this better. There it is.
1:16
Python obviously doesn't care if it's formatted in a pretty way or not, but just so we as humans can read this without going crazy.
1:24
And you can now clearly see these are the different sections that have basically what will need to be nested classes.
1:32
All right, so again, we could take this data and create this type of model ourselves, but we're not going to do that.
1:40
Instead, I'm going to introduce you to the JSON to Pydantic converter. Now check this out. It has a very simple JSON example with foo and bar baz.
1:52
I personally hate those meaningless vacuous examples, but here they are nonetheless. It's still cool. Here's some JSON and look what it does.
2:01
It goes in, it says, all right, we're gonna create typed identic models against that data, kind of like a human would, and it's pretty good.
2:09
So it knows that that's an integer and that that's a string, right? Check this out. Let's put that data we just had.
2:17
Didn't even pretty print it, don't care. still valid JSON. And look, it gave us the weather which is this nested bit here at the
2:29
description and a category. Those are strings that give us the wind which is a float and
2:34
an int. So on and then this model down here is the top level thing which has a weather
2:39
wind forecast class and so on. So let's just copy this. We don't need to take the from
2:46
the future because we don't live in the past. We're not working on crummy old Python. We're
2:50
working on modern Python, so we can just use this. Now that's a lot of code, and I don't
2:55
want it all in one file. So I'm going to go over here and add a new Python file called
3:01
weather models. pasted format it. Now this thing called model is really what we work
3:09
with. So it's called a say weather forecast like that. Now I'm going to go over here and
3:15
use that. So I could say from weather models, import this, oops, not this, a weather weather
3:25
forecast and it looks like it might be working. But this is only working because PyCharm is
3:31
setting this as the working directory. So if you don't have PyCharm, you're going to
3:35
make sure you need to make sure that this is the working directory so that when Python
3:39
says look at the module called weather models, it's right there with it. Okay, we can be
3:44
a little extra safe and PyCharm. We unselect, just go to the directory, say mark directory
3:50
as sources root and it turns blue. You want to make sure that you're doing that. And then
3:54
down here. Just like before, it doesn't matter how complicated this gets. We can just say
4:00
weather forecast now and watch this. Oh, yes. The weather part is this weather class, which
4:08
is exactly as you expect. The wind is this wind class, which is as you would expect the the units as a string.
4:15
Look at this, it parsed the whole thing perfectly. And again, if some bit of data up here like this was a, this was a 64, doesn't matter.
4:25
You know, it says, hey, down inside of this forecast subsection, this is supposed to be an int, so let's try to parse it. We can, it's good to go.
4:35
How awesome is that? So here's how we use Pydantic to parse and validate data.
4:41
again, if this is missing, we try it. Even in this complex example, we get fields are
4:49
required. And if it's the wrong type, you get type validation based on Python typing.
4:56
We said, I is contained within this, what did it call it? This forecast class and down
5:07
here's the high and that is an int. So it used the for class type information in the
5:11
high being a type of int to make sure that everything is hanging together. Awesome, awesome
5:17
stuff. I love pydantic. It's such a great way to work with code. And let's just show
5:22
you now how simple it is to use this quote complicated data. Right now it's w dot. Whether I'm not loving the name, but that's okay. Description and...
5:38
Right now it's whatever the weather type is, cloudy, sunny, whatever, and the temperature is something Fahrenheit. Look at that.
5:51
Right now it's broken clouds and 60 degrees Fahrenheit. I'm recording this video right now.
5:58
Well, five minutes ago when I copied that JSON data out of the API at least. Pretty awesome, right?