Skip to content

Commit

Permalink
04 - done, reversed some ID capitalization from 03, because it doesn'…
Browse files Browse the repository at this point in the history
…t match the context later on and would be confusing
  • Loading branch information
hclivess committed Apr 16, 2017
1 parent ba4efb6 commit c3b428c
Show file tree
Hide file tree
Showing 9 changed files with 201 additions and 201 deletions.
2 changes: 1 addition & 1 deletion transcripts/03-Foundational-Concepts/2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
0:30 it checks to see if the database was available and if it's not available,
0:34 it returns None which we know is False.
0:36 Otherwise, it's going to actually do a search against our database
0:40 and return a list of account IDs.
0:40 and return a list of account ids.
0:42 However, there might not be any matching results
0:45 in which case that list could be empty,
0:47 so you might be tempted to say if not accounts,
Expand Down
8 changes: 4 additions & 4 deletions transcripts/03-Foundational-Concepts/7.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,12 @@
0:55 First one uses a little support module here,
0:57 we are going to ask is the download url set,
0:59 if it is, then we are going to check the network status
1:02 and then we are going to make sure that the dns is working
1:02 and then we are going to make sure that the DNS is working
1:04 then finally we are going to check that we have permission to access the file
1:06 and if all those things are true, then we are going to try to download it.
1:11 Otherwise, we are going to say well, this one goes back here
1:14 so it looks like no access
1:15 this one here, no dns,
1:15 this one here, no DNS,
1:17 this one here PyCharm even has little like tiny lines
1:21 that are probably hard to see but I can follow back up,
1:23 no network and then finally this one is bad url.
Expand All @@ -43,8 +43,8 @@
2:09 of course now that we are up here we want to return early,
2:13 we'll say "if not check the network" then we want to say "no network".
2:19 And return, and again, unindent, it's better, again,
2:24 "if not check dns", do something and then return
2:28 we'll print out that there is no dns
2:24 "if not check DNS", do something and then return
2:28 we'll print out that there is no DNS
2:32 and then we'll unindent and finally we'll do this "if not this, return",
2:41 and then if all the guarding clauses pass
2:43 then write at the very edge of our method, not indented at all
Expand Down
22 changes: 11 additions & 11 deletions transcripts/04-dictionaries/1.txt
Original file line number Diff line number Diff line change
@@ -1,31 +1,31 @@

0:01 Now we are going to focus on a really important set of topics 
0:04 revolving around dictionaries in Python. 
0:06 So, the first question you might ask is like 
0:09 why should we focus on dictionaries in Pythonic code? 
0:11 Well it turns out that dictionaries are everywhere in Python, 
0:09 "why should we focus on dictionaries in Pythonic code?" 
0:11 Well, it turns out that dictionaries are everywhere in Python, 
0:15 you'll see that dictionaries are the backing store for many types, 
0:18 so for example when you create a new object from a custom class you've created, 
0:22 every instance has its own backing store 
0:26 which is a dictionary for the fields and what not that you add to this class. 
0:30 Dictionaries are isomorphic with Json
0:25 which is a dictionary for the fields and what not that you add to this class. 
0:30 Dictionaries are isomorphic with JSON
0:33 so you'll see that there is basically a one to one mapping
0:36 between Python dictionaries and Json 
0:35 between Python dictionaries and JSON 
0:38 which is the web's most important transport type. 
0:41 If we want to create a method, that allows us to use keyword arguments, 
0:45 one of the ways we can do that is to have the kwargs, 
0:49 **kwargs parameter and this allows us to 
0:51 not just pass a set of non keyword arguments 
0:48 **kwargs parameter and this allows us to 
0:51 not just pass a set of non-keyword arguments 
0:54 but actually arbitrary ones as well, 
0:56 and those come through as a dictionary. 
0:59 As we'll see, dictionaries add incredible performance boost 
0:58 As we'll see, dictionaries add incredible performance boost 
1:02 for certain types of algorithms, 
1:04 and we'll look at that in the section as well. 
1:06 Python, the language odes not have a switch statement, 
1:10 and we don't miss it too often but sometimes a switch statement 
1:09 and we don't miss it too often but sometimes a switch statement 
1:12 is really nice and you'll see that we can actually leverage dictionaries 
1:16 to add switch statements, 
1:19 switch statement like functionality to the Python language. 
1:21 If you access a database and you are not using an orm such is SQLAlchemy, 
1:17 switch statement-like functionality to the Python language. 
1:21 If you access a database and you are not using an ORM such is SQLAlchemy, 
1:25 typically the way the rows come back to you are each row comes back 
1:29 either as a tuple or a dictionary, preferably as a dictionary 
1:32 so you can lookup the columns by  name rather than index. 
Expand Down
49 changes: 24 additions & 25 deletions transcripts/04-dictionaries/2.txt
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@

0:01 The first area I want to cover around dictionaries
0:04 is using dictionaries for performance. 
0:03 is using dictionaries for performance. 
0:06 So when you have a collection of data, 
0:08 the most  natural choice the thing Python developers reach for first 
0:08 the most  natural choice, the thing Python developers reach for first, 
0:12 has got to be a list. 
0:14 It's super common, super useful and very flexible.
0:17 Let's look at two different algorithms 
Expand All @@ -12,8 +12,8 @@
0:28 but here is the basic way it works. 
0:31 So we start out with a list of data, here you can see in our example 
0:34 we are going to be using half of million items, 
0:37 so we are calling a data_list and it's going to contain some rich objects 
0:40 with multiple values or measurements details aren't actually affected. 
0:37 so we are calling a data list and it's going to contain some rich objects 
0:40 with multiple values or measurements - details aren't actually affected. 
0:46 Then, suppose for some reason we have done some computation 
0:48 and we've come up with a hundred pieces of data 
0:51 we would like to go look up in our list, 
Expand All @@ -25,11 +25,11 @@
1:10 and it just walks through the list, it compares the id 
1:13 it's looking for against the ids that it finds in the list, 
1:15 as soon as it finds the match, it returns that one, 
1:17 it's assuming to be unique and then it appends it to this interesting points
1:17 it's assuming to be unique and then it appends it to this interesting_points
1:21 So this is one version of the algorithm.
1:24 The other one is well, maybe we could do a little bit better 
1:27 if we actually used a dictionary. 
1:29 And then we could index the key for the dictionary could be the id. 
1:29 And then we could index - the key for the dictionary could be the id. 
1:32 So if we wrote it like this, here we have a dictionary of half a million of items, 
1:37 again the same dynamically discovered 100 interesting points, 
1:42 and instead of doing this lookup by walking through the items, 
Expand All @@ -41,8 +41,8 @@
2:01 if we are going to do this a 100 times, we have half a million items, right, 
2:04 it's much more complicated to generate a dictionary with half a million items 
2:08 than it is a list. 
2:11 So let's see this in action and see what the verdict is. 
2:13 So here we have our data point, our data point is a named tuple, 
2:10 So let's see this in action and see what the verdict is. 
2:14 So here we have our data point, our data point is a named tuple, 
2:18 it could have been a custom class but named tuple is sufficient 
2:21 and it has five values, id, an x y for two dimensional coordinates, 
2:26 a temperature and a quality on the measurement of the temperature. 
Expand All @@ -52,7 +52,7 @@
2:36 In PyCharm you can highlight these and hit command period 
2:37 and turn them into little collapsible regions, 
2:40 so I did that so that you can focus on the algorithm and not the little details. 
2:44 Here we have our data list that we are going to work with, 
2:44 Here we have our data_list that we are going to work with, 
2:47 and we are going to use a random seed of zeros 
2:50 so we always do exactly the same thing, but randomly, 
2:53 so that we have perfectly repeatable results 
Expand All @@ -64,7 +64,7 @@
3:08 since we are using auto incrementing ids, 
3:12 next we are going to create our set of interesting ids 
3:14 that we are going to go search through our list, 
3:17 and then later through our dictionary. 
3:18 and then later through our dictionary. 
3:19 Really we would use some kind of algorithm 
3:21 and we would find interesting items we need to go look up, 
3:23 but in this case we are just going to randomly do it, 
Expand All @@ -87,8 +87,8 @@
4:19 but now we have the set of approximately 100 ids, 
4:22 assuming that there is no conflicts or duplication there, 
4:27 and next thing we are going to do is we are going to come along here, 
4:32 we are going to start a little timer figure at the end what the total seconds pass were,
4:35 and during that time we want to go and actually pull out the interesting points 
4:29 we are going to start a little timer figure at the end what the total seconds pass were,
4:34 and during that time we want to go and actually pull out the interesting points 
4:38 that correspond to the interesting ids.
4:40 So we are going to go for each interesting id, 
4:42 remember, it's about a 100, we are going to say find the point in the list like so, 
Expand All @@ -106,38 +106,37 @@
5:26 All right, so let's take this algorithm here and adapt it for our dictionary. 
5:31 So I've got a little place holder to sort of drop in the timing and so on, 
5:34 you don't have to watch me type that, 
5:36 so the first thing we want to do is create a dictionary,
5:38 before we had data list, now we are going to have data dict
5:35 so the first thing we want to do is create a dictionary,
5:38 before we had data_list, now we are going to have data_dict
5:40 we can create this using a dictionary comprehension, 
5:43 so that would be a very Pythonic thing to do 
5:45 and we want to map for each item in the dictionary the id to the actual object. 
5:49 So, we create set and dictionary comprehensions like so 
5:53 but the difference is we have a key colon value for the dictionaries 
5:57 where we just have the value for sets. 
5:59 You kind of have to write this in reverse, 
6:02 I am going to name the elements we are going to look at d,
6:04 so I am going to say d.id, maps the d for d in data list, right, 
6:00 I am going to name the elements we are going to look at "d",
6:03 so I am going to say "d.id", maps the "d for d in data_list", right, 
6:11 so this is going to create a dictionary of all half a million items 
6:13 and mapping the id to the actual value. 
6:16 So now, let's start a little timer, 
6:18 and next we want to locate the items in the dictionary, 
6:21 so again, we'll say interesting points, let's clear that; 
6:24 for id, we call it d.id so it doesn't conflict with the id built in, 
6:21 so again, we'll say interesting_points, let's clear that; 
6:25 for id, we call it d.id so it doesn't conflict with the id built in, 
6:31 so for d.id in interesting ids we want to do a lookup, 
6:35 we'll say the data element is now we have a dictionary 
6:38 and we can look up things by id so that is super easy, 
6:41 we just say like so, assuming that there is none of the id 
6:35 we'll say "the data element is", now we have a dictionary 
6:38 and we can look up things by ids, so that is super easy, 
6:41 we just say it like so, assuming that there is none of the id 
6:44 that is missing, something like that 
6:46 and then we'll just say interesting points.append d. 
6:49 .
6:45 and then we'll just say interesting_points.append(d)
6:53 Oops, almost made a mistake there, 
6:55 let's say d.id not the built in, that of course won't work.
6:55 let's say "d.id" not the built in, that of course won't work.
6:58 All right, so let's run it again and see how it works, 
7:00 so we are going to run, it's still going to run the other slow version, 
7:02 I'll skip that in the video, 
7:05 wow,  look at that, 8 seconds, and this is 0.000069 seconds. 
7:13 So that's less than 1 millisecond, by a wide margin.
7:16 That is a non trivial speed up, let's see how much of a speed up that is, 
7:16 That is a non-trivial speed up, let's see how much of a speed up that is, 
7:21 then the other thing to consider as well, 
7:24 maybe the speed up was huge 
7:26 but the cost of computing the dictionary was 
Expand Down
44 changes: 22 additions & 22 deletions transcripts/04-dictionaries/3.txt
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@

0:01 Next, let's talk about taking multiple dictionaries
0:03 and combining them into one that we can use
0:04 to look up items that came from various places.
0:07 Here in PyCharm, I have a sort of web example for you,
0:03 to look up items that came from various places.
0:08 Here in PyCharm, I have a sort of web example for you,
0:11 imagine that you are writing a web app, 
0:14 there is data coming from different locations into your action method, 
0:18 so on one hand we have routing setup that is passing data through the URL, 
0:22 we might be in that location, passing the id and the current value might be this, 
0:27 the title might be that, we also might have the query string 
0:30 and the query string might have some other value for id like 1, 
0:33 it might have a separate value it's adding to the mix here called render fast is true
0:38 then maybe we are also as part of posting long url 
0:30 and the query string might have some other value for id, like 1, 
0:33 it might have a separate value it's adding to the mix here called render_fast is True
0:38 then maybe we are also, as part of posting long url 
0:42 which matches the route with the query string we are posting back a form 
0:45 and that form has email, a name and it is well. 
0:49 So here I am just going to print out these three dictionaries 
0:51 just so you can see what they look like, and no surprise, here they are, 
0:55 they just look like basically they are written here, 
0:57 so what if I want one dictionary that I could just ask what is the id, 
1:01 what is the user name or what is the email address 
0:57 so what if I want one dictionary that I can just ask - "what is the id, 
1:01 what is the user name or what is the email address" 
1:06 and not have to worry about which one is located 
1:09 and we have our super non Pythonic way here 
1:09 and we have our super non-Pythonic way here 
1:12 and I am going to use this dictionary called m1, 
1:15 we'll go like this, and I'll say for k in and what we are going to do 
1:15 we'll go like this, and I'll say for "k in" and what we are going to do 
1:19 is we are just going to loop over each dictionary and stick the value in there. 
1:23 The order in which we do this matters. 
1:25 So let's suppose we want the query string that has the least value, 
Expand All @@ -29,17 +29,17 @@
1:34 and we can just set the value for whatever the key is, 
1:38 to whatever the value and the query dictionary, here's like that. 
1:43 And, we'll do the same thing for let's say 
1:47 the next thing we are going to do is the post, 
1:50 like that, and finally, I'll do it for the route.
1:55 Ok, so if we run it we should get a dictionary because the route has higher priority, 
1:59 with id 27, title fast apps, render fast is true and then that data in it. 
2:04 Let's run it, all right, id 271, like we expected, jef fast apps, perfect, 
1:46 the next thing we are going to do is the post, 
1:50 like that, and finally, we'll do it for the route.
1:55 Ok, so if we run it, we should get a dictionary because the route has higher priority, 
1:59 with id 27, title Fast apps, render_fast is True and then that data in it. 
2:04 Let's run it, all right, id 271, like we expected, Jef, Fast apps, perfect, 
2:11 so it looks like you combined it well, 
2:13 but this is a very procedural way, and there is better ways, 
2:16 so in Python down here we can actually sort of improve upon this 
2:21 by leveraging a couple of methods on the dictionary, 
2:24 so we can go like this, and remember 
2:26 we wanted the query first so we can say query.copy 
2:26 we wanted the query first so we can say "query.copy"
2:29 and actually create  a copy of the dictionary 
2:31 and then we can go here and we can say update, 
2:34 I would like you to update your values possibly overwriting them with post, 
Expand All @@ -55,18 +55,18 @@
3:08 But in the end, we write these better versions here, we should be good. 
3:11 We can actually do this in one line with the dictionary comprehension, 
3:14 it's not pretty but it does work. 
3:17 So, let's imagine where they end, we can say for d in 
3:17 So, let's imagine where they end, we can say "for d in" 
3:22 and we can put all of our dictionaries in the order we care about, 
3:25 query, post and route and so for each one of those we can come back here
3:30 and say we would like the key colon value and we'll go for each one of these, 
3:35 we'll say for k,v in d.items. 
3:42 So here you can see exactly the same dictionary ordering, 
3:35 we'll say "for k,v in d.items" 
3:42 So here you can see exactly the same dictionary, matching ordering, 
3:45 let's do a little formatting on that. 
3:48 Ok, so we are looping over all the dictionaries 
3:50 and for each dictionary we are looping over, 
3:53 we are looping over and point out the key 
3:56 and the value from the items and then we are creating the dictionary from that.
3:59 So, this works, I really don't like it very much even though it happens in one line
3:59 So, this works, I really don't like it very much, even though it happens in one line
4:04 and it feels like oh that must be Pythonic because look at it, 
4:07 it's cool and special and declarative. 
4:10 To me it's hard to look at it and not go oh, 
Expand All @@ -81,8 +81,8 @@
4:35 and we want to take a dictionary and pass it as keyword arguments here 
4:39 I could say **query say, 
4:41 if I wanted to pass the values from the query,
4:44 well they kind of apply that same syntax here to say 
4:46 I would like to merge all the values from let's start with query, 
4:43 but they kind of apply that same syntax here to say 
4:46 I would like to merge all the values from - let's start with query, 
4:51 all the values from post and all the values form route. 
4:55 so this does not work on Python 3.4 and below so be careful. 
5:00 It's only Python 3.5 and above, 
Expand All @@ -95,7 +95,7 @@
5:25 we have the clever but too clever in my opinion way of 
5:29 looping over the dictionaries and looping over their key value items 
5:33 and recombining them,
5:35 and then we have the quite sleek Ptyhon 3 way of the ** dictionary 
5:35 and then we have the quite sleek Ptyhon 3 way of the **dictionary 
5:39 to unpack them, back into a dictionary. 
5:41 And notice, it has the same overwriting process where the id from the query 
5:46 was overwritten by the id from the route because the route came last. 
Expand Down
Loading

0 comments on commit c3b428c

Please sign in to comment.