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