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


Talk Python's Mastodon Michael Kennedy's Mastodon