Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Building the beginnings of the weather app
0:00 Before we talk about pip and packages
0:02 and the Python package index and all of those things
0:05 let's just sketch out our app as far as we can take it
0:08 and then we'll bring in the packages.
0:10 So we are going to get started as always by writing a program.py file
0:14 and let's also start this time by defining a main method
0:18 that's going to be sort of the top level method to be called here
0:21 remember, this is not a Python convention, this is just Michael convention,
0:25 so we are going to have to do things like
0:26 I'll just sketch out in comments like print the header
0:29 and then the next thing we have to do maybe is get HTML from web
0:35 so I want to download the HTML,
0:38 then we are going to have to parse, the HTML,
0:41 we have to display the forecast.
0:43 And at some point I suppose we are also going to need
0:46 to actually get the locations, we'll say get zip code from user.
0:52 Ok, so these are the kind of the steps
0:53 and you can see there is little warning
0:55 that's because there is actually nothing happening in this method
0:58 let's just do a print hello from main really quick
1:01 now recall, if I run this there is no run configuration
1:05 so I'll make one by right clicking and say run program- nothing happens,
1:09 our little print statement doesn't show up
1:11 and that's because we have to call main()
1:13 and we saw that this really is not the right way to do it
1:16 if somebody wants to reuse this module, this script,
1:20 if they import it this is always going to run our app,
1:23 so we saw that there was that convention we say, if__name__='__main__'
1:31 then we would run our main() method,
1:34 this is so common, that PyCharm has what is called live templates.
1:39 So I can come over here and you can see that there is main(),
1:42 this is the method I wrote
1:43 but then there is this main which has actually got some code here
1:47 and if I just hit tab it actually auto expands out of here.
1:51 So this is one of the live templates,
1:53 I'll pull those up really quick,
1:55 you can see there is actually a ton of live templates here,
1:58 that you can use, here is the one that we are running
2:00 but for all sorts of various not as Python
2:02 but SQL and Django and so on,
2:04 so let's go ahead and use our main() bit here,
2:07 and in that case we are going to call main().
2:09 So now our job... let's verify runs, still is,
2:12 now our job is to actually start writing these methods and drop this bit.
2:15 So we are going to print the header so we'll just say
2:18 call a method print_the_header(), or print header however you want to call it,
2:22 and let's put this down below, like so
2:28 and remember, all of our headers looked something like this,
2:35 and here it says weather app centered, usually, and the final new line.
2:41 So perfect, that is solved.
2:43 The next thing we want to do is get the zip code from the user,
2:46 so we can say zip, it's not exactly what we want to say
2:50 but let me just start this way for a moment,
2:52 input will say what zip code do you want the weather for,
2:59 and let's give him a little hint, and say like 97201 as an example.
3:04 Now, notice there is a small warning here,
3:06 we have a little problem with zip,
3:09 there is actually two warnings, one is that we are not using it,
3:12 but the more important warning,
3:14 the not using the part is going to go away,
3:15 is that this shadows the built in name called zip,
3:18 zip is a built in function that does kind of map produce like stuff,
3:23 and this is overwriting, basically hiding the zip function for the rest of this method
3:29 so let's change this, I just want you to be aware
3:32 that those types of things can happen,
3:33 so let's change this to code now there is still a warning
3:35 but it's just that we haven't used it yet, so that's not a big deal.
3:38 The last part is to get the HTML from the web
3:41 and we need to go talk about
3:43 where on the web we are going to get this
3:45 but let's just do a print out of the code really quick
3:48 just to make sure everything is hanging together.
3:50 So what is your zip code, it's 01010 five digits perfect,
3:54 everything is working fine, except for we now need to go get some HTML off the web.