Skip to content

Commit 4baea18

Browse files
committed
Transcripts for app 10 are back (after re-recording the videos)
1 parent 91dc80c commit 4baea18

File tree

9 files changed

+536
-0
lines changed

9 files changed

+536
-0
lines changed

transcripts/txt/10_app/1.txt

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
0:00 Application number 10, let's build a movie search app. 
2+
0:03 Now it turns out we are going to put this movie search app 
3+
0:06 into a particularly unreliable environment, 
4+
0:10 so we are going to have to do a lot of work 
5+
0:12 to make sure our movie search app is durable 
6+
0:15 and it doesn't crash or behave too weirdly. 
7+
0:18 Let's see how that's going to go. 
8+
0:20 We are going to build an app that looks like this, standard header. 
9+
0:22 We are going to enter some text and this text is going to be the title of a movie. 
10+
0:27 So, here we might say Top Gun,
11+
0:30 and we'll try to go out to the internet and find information 
12+
0:33 about movies with Top Gun in their title, 
13+
0:35 but you can see in this case it said oh sorry, 
14+
0:37 the network happens to be down so no searching right now. 
15+
0:41 But our app doesn't crash or give weird errors it just says hey, the network is down, 
16+
0:45 check your wi-fi or something like this. 
17+
0:47 Then later we try again, search for Top Gun and boom, 
18+
0:49 we get real live movie search results off the internet. 
19+
0:53 So it might sound like this is about searching movies, 
20+
0:56 but really what we are going to focus on is error handling, 
21+
0:59 writing durable reliable applications. 
22+
1:02 So we are going to focus on error handling,
23+
1:05 the primary way to do this in Python is with exceptions, and try/except blocks, 
24+
1:10 you'll see that we can have 
25+
1:12 multiple sort of error handling clauses in any particular situations, 
26+
1:17 so there might be a host of reasons that our movie app doesn't work, 
27+
1:21 maybe the DNS server is down, maybe we entered a bad title, 
28+
1:26 maybe the wi-fi is down, maybe the other server actually gave a 500, 
29+
1:30 maybe malformed data came back, 
30+
1:32 so we can actually catch different errors and give different messages back. 
31+
1:37 We might want to tell the users something different 
32+
1:39 if the data that came back was malformed, 
33+
1:42 rather than if the network was not connected. 
34+
1:45 Right, so we'll use multiple what are called except blocks to do this
35+
1:48 and that will allow us to differentiate errors based on the type of the error, 
36+
1:52 and ultimately, we are going to talk about writing reliable code 
37+
1:56 and you also see how to raise errors and exceptions 
38+
1:59 not just catch them because we are going to throw some 
39+
2:02 to sort of make our app particularly unreliable.

transcripts/txt/10_app/2.txt

+67
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
00:00 Let's begin this section, this app by talking about
2+
00:03 where we're going to get our movie data from.
3+
00:06 We're going to use a movie service at movieservice.talkpython.fm.
4+
00:10 Now this is just a simple service with a subset of the movies,
5+
00:15 there's only a couple of thousand movies here,
6+
00:17 but these are the movies that you can go and search,
7+
00:20 and you'll see that there's a couple of things we can do with this basic api,
8+
00:24 we can search for movies by keywords,
9+
00:27 so if I click on this, it will run a little example,
10+
00:29 in Firefox, it happens to parse and give this pretty view
11+
00:34 but if we hit raw, you can see what comes back
12+
00:36 from the server is actually this json
13+
00:38 we get a couple of things, we get the keyword sent back just to say
14+
00:42 what the server thought we searched for
15+
00:44 and the hits, the movies that matched.
16+
00:47 So here we can see we've got Blade Runner when we searched for run,
17+
00:51 we've got Running With Scissors and a couple others,
18+
00:55 we've got Kite Runner, things like that.
19+
00:58 So we can go and search for anything, put the keyword up here,
20+
01:01 we could search for particle,
21+
01:03 it comes back with this movie called Particle Fever
22+
01:07 this is an absolutely awesome movie
23+
01:09 about Large Hadron Collider and search for the Higgs Boson,
24+
01:12 one that I really like is one called Capital C,
25+
01:17 this is about crowd funding and kickstarters and stuff
26+
01:20 and this is really cool and interesting movie;
27+
01:23 so let's see what we can find out about that,
28+
01:26 so if we come over here and we search for capital,
29+
01:29 you'll see that we get a couple of things,
30+
01:31 we get Super Capitalists, we get Capital C, that's the one
31+
01:35 I was talking about, Capitalism a Love Story, okay,
32+
01:39 we have things like their IMDB code,
33+
01:42 so we can pull them up on imdb.com and things like that if we wanted.
34+
01:45 We have keywords, genres, the year, rating and so on.
35+
01:49 Okay, so we're going to use this movie service
36+
01:54 to go search for movies by keyword
37+
01:56 we could also search by directory, or we could just pull them up by IMDB,
38+
01:59 so if I click here, you can see if this is searching by Cameron,
39+
02:03 so James Cameron or Cameron Crowe, anyone who matches that,
40+
02:06 here's James Cameron, The Abyss, Terminator, Judgment Day, Titanic,
41+
02:10 somewhere in there is Avatar, things like that.
42+
02:13 So we can search by director or just by IMDB number
43+
02:15 so for example this one pulls up the Abyss by James Cameron
44+
02:19 where the IMDB code is right there.
45+
02:22 Okay, so this works pretty well, I wouldn't use this for a real app
46+
02:27 and just because there's not that much data behind it,
47+
02:30 it's just built for this course, for this specific purpose.
48+
02:34 But there's enough there that you can do some interesting queries;
49+
02:38 if you are looking for a real API that's very similar to this
50+
02:42 it will work in a similar way to what we are doing,
51+
02:44 is you can check out the OMDB API, the Open Movie Database.
52+
02:48 So, this actually used to be free,
53+
02:51 and you would be able to just go and use their API,
54+
02:54 the API is very similar, you could come down
55+
02:57 and you could click on say Capital
56+
03:00 and it will do a request in a really similar way,
57+
03:03 and it will give you things like if we put Capital C
58+
03:07 we could probably get that exact movie back from it, yeah, there you go.
59+
03:11 So, we could do a similar search here but you have to pay for this,
60+
03:13 the minimum is like a dollar a month or something,
61+
03:17 it's not crazy but it turned out that this free thing
62+
03:19 was kind of a victim of its own success,
63+
03:22 it did 44 terabytes of traffic in just one month
64+
03:28 and that turns out to be super expensive.
65+
03:30 I ended up making this one just
66+
03:33 so you have a nice stable simple service to work with,
67+
03:35 but if you want to build a real app, I'd recommend checking out OMDB API.

transcripts/txt/10_app/3.txt

+120
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
1+
00:00 Now we know where our data is coming from
2+
00:02 let's write some code and get started on this app.
3+
00:04 We're going to do it in two steps, so let's take this url here
4+
00:07 this movie service.talkpython.fm/api/search/ the name there
5+
00:13 and let's go over here and we're just going to write
6+
00:16 some sort of play around code in the beginning
7+
00:19 and then we'll structure this into a proper program or proper app in a minute.
8+
00:22 So we're going to go and make a request against this
9+
00:25 and let's make it really clear that there is a search term happening here
10+
00:29 so we'll say format search, search equals this, I'll search for a capital
11+
00:34 and we're going to need to make a request against this service,
12+
00:38 well, we've already seen one of the best ways to do that
13+
00:41 is to use the request package, so import request;
14+
00:45 now I don't have request installed in the virtual environment here
15+
00:49 so we'll go ahead and install that,
16+
00:54 you might be using it your system wide Python
17+
00:56 and have it left over from other examples
18+
00:58 or maybe using the same virtual environment
19+
01:00 but here I'm an going to install it separately;
20+
01:02 we're also going to be using named tuples,
21+
01:04 so I'm going to import collections while we're at it,
22+
01:07 so let's start by going and doing a request against this,
23+
01:10 so we'll say the response is request.get url,
24+
01:13 and then let's just print out the status code
25+
01:16 and go ahead and run this, see what we get.
26+
01:19 Status code 200, that's good, so we could
27+
01:23 actually print out the text that we got back
28+
01:26 that looks like the javascripty json text that we're looking for,
29+
01:30 we could use the json module and parse that,
30+
01:33 but it turns out that if we have let's say I have a variable here
31+
01:39 it turns out that request is very commonly used to talk to these javascript apis
32+
01:43 and it can automatically turn this into the python dictionaries
33+
01:46 that we're going to work with, so now I could print out this
34+
01:50 let's say the type of movie data and maybe data, we'll see what we get back
35+
01:56 so if we run this, we get a dictionary, not the strings, but an actual dictionary
36+
02:00 which we can use to start working with this data.
37+
02:04 So notice we have our keyword capital in our hits
38+
02:07 which is an array of these movies,
39+
02:10 so we're already on a good path here,
40+
02:13 we probably should do some kind of check to make sure everything is ok
41+
02:18 and we could say something like if response.status code is not 200,
42+
02:23 do something, but request has a nice little thing here
43+
02:27 we can say response.raise for status
44+
02:30 so this is going to cause an error an exception
45+
02:33 if something went wrong basically if it's not a successful status code, all right
46+
02:38 and we'll talk about how to handle these types of situations in a little bit
47+
02:42 but for now, let's just keep playing around with this.
48+
02:45 So we've got our movie data, and we actually care about our movies
49+
02:49 and that's going to be going to the movie data
50+
02:52 and we want to get out the hits,
51+
02:54 so let's print out our movies here and see what we get.
52+
02:59 Now this time it shouldn't come back as a dictionary,
53+
03:02 notice the square bracket, this should come back as an array, a list;
54+
03:05 so here's a list, and then these are all of the movies,
55+
03:08 now the next thing we want to do is convert them just from these flat dictionaries
56+
03:14 into something that's more useful for application,
57+
03:18 we've already seen that named tuples are really powerful
58+
03:21 and let us work with data better,
59+
03:23 we could go and create classes like we did in the wizard app,
60+
03:26 but I think probably named tuples are good enough
61+
03:29 for what we're trying to do here, so let's go and set that up.
62+
03:32 Let's call this a movie result and we're going to set that to be a collection.named tuple,
63+
03:39 and we need the name right here to be exactly the same so movie results,
64+
03:43 and then the next thing that goes in here is actually
65+
03:47 a list of all of the fields, and you can see we have rating
66+
03:50 we have duration, we have title, we have imdb code
67+
03:53 and it needs to match up, well, it should match up exactly to this,
68+
03:57 to make it as easy as possible, we could of course transform it
69+
04:00 and if we don't want imdb code we just want to say
70+
04:03 imdb or just code or something like that,
71+
04:05 we could change it but we have to account for that later;
72+
04:08 so our plan is to just use the same field names or attribute names here,
73+
04:12 so I've already copied those over, so you don't have to watch me type them in,
74+
04:16 so we have this movie result and let's go instead of printing these out,
75+
04:21 let's actually go and loop over all of this data that we got back
76+
04:26 and build up a list of these movie results,
77+
04:28 so let's rename this to movie list or something like that
78+
04:32 and then we'll define our movies, this is the thing we really are after
79+
04:35 we really want to work with, we're going to add a bunch of these movie results
80+
04:39 converted from the data we got from the web service.
81+
04:42 So we'll say for md in a movie list, all right
82+
04:48 and then we want to come down here
83+
04:50 and we're going to create one of the movie results
84+
04:53 and I'm going to do this in three different ways,
85+
04:56 I'm going to start out in the most sort of verbose least Pythonic way
86+
05:00 and we're going to improve it in the next video in a couple of different ways.
87+
05:04 So we're going to say m is a movie result,
88+
05:08 then we need to set a bunch of stuff,
89+
05:10 we need to set the imdb code is equal to md.get,
90+
05:14 imdb code we're going to come over here
91+
05:18 and we're going to set the title to be the title
92+
05:21 and I'm just going to knock the rest of these out.
93+
05:27 There we go, that was a lot of typing
94+
05:29 so we've got all of these here and some of these are numbers
95+
05:32 so we might want to be a little careful we could say things like the score
96+
05:37 if you don't have it in here in this dictionary get
97+
05:40 instead of giving us none you could give us zero,
98+
05:43 same maybe for the year or the rating, things like that
99+
05:46 but this is not entirely necessary I believe all of them have a year and rating.
100+
05:50 Okay, so now we're just going to say movies.append,
101+
05:54 and give it this m, so now we could do things like this
102+
05:58 we could just print out something about the movies,
103+
06:04 so we can print them out and something like this
104+
06:06 we could say, we could just put the year and the title
105+
06:09 we could also save print found some number of movies for search something like this
106+
06:14 the length of the movies and the search string that we sent in, okay,
107+
06:20 so let's run this one more time
108+
06:22 and we should go find a couple movies for capital
109+
06:25 and then print them out here. Look at that, searching for capital,
110+
06:28 we get Supercapitalist, Capital C and Capitalism A Love Story,
111+
06:31 let's go and search for a runner, oh look at that,
112+
06:35 we found Blade Runner, Kite Runner, Logan's Run,
113+
06:38 let's make it feel a little more real, I had let the user input something here,
114+
06:42 so let it come down here, alright,
115+
06:45 so now we could do blade for Blade Runner or something like that,
116+
06:48 we got Sling Blade, Dragon Blade, Blade Runner,
117+
06:52 we come down here and look for a runner, get these again beautiful
118+
06:56 all right so it looks like this is working,
119+
06:59 we'll see that we can actually improve upon this in a couple of ways,
120+
07:02 make this much more concise and pythonic.

transcripts/txt/10_app/4.txt

+69
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
00:01 Let's return to how we're parsing these movies here.
2+
00:04 What we're doing is we're creating this movie list
3+
00:06 we're looping over all the results, we're allocating one of these,
4+
00:09 and then we're adding it here, let's address this part first.
5+
00:13 So it turns out that this keyword value keyword value thing that we're doing
6+
00:18 actually has a shortcut, a simplified version,
7+
00:21 when you're working with dictionaries as the source,
8+
00:24 and this md is a dictionary, we'll be able to condense this down
9+
00:28 assuming that the keywords here and the keys in the dictionary
10+
00:33 that it's coming from actually line up exactly,
11+
00:36 if that's the case, you can do what I’m bout to show you.
12+
00:39 So let me save this, let me make a copy of this and we'll try another one,
13+
00:42 so quick diversion so you guys understand what's going on here,
14+
00:46 suppose I have a method or function and it has
15+
00:49 some positional parameters, so like x y and z, right,
16+
00:53 if I'm going to call this method, I would say seven, one
17+
00:58 and I could even say z = 2, right
18+
01:01 so I could either call it positionally like x and y
19+
01:04 or I could call this function using the keyword named parameter thing.
20+
01:08 Some methods are extensible, so I could say format = true, age = seven
21+
01:16 but you can see, PyCharm is saying no, no, this is not going to work;
22+
01:19 so in order to make this sort of extensible,
23+
01:22 to say look you can pass other stuff and maybe we can work with it
24+
01:25 we're going to use this concept that's often called
25+
01:28 in the variable kwargs, keyword arguments.
26+
01:31 Now this name here is just a convention,
27+
01:34 it could be jerry it could be anything, it doesn't matter,
28+
01:37 the symbol that we use is this ** here,
29+
01:41 so if I print out kwargs, like that, if I run this, let's see what we get;
30+
01:48 you can see kwargs its format is true and age as seven
31+
01:53 like this extra step that we passed here, under **kwargs,
32+
01:57 becomes, you can see the curly braces- a dictionary.
33+
02:00 So, with that in mind, there's a way to reverse this,
34+
02:04 so what we're doing here is we're taking keyword arguments
35+
02:07 and we're turning them into a dictionary,
36+
02:10 but we can use in a reversed direction we can use this **
37+
02:13 to go from a dictionary to keyword arguments,
38+
02:16 so what we can do down here is we can replace all of this
39+
02:19 with just going to the dictionary that has the data md
40+
02:23 and we can say ** md, and what that means basically is
41+
02:28 exactly what we had written besides little zeros,
42+
02:31 besides this little part right here where I was saying alternate default values
43+
02:35 but this is what it means, it means go here and get all the values out
44+
02:39 and apply them as keyword arguments.
45+
02:42 So if I run this, it should run exactly the same,
46+
02:45 if I search for 'runner' we should see things like Blade Runner and so on.
47+
02:49 So this is pretty cool, again I said it's only going to work
48+
02:53 if the keyword arguments or the arguments expected
49+
02:56 exactly match up with the keys, if there is
50+
02:59 one is capital title one is lower case title this isn't going to work.
51+
03:03 This is really, really nice. Now, let's do one more thing.
52+
03:07 We can improve it one more time,
53+
03:10 we can come down here and whenever you want to
54+
03:13 create a list and then loop over something,
55+
03:15 and then put some sort of transformed or computed element into the list like this,
56+
03:20 we can do this with a list comprehension.
57+
03:24 So, we saw that before, we can come down here and just say this
58+
03:27 maybe write like this for md in movie list, and what are we going to put in,
59+
03:36 we want to put in this movie result thing,
60+
03:39 so taking the md and converting it to that, there.
61+
03:42 Now, this should run more or less identical, the results should be identical,
62+
03:46 so if we go down here for runner again, we get exactly the same results.
63+
03:52 So look at how amazing this is, look how short and clean and concise,
64+
03:56 we want to go to the movie list and convert it into a list of movie results
65+
04:01 by assigning all the values that are in the dictionary,
66+
04:05 compare that to this big puppy, this one is more flexible
67+
04:09 and maybe you need to use this form like I said
68+
04:12 if the keys don't match up or things like that,
69+
04:14 but I think this is really beautiful and we'll use that in this particular app.

0 commit comments

Comments
 (0)