MongoDB with Async Python Transcripts
Chapter: PyPI Beanie
Lecture: CLI Skeleton

Login or purchase this course to watch this video and the rest of the course contents.
0:00 All right, let's create our CLI app now. I'll go over here and I'm just going to create a standard Python thing. I'll call it CLI.
0:08 And let's go ahead and just run it to make sure that we're running not some old thing but this current one here. Looks like it's running the right one.
0:16 Cool. So we're going to just have a main method and the first thing we're going to do is initialize the connection to MongoDB to make sure that works.
0:25 And of course, you've got to do that before you can work with the database structures
0:30 at all. So let's define a main method. Remember I added this shortcut to run this. Now this
0:41 is your standard way of getting started in Python, right? But in order to work with Beanie,
0:46 remember everything is async over there. So this has to be async, which means that if
0:51 we still run it, we'll just get a warning. Coroutine main was not actually run. It was is not awaited, we have to say asyncio.run this.
1:00 Now we get it working. So the first thing that I want to do, instead of saying hello, is I want to have a little header like,
1:11 welcome to the PyPI CLI some version. I'm going to drop in some text here, just says PyPI CLI version 1.0, and we're going to call print header.
1:22 Make sure that's working. Beautiful, look at how amazing our ASCII art is now. So that's great, we've got our print header.
1:31 Then we want to use our Mongo setup to connect to PyPI. Let's do that. So say Mongo, you hit Control + Space twice,
1:41 PyCharm will go look to autocomplete and import stuff for us. Beautiful, thank you PyCharm. And hit connection and the DB name will be PyPI.
1:50 I double check over here. It is PyPI, super. Now there's a warning because we're not awaiting, right? Coroutine was not awaited. Await.
2:02 All right, let's run that and make sure that this works. Well, it looks like we found our first problem here.
2:08 Let's jump over to our package real quick. And it says right here, you can't use bson.objectid. Whoops, I grabbed the wrong one.
2:16 We want beanie.pydantic.objectid. So it's like an object ID, but one that pydantic can work with. There we go, connected to PyPI.
2:27 And as we noticed from that error, a little side benefit from screwing that up, is that
2:33 you notice that it actually validated that everything was working together pretty well.
2:37 There's still some possibilities of things going wrong, but some of the validation, some of the checks that we got things right are happening.
2:45 The next thing that I want to do is have a summary of what data we have in our database.
2:52 For example, if we go back to the PyPI homepage here, you can see we have how many projects, how many files, how many releases, how many users.
3:00 So I want to have something like that at the beginning of our startup that says, ""How much data do you have in here?
3:06 And I'm going to create a little method here that says, ""Show summary"" or just ""Summary, and it'll just print out the stats when we get going.
3:14 So we're going to need to get the package count, release count, and user count, and it has to be async because we're talking to the database.
3:22 So up here, we're going to go, let's put the database stuff together, summary.
3:28 Now if we run it, it's not actually yet talking to the database, it just has PyPI stats there.
3:35 Let's put a parent just to get a little separation on the reporting. There we go.
3:42 So our goal is going to be to actually talk to the database and fill out that, like how do we get the release count or the user count, right?
3:51 So we still need to write that bit of code. And then I want to have a loop, like a REPL almost,
3:57 where it goes around and says, ""What do you want to do? Do you want to show the summary? Do you want to search for a package? And that kind of stuff.
4:04 We can just put that up here. Here's a little starter for what we might do, just so we don't have to type it all out.
4:10 So while true, we're going to let people say, Enter S to show the summary again, F to search for packages,
4:18 P to list the most recently updated packages, U to create a user. What is that? That's R to create or release, or X to exit.
4:26 And we're just going to go around and around until either they exit out and we'll just let them interact with it in this way. Alright?
4:34 So our goal is going to be to write all these different functions to make that happen. happen.


Talk Python's Mastodon Michael Kennedy's Mastodon