Python Jumpstart by Building 10 Apps Transcripts
Chapter: App 5: Real-time weather client
Lecture: Named tuples: Making tuples usable
0:00 We saw these tuples, it works but they are hard to use, right,
0:04 like we need to know what order these items are put in there,
0:08 and the only way to know really is to go down here
0:12 and actually look what the return type is.
0:15 So let's do something better,
0:17 let's go over here and start with our collections module
0:20 and when we import collections in there
0:22 there is a type called named tuples
0:24 and we can create whatever named tuple we want,
0:28 and it will be the tuple that knows how weather reports look,
0:31 so I'll call this a weather report, and just make this part up,
0:36 I'll say this weather report is a collections.named tuple
0:40 and its name is going to be this and then the next item that we put in here
0:43 is actually a comma separate list of the names of the values,
0:47 so let's say it's going to have a condition, it's going to have a temp
0:52 it's going to have a scale, Celsius and Fahrenheit
0:56 and it's going to have a location.
0:58 All right, now this weather report actually defines a type
1:01 and we can create instances of this type down here
1:04 so we go back under this get weather report and let's say
1:07 ok that was cool that sort of worked but it was not the best,
1:10 and so here we can say report equals and we'll just create a weather report
1:14 and in here we'll pass the values,
1:17 if we have the values exactly the same order that they are listed above,
1:21 we could just copy them in like this.
1:25 All right, so maybe I remember that right,
1:28 but there is a little safer way to do this so it's more obvious
1:31 and this is using keyword parameters
1:33 so I can say the condition or the cond=the condition and the temp=temp scale,
1:42 I guess I didn't do a great job choosing different names, the location=loc.
1:46 so now we've got our report and we are going to return our report,
1:49 so if I run this code again, I will probably get different output,
1:53 because of the order, but we'll see that these are compatible with standard tuples,
1:58 so if I run this again, great, we still get the same meaningless report.
2:04 But now that we have named tuples,
2:06 we are in a much better place, let's go fix this,
2:09 up here we wanted to print out this data
2:11 and we are using these indexes and we still can but check this out,
2:14 if we go . we now have a temperature a condition, a location, and a scale,
2:19 PyCharm knows that named tuples are special
2:22 and it actually looks in here to help us and get some intellisense
2:25 but regardless of PyCharm, Python will let us say things like .location.
2:32 So the temperature and the location is temp
2:35 let's see my little message doesn't make a lot of sense here
2:40 let's put temp and then scale say that scale and finally
2:45 we'll put condition here like sunny or rainy and so on.
2:49 All right, so we are going to our get weather from html
2:51 we are instantiating one of these named tuples and we are passing it back
2:57 grabbing into report variable and then because it's a named tuple
3:00 we can work with well with the names. Let's try this.
3:04 That is beautiful, the temp in Portland Oregon is 8.6 degrees Celsius and mostly cloudy
3:12 all right, so that pretty much wraps up our weather app
3:14 let's come back and look at this tuple concept a little deeper
3:17 it's one of our core concepts.