Practical Guide to Python

Anatomy of a Python Program

Practical Guide to Python

Check out a free preview of the full Practical Guide to Python course

The "Anatomy of a Python Program" Lesson is part of the full, Practical Guide to Python course featured in this preview video. Here's what you'd learn in this lesson:

Nina walks through a completed Python program that uses the GitHub API to load data from Python, JavaScript, and Ruby repositories with over 60000 stars. The code contains examples of variables, functions, lists, dictionaries, and loops.

Preview
Close

Transcript from the "Anatomy of a Python Program" Lesson

[00:00:00]
>> Now that you know all about Python, you know how to set up your environment, I'm gonna quickly show you the anatomy of a Python program. Note that you don't need to understand everything that's happening in the program. So don't worry, don't panic. You're gonna understand all of the concepts throughout the course of the day.

[00:00:19]
But I wanna show you what some typical Python code might look like and what it does and break it down into pieces. So here I have a Python program for using the GitHub Search API. And I'm gonna open this up in my editor so that I can walk you through it, and I'm gonna run it and we'll see some results.

[00:00:49]
So first line I'm importing requests. This is a package import in this case requests is a third party package that I would need to install in my virtual environment before running this program, since it's not included in Python status library. Then I'm going ahead and defining a GITHUB_API_URL.

[00:01:12]
So, what this piece of code is going to do is, it's going to use the public GitHub search API. And it's gonna pass an e-query asking for repositories in particular languages that have more than a set number of stars. These are methods and you can tell because they have the def keyword in front of them and some code that is indented to the right of them.

[00:01:41]
But for the time being, I'm gonna scroll all the way down. And this code is only 40 lines. So once you start getting more familiar in Python, you'll have no trouble reading this line by line and understanding exactly what it does. But the thing that I'm scrolling down to is on line 30.

[00:02:00]
And it says, if __name__ is equal to __ main in quotes, that's a string. This is how Python knows whether the main method is being run. So when we start a Python program from the command line, this is going to be the point of entry. Here I'm defining a list.

[00:02:27]
And this is a list of what languages I would like to query for. In this case, it's Python, JavaScript and Ruby. And then I'm calling a method that is defined in this file called repos with most stars. And I'm passing in the list of languages as an argument.

[00:02:49]
Now in VS code, I can in Mac Command click or in Windows+ Ctrl, click on to this method. And it will automatically jump and bring me straight to it in the code. This method is defined in line 16. So first I am creating a query with that list of languages.

[00:03:11]
Create query is another method that I've defined in this file. Let's jump up to that. This one takes a list of languages and a second argument. Here I'm providing a default value of 60,000 as the second argument. So if that second argument is not provided, it's going to use the default.

[00:03:36]
Then I'm going to build up my query. This is something called an F string in Python or a string with built in formatting. And this allows me to provide a variable inside of curly braces, just like this. And the representation of that variable is going to get inserted right into the string.

[00:04:00]
So I start building my base query and then I loop through every language in the list of languages. And I append onto my query a string that looks like this. So a sample query that might be generated from this method looks like this comment right here. Stars colon greater than 50, language colon Python space language colon JavaScript.

[00:04:27]
This is syntax that the GitHub Search API expects as part of a query. And then it returns the built up query. Now we're back in the repos with most stars method. I'm going to build a dictionary that's defined with these curly braces. The first entry in this dictionary has a key of Q and the value of query.

[00:04:57]
A key of sort, that is going to be in this case the default value, which sort by the number of stars. And then also a key of order, it will have a value of disc for descending. I'm going to get a response from the GitHub API by using this requests library.

[00:05:21]
The request library provides a .get method that maps to HTTP GET where I pass in the URL, and then that dictionary of parameters that I built online, a team. And then I'm gonna get the status code in the response. So for those of you familiar with API's it might be 200 for okay, 404 for not found, maybe 503 if I wasn't authorized.

[00:05:49]
It doesn's matter to me. If I got back a status code that was not 200, sorry, I have ligatures turned on, for myself, I will turn those off for all of you at the next break. Because what this should actually look like is not equals without a space in between, I just have a fancy setup.

[00:06:15]
So, for that I apologize. But I'm checking here that if the status code does not equal 200, I'm gonna raise a runtime error, my programs just gonna quit. I got an error code, I don't know what it is. Don't know where to go from here. Otherwise, I'm going to get the JSON from the response, save it in a variable called response_JSON.

[00:06:39]
And then I'm gonna return the items key in that response JSON back to the calling main method. So now I'm here, I've gotten the results from the repos with most stars method right up here. And I'm gonna loop through every results in that list of results. And I'm going to get the language which I know from reading the GitHub API docs, that it's gonna be in a JSON format that's gonna have a language key, a stargazers count key, which is the number of people who have started that repository.

[00:07:24]
And it's also gonna have the name of the repository. Then, for every result in these results, I'm gonna use one of these f strings, to print out an arrow. The name of the repo is a language repo with these many stars. So it's pretty short program. It's only 40 lines of code.

[00:07:52]
I like to think that this is pretty readable. Do you folks think that this is readable? Yeah. It's compact and condensed, it's pretty straightforward, what's happening. And let's go ahead and run this. And I'm just gonna do that by opening up the command palette, and then selecting run python file in terminal.

[00:08:18]
This is something that you might run into, if you already have a repo open in your terminal and you select, run Python file in terminal VS code is gonna try to paste in the commands to run the file into your repo, and then your repo is going to explore.

[00:08:36]
So if you see something like this, this is the fully qualified path to the Python executable on my system. And the file that I'm trying to run if you see that and like invalid syntax, know that you just need to exit out of your repo or open a new terminal here with plus before you try running that again.

[00:08:59]
So, let's try that again. I'm gonna select Python run file and terminal and now it works successfully. And this went and reached out to the GitHub API and pretty quickly returned this list of some of the top Python JavaScript and, forget the third language. What was the third one?

[00:09:24]
Let's see, Ruby. I guess there are no Ruby repositories of over 60,000 stars. The GitHub_Search_API doesn't always return super accurate values, just an FYI. It optimizes for returning quickly versus super accurate results. So if you run this multiple times, you might get multiple different results. But that was, it was pretty straightforward.

[00:09:57]
It used a lot of the language features that we are going to learn today. And it's a really quick and easy way to interact with API's, interact with data, and automate a lot of the tasks that you might have to do day to day.

Learn Straight from the Experts Who Shape the Modern Web

  • In-depth Courses
  • Industry Leading Experts
  • Learning Paths
  • Live Interactive Workshops
Get Unlimited Access Now