Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: The user-friendly weather report

Login or purchase this course to watch this video and the rest of the course contents.
0:00 The final step here is to report the weather,
0:01 but in a nice way that users went to see that they expect. So instead of just printing
0:07 out this random data structure, we're gonna go and do a print statement.
0:11 So I'm gonna say print, little
0:12 f-string, the weather and just put little curly braces there for some for a bit because
0:19 we don't have it done yet, in some location,
0:21 like Portland, is so many degrees,
0:24 whatever the scale, Celsius, Fahrenheit and the condition is cloudy or something along those
0:30 lines. Take that away just for a minute
0:32 so it doesn't crash. So what we need is to get stuff like the location.
0:36 So it would be easy to think we could just put like the location there,
0:42 but remember, the location is this data structure,
0:44 and it'll look weird. And it will say that the state is None some
0:48 of the time. So what we need to do is get some location
0:50 text. Let's say location_name,
0:55 so get location name and we'll pass the location.
0:59 Let's write this little bit here.
1:04 And really, it all comes down to whether or not we have the name of
1:07 a state or no state. So we'll say,
1:09 "if not location.state"
1:11 So there is no state specified then we just want to return a string that
1:15 says "location dot city comma location dot country".
1:22 Now there's a small problem here,
1:23 It's gonna be lower case city and lower case country because we converted it that way,
1:27 remember? We did all that lower stuff to canonicalize
1:30 it to make sure we always sent the lower case thing to the server?
1:33 Well, we wanna undo that.
1:35 So now we can say "capitalize" here on this one.
1:39 And then the country, remembers it was a two digit code which probably just wanna make that upper
1:43 case. Now, if it's not the case that there is no state,
1:48 so there is a state, we just want to add it in there,
1:50 and it's gonna look like this because the state is an abbreviation as well,
1:54 so we'll just put state there,
1:56 and that's it. So let's just print out, comment that out for just a minute, on
2:01 the way, location name.
2:03 If I say, let's say Seattle,
2:07 Washington, US, The weather in, look at that,
2:10 Seattle, Washington, US. fantastic.
2:14 Alright, So what else?
2:15 What else do we got to do to make this work?
2:16 Over here We have It's the temperature.
2:20 So we're going to say the temp is easy.
2:23 That's just "weather.temp" and the scale,
2:26 let's just put it for a minute "f",
2:28 we'll fix it in a second,
2:29 let's say temp, scale, and then the condition as
2:33 well, gonna be "whether.condition".
2:38 Now, some of these we could actually just in line them like We don't need
2:40 a separate thing for this, I guess,
2:43 probably for the temp, either.
2:44 But for the scale, we're gonna need some help.
2:47 Let's go ahead and run this. See what we get.
2:48 Make sure it's working. Boston, the weather in Boston,
2:52 United States, is 53°F, clouds and overcast clouds.
2:57 And I think in this condition where we created that,
3:02 we'd already put a period. I didn't want the period there
3:04 I don't think like there. Try one more time.
3:07 Boston. Look at that. Perfect.
3:10 The weather is 53.29°F.
3:14 The last thing that actually make this legit is to determine whether that's an F or
3:18 a C. And that's actually one of the easiest things we've done.
3:21 So let's go over here and we'll just say "if whether.units equal imperial else
3:35 the scale is C". So let's change where we're calling that just to make sure if
3:42 we put metric over here, we should get the right answer.
3:46 Boston and you spell it right,
3:50 Boston, here we go. But are we, we are not getting right here,
3:55 are we? Let's see. And I think that's because when we called this,
4:02 I hard coded that into two places,
4:03 which is never a good idea.
4:05 But down here, we just wrote
4:07 this right. So I think we wanna put units in here and pass it over.
4:12 We don't have to pass it.
4:16 It's with the data. So here we just do "data
4:20 dot get units". Alright,
4:23 try again. Boston. Look at that! See, now if we go and just change this to Imperial
4:32 and Boston again, perfect. There it is.
4:35 Our little scale is being shown. We can do one more thing on the scale to make
4:39 it nicer. It would be better,
4:42 I think, if this happened on its own place.
4:44 So we can come over here and make a method called "get scale",
4:50 something like that. This writes
4:51 this little function so we don't have to worry about in-lining that, makes our
4:55 code up here much simpler. Okay,
4:57 so we're pretty much there. We can trim up some of those comments.
5:00 Like, do we need to say show the header? show the header.
5:02 No, those basically say the same thing.
5:05 Convert Plain text, says the same thing. Call the weather
5:09 API. Report the weather. This could even be a function that we create if
5:12 we want called "report weather" that might be cool.
5:17 Create a method called "report weather".
5:20 Here we go. And now how much,
5:24 how many comments do we need? We've got show header,
5:26 what do you want to do, convert to plain text, call the API,
5:29 report it. Super, super clean way to have our program working here.
5:34 There's one final piece that we want to take care of to
5:38 really put a nice cherry on the top.
5:40 Now check this out. If I type Boston and miss the T, Boston,
5:44 something crashed. This came back as "None"
5:48 and we tried to get the units which made it crash right there in the get
5:51 scale. What we need to do is there's a couple places like if they don't
5:57 pass the, we were not able to create the location,
6:00 right? They passed in nothing or something, we want to print
6:03 "Could not find anything about
6:08 the location" location, text
6:11 let's say. Like that, and then return.
6:15 Right. So now if we come in here and we just hit enter, couldn't
6:18 find anything about that. There's also the same things happening right here.
6:21 So if not whether, if it comes back
6:23 as none, we'll say "could not get the weather for this place from the
6:31 API". Now, if I put "bos", somewhere
6:35 we're still printing that out, right?
6:36 But it says could not get the weather for bos from the API,
6:38 but if I do "Boston", perfect,
6:42 that works. We got one stray print in here before we call it, totally good,
6:49 maybe we want to keep it there,
6:51 maybe we don't, right probably belong in some kind of logging or,
6:55 I don't know, we could get the status code and try to make sense of
6:58 that and so on. But I'm just gonna put it like this and call it
7:00 done. So even though it sounds like quite a big deal,
7:03 what we've done is pretty straightforward,
7:07 right? We just do a little bit of string work to get the data in
7:10 the right format, we call the weather
7:12 API using requests which we've installed into our virtual environment using pip,
7:16 and then we just take that dictionary data that came back and put it over and
7:20 here, we just call request.get, make sure it was successful,
7:23 JSON, and we're done. Beautiful.
7:26 This is a really, really cool app,
7:28 and I would consider it a real app.
7:30 It has really error handling, it has real behaviors, super cool.
7:35 Working with live data off of a
7:37 live API, gotta love it.