MongoDB with Async Python Transcripts
Chapter: PyPI Beanie
Lecture: Recent Packages
Login or
purchase this course
to watch this video and the rest of the course contents.
0:00
What one do we want to do next? Well, how about recently updated packages? So we should be able to write a function here package service dot
0:10
recently updated and then we're going to need to
0:15
Store this somewhere. So call this packages and this is gonna have to be an async function. So we're gonna wait it
0:22
Well now we can tell pycharm to go write that for us Thank you. You almost got it right, PyCharm. You almost got it right.
0:32
But you didn't know that that was there. Now let's suppose that this is going to take a parameter that tells us how many they want.
0:40
But we can default it to five if they don't specify it. So down here, what are we going to do? Just like before, we're going to go to the type.
0:48
So we'll say packages or say I'll call it updated. I'm going to go to package, and now we don't want to do a filter.
0:57
What we want to do is get all of them, but show just the top. So in order to figure that out, we need to sort by something.
1:05
Luckily, if we jump over to package, it has a last updated time. So we can just do a query directly against that. So we can do find all.
1:17
That's the entire list, but we want them sorted, so we say sort. There's two ways we can sort here. I could say negative last updated
1:28
to sort by last updated, to sending to put the newest first and the oldest last, or I can use the type.
1:36
I kind of prefer the type, so package last updated. Now, notice there's a error here. This is a mismatch in the type checker
1:48
versus the way Beanie defined it or something like that. It's not an actual error, it's an important thing.
1:53
So I'm gonna tell it not to worry about that. And then we only want so many. We don't want all 5,000 packages. That would be silly.
2:02
We just want how many they're asking for. So let's say we're gonna do a limit of this count. Now, so far this is just a query.
2:13
We could asynchronously loop over it or we could serialize it.
2:18
We could execute it into something like account, we could do something like convert it into a list. All those are async operations.
2:27
So finally, we want to do a to list. In order for that we have to await. And what we get back here is a list of packages.
2:36
So we should be able to return updated like this. And I think that will do it. I think that'll that'll work. So let's go back to our CLI here.
2:49
We got our packages, let's just real quickly print out packages, see what we got back.
2:56
And in our CLI here, we're going to create most recently updated packages, we'll just do p.
3:02
Who are created date has something going on with the releases here. Let's see. Package.
3:09
Package. Oh yeah, it expected a string. Remember that was one of the parsing over from our
3:17
JSON to Pydantic. Let's try again. P for packages. And one more thing with the object ID. Oh,
3:28
it looks like we forgot to change the type of underscore ID here. So recall we had underscore
3:35
ID. Typically is that bson.object ID. But in our case, we said we want the ID to be
3:40
a string. So unless we explicitly state it, Beanie is going to try to make that object
3:46
ID. And when it saw the object ID, or sorry, when it saw the string in the database, instead
3:52
of an object ID, it couldn't work. So let's do one more time. Here we go. We've got all
3:59
of our packages, you can see all sorts of stuff about them. It just apparently prints
4:04
a ton of them out because it's a Pydantic model, which we don't necessarily want to
4:08
see all that information. But you can bet that if we just print out the length of them
4:13
that we would have five. So let's go ahead and do something a little more nuanced with
4:17
showing the packages. So what we're going to do is going to loop over them and Wells
4:22
get the index back starting at one print out its name and when it was last updated in a
4:27
little bit of its summary. Let's try that instead. See how this looks. Here we go. Look Look at that, the most recently updated ones was SKTime,
4:36
PYCT, Shapely, and some of these others. And look, you even get a cool little bit of summary about them. Excellent, right?
4:43
Now these dates here, this is when I exported the stuff from the API, preparing this class quite a while ago.
4:48
So it's not really like today, but that's okay. It gives you the idea, right? These are the, this is the basically the most recent ones
4:57
out of that list. All right, I think that one is working. Right, that one is working great right there.