Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Main method

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Before we get into writing the code that goes behind each one of these steps,
0:03 let's do just a little bit of organization.
0:05 Remember, I always like to structure my program with a main method that has the
0:09 high level steps of what it's going to do right the top.
0:12 So let's write that, and we'll put these steps here. Now, notice there's a little error
0:17 right there, if I make it go up
0:18 it'll probably move to the bottom.
0:20 No, it still stays there. The error is that the main method doesn't actually do anything yet,
0:24 so let's just make it do something.
0:29 "Hello from weather main". Okay,
0:31 so now there's a meaningful main implementation here so that the structure of the Python code
0:36 actually works. However, remember,
0:38 if we run it, there's no output.
0:40 What happened to our "hello weather main"? So that's because we're not actually calling it, right.
0:45 That's just defining a function that if it were to be called,
0:48 it would say "hello from weather main".
0:49 So if we call it like this,
0:50 that's great. However, this will not allow us to reuse the functionality of this
0:57 script or this application. So,
0:59 like get a report from the API?
1:00 Nope. We will not be able to use that function because if we import this,
1:03 it's going to actually, as part of the import statement,
1:06 processing, is going to run this.
1:08 So, recall there's this convention, "if __name__ == '__main__'"
1:14 that means it's being run explicitly,
1:16 not imported, so we should do the same thing.
1:19 But if somewhere else we import it,
1:20 it'll just define the functions and not try to run the program.
1:23 This actually is so common, this convention right there,
1:27 in most real Python programs, that PyCharm actually has what's called a "live template"
1:33 for it. So if I type the word "main"
1:36 you can see there's this thing that's a function,
1:38 that's just what I wrote above,
1:39 but there's another one down there with a little hint that's like "if name equals main"
1:43 hmm, maybe that's interesting. So if we hit Tab here it's going to expand that,
1:48 that live template will expand that, and then we can call the actual function we're
1:53 looking for, Okay? And it doesn't matter what this is called. This could be called
1:58 something like "entry point" or whatever,
2:01 right? It just means if we're being run as a program, run the thing that
2:06 we're thinking of as the top level.
2:08 Here we go. Okay, So let's look over here.
2:13 And if we look actually in the editor,
2:16 you can see there's a whole section of these live templates for all the different languages.
2:19 If you're doing SQL or you're working on Flask or you're doing Javascript but
2:24 most relevant to us right now
2:25 is Python. And down here you can actually see "main".
2:30 So this is the template that we were expanding, and I actually went
2:33 and created my own that will create the main method and write that. So I
2:36 can just start from an empty file and type "F-main" so you can create new
2:40 ones as you see fit. So,
2:42 for example, the one I created would do this part right here.
2:46 Saves me a little bit less thinking about it, just get started and go. So these live
2:50 templates are super helpful, and now our program is ready to be run as an application.