Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Converting API data to weather tuples

Login or purchase this course to watch this video and the rest of the course contents.
0:00 Recall what we got back from here with our data. Let's just print out Portland. We got all of this stuff, like our weather with our
0:08 details and our categories and whatnot, what I want to do is get more structured, simple data that we could work with.
0:15 We did that up here with locations, and let's just actually do the same thing for weather.
0:19 So we'll say whether or weather report or something like that is gonna be another named tuple and it's just gonna have city,
0:27 let's actually, you know, we could have city, state, country, or we could pass over just a location object.
0:31 So let's say it just has a location and then it has units. Is it metric or is it imperial or whatever?
0:38 And then the temperature and the condition. So if it has those things, we'll be able to
0:43 create it down here. So all we gotta do is come down and, it's not, we can print it for a minute.
0:48 I'm gonna go over here and what we need to pass is the location which is already passed in, so that's easy.
0:54 The units, well, the units we're passing over are right there, so you could pass it however you want. And then we need the temp and the condition.
1:05 I'm gonna do a short little version here, and then we'll do an improvement in a minute, but let's return the weather. So we'll say the temp is data,
1:14 now remember, how do we get this out? It actually is kind of all over the place. We want to run. Remember, there's weather, which has, like,
1:23 the description here, so that's gonna be interesting, I'll put that up here for us. The other part that we care about is what's called
1:34 the forecast right here, it's wrapping around oddly but like this. So what we actually need to do is we need to get, go to the weather
1:44 and we're going to get some stuff here and then go to the forecast and then get the temperature, so the temperature is gonna be go here.
1:50 I'm going to get the forecast and from the forecast, we're going to get the temp, and then the condition,
2:00 same type of thing. Get the weather, then we want to get like we could get the description right. But I want to say, "category : description",
2:11 So what we could actually do is come over here, like thi, and say we're gonna
2:14 put this into a little variable, and I could just say, create an f-string, say it's gonna be "w dot get category"
2:24 and then outside the curly braces "colon w dot get description". now, the description I wanted to be a capital first letter.
2:33 So a cool way to do that is to say, "capitalize", makes it like a sort of like a proper sentence. And then here put a period on the end.
2:42 So let's just print out the weather before we go any farther here, see what happens. If I say "Boston" got a bunch of stuff printed there.
2:51 But this part, this is the interesting part. Look how cool that is! So we got the location is Boston, bo state specified, US.
3:00 Units are imperial, temperature is 53 degrees, and the condition is clouds, overcast clouds. Super, super cool.
3:07 So the final, final thing we've got to do is just show that to the user in some meaningful way. But this is starting to look complicated,
3:14 and it shouldn't be complicated. There's not much going on here. All we need to do really is we wanna actually,
3:20 maybe even do a little bit more like so when it said that the temperature was 59, we're not using the units either,
3:26 are we? So what I want to do for the unit is we're gonna need to get the, whether or not it's imperial or metric.
3:33 But you don't say the degrees or imperial, you say they're Celsius or Fahrenheit, so we're gonna have to do a little conversion there.
3:39 And what I want to do is I wanna make this something that we can put away and make it simpler. So let's say, we're going to convert this data over
3:47 to a weather report so we could go and write a function, I could come down here and type "def convert API to weather object"
3:58 right? Or PyCharm actually has our back here, which is pretty cool. We can go and
4:02 highlight that, right click, refactor, extract method, and you give it a name, hit enter, and check that out. It wrote that right there for us.
4:13 So that's cool. It's, if I deselect it here, you can see that it's helping us out here a lot by cleaning things up. We don't need to print the weather.
4:22 We could, in fact, just return this directly. Now, if we go back to our calling the weather API,
4:28 it looks simple again, and that is because it's simple. It was just getting a little bit complicated there.
4:33 So now, call this "weather" maybe, up at the top, and, print it out one more time,
4:39 It's not quite the end, but you can see nice print out of everything. All we gotta do is convert that into something we'd like to show the users,
4:48 maybe get rid of that, and we'll be set with our weather example.

Talk Python's Mastodon Michael Kennedy's Mastodon