Skip to content

Commit fd0bc97

Browse files
authored
Merge pull request mikeckennedy#23 from diazgilberto/gd-app-10
Transcript for app 10
2 parents 5972d4f + 343c5a5 commit fd0bc97

File tree

12 files changed

+81
-90
lines changed

12 files changed

+81
-90
lines changed

transcripts/txt/10_app/1.txt

+5-6
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@
33
0:06 into a particularly unreliable environment, 
44
0:10 so we are going to have to do a lot of work 
55
0:12 to make sure our movie search app is durable 
6-
0:15 and it doesn't crush or behave too weirdly. 
6+
0:15 and it doesn't crash or behave too weirdly. 
77
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. 
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. 
1010
0:27 So, here we might say Top Gun,
1111
0:30 and we'll try to go out to the internet and find information 
1212
0:33 about movies with Top Gun in their title, 
1313
0:35 but you can see in this case it said oh sorry, 
1414
0:37 the network happens to be down so no searching right now. 
15-
0:41 But our app doesn't crush or give weird errors it just says hey, the network is down, 
15+
0:41 But our app doesn't crash or give weird errors it just says hey, the network is down, 
1616
0:45 check your wi-fi or something like this. 
1717
0:47 Then later we try again, search for Top Gun and boom, 
1818
0:49 we get real live movie search results off the internet. 
@@ -36,5 +36,4 @@
3636
1:52 and ultimately, we are going to talk about writing reliable code 
3737
1:56 and you also see how to raise errors and exceptions 
3838
1:59 not just catch them because we are going to throw some 
39-
2:02 to sort of make our app particularly unreliable. 
40-
39+
2:02 to sort of make our app particularly unreliable.

transcripts/txt/10_app/2.txt

+3-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
1-
0:00 Let's talk briefly about where are we going to get these movie information 
1+
0:00 Let's talk briefly about where are we going to get these movie information 
22
0:03 and what we are going to search for anyway. 
33
0:06 So we are going to use the OMDB API, 
44
0:09 the open movie database API, so that's omdbapi.com 
55
0:14 and this is a free service where we can go and do queries 
6-
0:17 against presumably imdb type of information 
6+
0:17 against presumably imdb type of information
77
0:21 and you see it says the API looks like this, 
88
0:24 and notice that you can get poster images as well, 
99
0:28 so if you want to get the particular poster you are going to need an API key
@@ -37,5 +37,4 @@
3737
2:05 we will parse it and then each one of these is going to come back like so,
3838
2:08 and when we parse that from json, 
3939
2:10 it will come back as a dictionary with a key of title, a key of year and so on. 
40-
2:13 And let's go get the searching. 
41-
40+
2:13 And let's go get the searching.

transcripts/txt/10_app/3.txt

+11-12
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,25 @@
1-
0:00 Let's just play around with this api really quick, 
1+
0:00 Let's just play around with this api really quick, 
22
0:02 get the general idea of it and then we'll structure into a proper program, ok? 
33
0:07 So our url is going to look something like this 
44
0:09 and we search for capital 
55
0:11 and so let's make it really clear that there is some search text that goes right there. 
6-
0:16 So we'll say search=this and then we'll do a .format, with the search. 
6+
0:16 So we'll say search=this and then we'll do a .format(), with the search. 
77
0:21 Now what we are going to do is we are going to work wit Request 
88
0:23 we've used that a few times and it's been really great,
99
0:26 we are also going to work with named tuples again, 
1010
0:28 so while I am here let me just type import collections. 
1111
0:31 So what I want to do is go and download this data 
12-
0:34 so we'll say r for response=request.get url. 
12+
0:34 so we'll say r for response=request.get(url)
1313
0:38 Now, I should test our .status code and all those things 
1414
0:42 but we are just playing around, 
15-
0:45 so I am going to pull this back  and say 
15+
0:45 so I am going to pull this back and say 
1616
0:45 the data is going to come back in json format 
1717
0:47 so instead of taking that as a string, 
1818
0:49 we are going to convert that to dictionaries and lists 
1919
0:53 and primary sort of fundamental values like numbers and dates and so on. 
2020
0:58 So remember how the results come back,
2121
1:00 there is an outer json object, a dictionary, it has a key called Search, capital S
22-
1:06 is in this case instead of, and in there, there is an array, 
22+
1:06 it is case sensitive, and in there, there is an array, 
2323
1:08 and this is what we are really after. 
2424
1:10 So let's go over here and we'll say results and just write this 
2525
1:14 like it's going to work and then we can add error handling, later. 
@@ -48,7 +48,7 @@
4848
2:33 so for this to work in the cool and sleek way that I am proposing, 
4949
2:37 the values need to have exactly the same name as they do in the dictionary, 
5050
2:42 and they are not really matching Pythonic style with lower case elements, 
51-
2:46 upper case, more something like CSharp or C++ 
51+
2:46 upper case, more something like C# or C++ 
5252
2:50 but we are going to make it work here. 
5353
2:53 All right, so let's take this and instead of having this dictionary that we use strings against,
5454
2:58 we'll have a strongly typed element called movie results, right, 
@@ -58,9 +58,9 @@
5858
3:13 in two dimensions if you will, 
5959
3:15 and then I'll show you two cool ways to improve this 
6060
3:18 so we are going to have a list, 
61-
3:21 we are going to say for result in results, plural, 
61+
3:21 we are going to say for result in results:, plural, 
6262
3:24 now I want to create a movie, it's going to be a movie result 
63-
3:28 and in here we want to say things like title=results of title
63+
3:28 and in here we want to say things like title=results['Title']
6464
3:33 and we can just knock this out like so, 
6565
3:38 ok so now we have a movie result, 
6666
3:40 let me just print out m as we loop across these so let's run this, ok, 
@@ -75,9 +75,8 @@
7575
4:11 we are converting that or upgrading it if you will into a movie result 
7676
4:15 and if we print it we can see them there, that's great, 
7777
4:19 but what we wanted to do is actually store these into our list 
78-
4:22 so we'll say movies.append (m), and then down at the bottom, 
78+
4:22 so we'll say movies.append(m), and then down at the bottom, 
7979
4:27 let's just print out the first one again.
80-
4:32 Print movies of 0, awesome, 
80+
4:32 print(movies[0]), awesome, 
8181
4:34 so you can see our movie result was converted properly 
82-
4:35 title is this poster is that and so on. 
83-
82+
4:35 title is this poster is that and so on.

transcripts/txt/10_app/4.txt

+6-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
0:37 kind of like self is the convention but not required for classes 
1313
0:40 and I can tell Python **kwargs and that means this is going to be 
1414
0:46 sort of extensible set of parameters that can be passed 
15-
0:49 and is actually going  to come in such that this is a dictionary. 
15+
0:49 and is actually going to come in such that this is a dictionary. 
1616
0:53 With that in mind, what you can do is 
1717
0:55 you can kind of do the reverse with dictionaries,
1818
0:58 so this takes a bunch of arguments that are written like this, 
@@ -25,13 +25,13 @@
2525
1:21 Now if I run it should operate exactly the same boom,
2626
1:25  it does, you can see title set, poster set and everything isn't that cool? 
2727
1:29 That's improvement one.
28-
1:31 Let's keep going, improvement two is 
28+
1:31 Let's keep going Improvement two is 
2929
1:34 we saw with list comprehensions and generator expressions 
3030
1:38 that we don't really need to make a list and append to it 
3131
1:41 if all we want to do is build up this list, 
3232
1:43 so we can come over here and say you know what, 
33-
1:45 movies are actually going to be some kind of movie results object 
34-
1:51 that is generated from let's call this, something simple like m for m in results. 
33+
1:45 movies are actually going to be... some kind of movie results object 
34+
1:51 that is generated from let's call this, something simple like m for m in results:
3535
1:58 So we are going to basically loop over the results, get the dictionary m, 
3636
2:03 we are going to unpack it, into this movie results named tuple 
3737
2:07 and for each one of these we are going to build a list that has these items in here. 
@@ -44,12 +44,12 @@
4444
2:33 but I much prefer to read in this style, compare that, those 3 lines, to this. 
4545
2:39 When you hear people talk about Pythonic code, 
4646
2:42 that means it's conforming to the sort of best and proper idioms
47-
2:46 that are part of the Python language, you see code written like this, 
47+
2:46 that are part of the Python language. You see code written like this, 
4848
2:50 you are like oh these people are taking some other language idioms 
4949
2:54 like maybe C or Javascript or something like that 
5050
2:56 and just converting it over just such that it will run in Python 
5151
3:01 but not like deeply embracing the language, 
5252
3:04 down here, this is an example of something you might call Pythonic, I mean, 
5353
3:08 look how much more amazingly readable and awesome this is, 
5454
3:11 it's easy to maintain, now granted you've got to know Python a little better
55-
3:14 to understand what's going on, but once you do, oh how sweat. 
55+
3:14 to understand what's going on, but once you do, oh how sweat.

transcripts/txt/10_app/5.txt

+8-9
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
0:00 All right, let's take the concepts that we had in our little playground script, 
1+
0:00 All right, let's take the concepts that we had in our little playground script, 
22
0:04 and let's move them over here to do a more proper application.
33
0:07 So here is our program app and just like all of it's got a main method,
44
0:11 it prints this little header, then it has this thing I am calling search event loop, 
@@ -8,7 +8,7 @@
88
0:21 Here we have just a search text 
99
0:23 and we are just going to go while they are not entering x, which means exit in our world, 
1010
0:27 and if they entered something they do want to search about 
11-
0:29 we are going to go and call movie client.search 
11+
0:29 we are going to go and call MovieClient(search)
1212
0:31 and then maybe we'll print out client, 
1313
0:34 to sort of just initialize it and then we can say perform search that will actually do it 
1414
0:39 and return the results, as a list of movie results named tuple kind of like we just saw. 
@@ -31,16 +31,16 @@
3131
1:34 and building up a list via list comprehensions. 
3232
1:39 Maybe we could do one more thing, 
3333
1:41 maybe we want to sort by title I don't know we could do that if we wanted, 
34-
1:44 we could say movies.sort(key=lambda m: m.title), like that, all right, 
34+
1:44 we could say movies.sort(key=lambda m: m.Title), like that, all right, 
3535
1:51 so this works great, let's just run it one more time, 
3636
1:53 let's search for a capital and let's actually change the output, 
3737
1:57 so we can see decent results down there, 
38-
1:59 so we are just printing out the list, let's say like this print found {} results, 
38+
1:59 so we are just printing out the list, let's say like this print("Found {} results.")
3939
2:06 we'll do a little format there and len of let's go up here and say results, 
4040
2:12 it was client.perform search and we'll say results here, 
41-
2:16 yeah, we'll say for r in results, print and well just do like this, 
41+
2:16 yeah, we'll say for r in results:, print and well just do like this, 
4242
2:21 we'll say year--title.format. 
43-
2:25 So here well just say r.year, r.title so let's go look for capital again, 
43+
2:25 So here well just say r.Year, r.Title so let's go look for capital again, 
4444
2:31 here you can see these results pretty cool, 
4545
2:35 I found a bunch of stuff about capital and the one that was actually cool 
4646
2:38 is this one from 2014 capital c. 
@@ -51,13 +51,12 @@
5151
2:50 but let's suppose something happens like our network goes down, right here, 
5252
2:57 so no network, turned it off, let's see how this bad boy runs now,
5353
3:01 I am looking for cats remember, cats and dogs was our first result 
54-
3:05 and uuu this is bad, so we have a request.exceptions.connection error
54+
3:05 and uuu this is bad, so we have a request.exceptions.ConnectionError
5555
3:11 the internal details are the connection pool with these settings failed 
5656
3:16 and this was caused by a new connection error 
5757
3:18 which was caused by da da da da da there is no network. 
5858
3:21 Ok, so what we need to do is write some code so that instead of doing this
5959
3:25 it just says it keeps going to this loop and it says hey,
6060
3:29 that didn't work so well, looks like your network is down. 
6161
3:32 If you turn it back on, we would be able to work again, 
62-
3:35 so let's go do that next. 
63-
62+
3:35 so let's go do that next.

transcripts/txt/10_app/6.txt

+12-13
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
0:00 We have our network working again, we searched for cats and dogs, 
1+
0:00 We have our network working again, we searched for cats and dogs, 
22
0:03 we should get things about cats and dogs, but if we turn off it's going to crash.
33
0:09 Not so great. 
44
0:11 Another thing maybe we want to do is we might want to check 
@@ -13,41 +13,40 @@
1313
0:42 often exceptions are the preferred way to do things in Python. 
1414
0:46 So let's come down here and well do a little test 
1515
0:49 and we can say something like this, 
16-
0:51 if not search text or not search text.strip, 
16+
0:51 if not search text or not search text.strip()
1717
0:56 we need this first test here because if search text comes in as none,
1818
1:01 this will actually give us an exception trying to call it strip on none, ok. 
1919
1:06 So either it's empty or it's not entirely empty but it's white space, 
2020
1:11 then we want to basically cause some kind of error, 
2121
1:15 we want to report an error back 
22-
1:17 and we can say raise value error, it's one possible option here, 
22+
1:17 and we can say raise ValueError, it's one possible option here, 
2323
1:22 we can say so let's try this, if I go down here and I hit cats, 
24-
1:25 I should get network error, if I come down here 
25-
1:29 and I hit empty value error you must specify a search string, 
24+
1:25 I should get NetworkError, if I come down here 
25+
1:29 and I hit empty ValueError you must specify a search string, 
2626
1:33 ok, so we are no longer going to be sort of sending off meaningless searches 
2727
1:37 and in fact we can't create a movie client in a malformed state, 
2828
1:42 we'll never be able to even allocate and initialize an object 
29-
1:46 instead we'll raise this value error, ok. 
30-
1:48 Now, in both cases, it didn't really matter what we do, our app is crushing,
29+
1:46 instead we'll raise this ValueError, ok. 
30+
1:48 Now, in both cases, it didn't really matter what we do, our app is crashing,
3131
1:52 we'd like to just go no that didn't work, keep going, 
3232
1:55 so the way we do that in Python is we come down here 
33-
1:59 and we use what's called a try block so we say try, 
33+
1:59 and we use what's called a try block so we say try:
3434
2:02 and here is all the code we would like to interact with 
3535
2:04 and if anywhere along the way,
3636
2:06 if this allocation this initialization threw us an exception 
3737
2:10 because of here or the perform search those an exception
3838
2:13 because the network is down or something like this, 
3939
2:15 regardless of where the error happens, 
4040
2:17 we'd like to immediately start running there 
41-
2:20 and go deal with the error so we can say except, and for now, 
42-
2:22 we'll just say print yikes, that sucked, ok, 
41+
2:20 and go deal with the error so we can say except:, and for now, 
42+
2:22 we'll just say print('YIKES, THAT SUCKED'), ok, 
4343
2:26 so if we try this, notice there is a little warning here 
4444
2:29 but this should help let's go hit enter, yikes, that didn't work, 
4545
2:31 search for something else, oh, that didn't work, turn the network,
4646
2:36 if I come down here search for cats now, hey, it worked, 
47-
2:39 so it's no longer crushing, that's really good 
47+
2:39 so it's no longer crashing, that's really good 
4848
2:41 but you'll see that PyCharm is warning us about this 
4949
2:44 and we should improve it, it says, you are doing too broad of a catch, 
5050
2:49 you are just catching everything 
5151
2:51 and there is no meaningful information here giving back about it, 
52-
2:54 let's do that next. 
53-
52+
2:54 let's do that next.

0 commit comments

Comments
 (0)