Skip to content

Commit

Permalink
05-Generators done
Browse files Browse the repository at this point in the history
  • Loading branch information
hclivess committed Apr 16, 2017
1 parent 0a389e7 commit ba3a366
Show file tree
Hide file tree
Showing 7 changed files with 195 additions and 190 deletions.
46 changes: 23 additions & 23 deletions transcripts/05-Generators/1.txt
Original file line number Diff line number Diff line change
@@ -1,51 +1,51 @@

0:01 Let's talk about collections, list comprehension,
0:04 generators and generator expressions.
0:03 generators and generator expressions.
0:06 All of these concepts are extremely central to this idea of Pythonic code, 
0:10 many of them are very unique to Python actually. 
0:13 The first item we are going to look at is iteration. 
0:16 We saw that Python does not have a numerical for loop, 
0:19 there is no for i=0 i++ style loop, you literally work with sequences, 
0:16 We saw that Python does not have a numerical "for loop"
0:19 there is no "for(i=0; ;i++)" style loop, you literally work with sequences, 
0:24 you iterate over collections and so on. 
0:26 There is many built in types that work that way
0:29 such as lists and dictionaries and so on, 
0:28 such as lists and dictionaries and so on, 
0:31 but if we had our own type we defined, 
0:34 we might want to be able to iterate over it as well. 
0:36 Here is the shopping cart class, 
0:33 we might want to be able to iterate over it as well. 
0:35 Here is the ShoppingCard class, 
0:38 and you can add items to it that you are going to buy later,
0:41 possibly we'd like to create an API such that you can 
0:44 iterate over the shopping cart and get the items back. 
0:46 Let's have a look over in PyCharm and see how that goes. 
0:50 So here is basically the same code and we are defining this thing 
0:53 called the cart item and it's just really a container for a name and a price, 
0:57 down on line 15 here, we are going to add three items to our cart,
1:01 a guitar, a cd and an iphone
1:04 What if we wanted to loop over our cart- maybe it works right now, 
1:08 let's just try, so if we want to write the code for item in cart, 
1:01 a guitar, a cd and an iPhone
1:04 What if we wanted to loop over our cart - maybe it works right now, 
1:08 let's just try, so if we want to write the code "for item in cart:"
1:12 maybe we'll just print this out, so we'll print, 
1:16 let's do the name and we'll do the price here, 
1:19 we'll do a little format so we'll say item.name, item.price. 
1:25 And let's do a little header here so items in your cart. 
1:25 And let's do a little header here, so items in your cart. 
1:28 You can see that PyCharm is warning us 
1:30 we are kind of going down a bad path here so it's like 
1:34 this is not going to work but let's go ahead and give it a try,
1:30 we are kind of going down a bad path here, so it's like 
1:34 "this is not going to work", but let's go ahead and give it a try,
1:35 just to see what the error is. 
1:38 Boom shopping cart object is not iterable. 
1:41 Ok, so we'd like to write this code but how do we do it
1:38 Boom, shopping cart object is not iterable. 
1:41 Ok, so we'd like to write this code but how do we do it?
1:44 the ability to add iteration to a type is based on the Python data model 
1:47 which all the dunder methods comprise. 
1:50 So we can come up here and add this particular one, 
1:54 we can say def__iter__ and form this method 
1:56 we have to return iterator object which has a length and next. 
2:01 If we just want to loop over the items as they are 
1:53 we can say "def __iter__" and form this method 
1:56 we have to return iterator object, which has a length and next. 
2:01 If we just want to loop over the items as they are, 
2:03 we can leverage the underlined collection class itself 
2:06 and it knows how to create one of these 
2:08 so we could just say self.items.__iter__ 
2:07 so we could just say "self.items.__iter__" 
2:12 go back down here, PyCharm is happy, that's a good sign, let's see if it works. 
2:17 Boom, items in your cart, guitar, cd, iphone, beautiful
2:16 Boom, items in your cart: guitar, cd, iPhone. Beautiful
2:20 What if we wanted to have a little more control than just exposing
2:24 the underline structure, or underline item here, 
2:27 what if we wanted to say sort these and then hand them back?
2:31 We can come over here and we could say sorted items=sorted 
2:27 what if we wanted to say "sort these and then hand them back"?
2:31 We can come over here and we could say sorted_items = sorted 
2:36 and we could pass self.items, and we could pass a key selector, 
2:41 we could say here is a lambda that given an item is going to return item.price, 
2:46 and then we can return sorted_items.__iter__ 
Expand All @@ -56,8 +56,8 @@
3:08 So you might think that this is fairly distasteful here and I don't really like it either, 
3:12 we are going to talk more later about generators, 
3:16 but if you are familiar with the yield keyword, 
3:19 we could write something like this for i in sorted items 
3:23 yield i, we could write this code as well, 
3:19 we could write something like this: "for i in sorted items 
3:23 yield i", we could write this code as well, 
3:26 and this would do basically the same thing, 
3:29 it returns the generator rather than list but that's fine. 
3:33 So take your pick, we'll talk more about yield later. 
Expand Down
38 changes: 19 additions & 19 deletions transcripts/05-Generators/2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,57 +2,57 @@
0:05 If you want to look for an item in a set,
0:07  in a dictionary, in a list, those types of things, 
0:10 and you are new to Python you might look for some kind of find
0:14 or index of type of method on the type itself.
0:13 or index of type of method on the type itself.
0:17 But in Python, we have a special keyword to do this test, 
0:20 over here in PyCharm we have a list, 
0:22 a set and a dictionary and the way we test
0:24 for containment in them all the same, 
0:26 so if we'd run it, you can see we are just printing out these values 
0:30 and if you look at the numbers, you probably recognize them 
0:29 and if you look at the numbers, you probably recognize them 
0:33 as the Fibonacci sequence up to 34 anyway 
0:36 or five here where I had to write them out, 
0:38 so what we are going to do is we are going to parse out 
0:40 a number gathered from the user,
0:42 and then we are going to test whether this is in the set. 
0:45 So, here we'll just do a few if tests and maybe we can do this 
0:45 So, here we'll just do a few "if" tests and maybe we can do this 
0:48 as a tertiary sort of expression, 
0:51 so we can say print like so, so we are going to say 
0:51 so we can say "print" like so, so we are going to say 
0:54 we'll print out something is in the set 
0:57 and then we'll do out if test we'll say if in is in nums list 
0:57 and then we'll do out "if" test, we'll say "if n is in nums_list"
1:04 and then maybe say a list here, keep the same order, 
1:07 otherwise we'll say not in list. 
1:10 All right, so the test here is n in nums list, all right, 
1:07 otherwise we'll say "not in list"
1:10 All right, so the test here is "n in nums list", all right, 
1:14 so this actually goes through and it searches the elements in it 
1:17 and it does a comparison not on index but by value, 
1:16 and it does a comparison not on index but by value, 
1:20 and then it'll tell you yes or no it's here,
1:22 then we could do the same thing as you'll see for the set 
1:25 and we could also do it for the dictionary. 
1:30 All right, let's run it and figure it out, 
1:32 here it says enter a number to test for the small Fibonacci set or a sequence 
1:36 and let's say well, say 21, 21 is in  the list, 21 is in the set, 
1:41 but because I was lazy and didn't write them all out 21 is not in the dictionary; 
1:41 but because I was lazy and didn't write them all out, 21 is not in the dictionary; 
1:45 let's try again, how about 1, it should be in all of them yes, it's in. 
1:48 So, if item in container, this even works for strings, 
1:48 So, "if item in container", this even works for strings, 
1:54 so if we had some text here like "Why did the multithreaded chicken cross the street?" 
1:59 do you know? Well it depends on when you ask it, 
2:03 you'll always get a different answer, 
2:04 this time we are going to get "Other side to the get".
2:08 So I could ask a question word, so here we'll say 
2:14 something we'll do this not the tertiary way, we can say something like this,
2:17 if word in text print such and such is in such and such, there, 
2:29 so we could say ask user for a word, they type it in, 
2:14 something, we'll do this not the tertiary way, we can say something like this,
2:17 "if word in text: print" such and such is in such and such,
2:29 there, so we could say ask user for a word, they type it in, 
2:32 we can do the same in test for a string, let's try, 
2:36 first look for 7 which should not be there, now I'll look for chicken, 
2:42 chicken is in the "Why did the multithreaded chicken cross the street", 
2:45 let's try it again, this time we'll enter 2 and here we'll put a cat. 
2:51 Right, so cat I don't believe appears in here. 
2:54 Cat is not in this string. 
2:53 Cat is not in this string. 
2:56 All right, so let's see that in a graphic, 
2:58 so here we are just going to work with dictionaries as you saw, 
3:00 it's basically the same across the three types of containers we worked with. 
3:02 Here we could try to directly index into this dictionaries and say 
2:57 so here we are just going to work with dictionaries as you saw, 
2:59 it's basically the same across the three types of containers we worked with. 
3:02 Here we could try to directly index into this dictionary and say 
3:06 I want the thing with key 2 but as we saw in other examples, 
3:09 this could give us a key error, if it's not there, 
3:09 this could give us a KeyError, if it's not there, 
3:11 so we might want to do this sort of check first style 
3:14 so we could say if 2 is in the dictionary,
3:17 then we can safely access it because we know it's not going to key error
3:17 then we can safely access it because we know it's not going to KeyError
88 changes: 44 additions & 44 deletions transcripts/05-Generators/3.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,98 +15,98 @@
0:40 we could do a little loop and gather them up and break out of it 
0:43 after we get to five and so on, 
0:45 but in Python, we can go to our list, 
0:48 and we can just say I would like to go from the zeroth item, 
0:52 up to but not including the fifth item, something like this, 
0:48 and we can just say: "I would like to go from the zeroth item, 
0:52 up to but not including the fifth item", something like this, 
0:55 and then if I print first five, you'll see we actually get the first five. 
1:01 Now, we could write it like this, 
1:03 but Python has a lot of conventions around slicing, 
1:06 so if you are going to start at the beginning you can say just :5 
1:06 so if you are going to start at the beginning you can say just ":5" 
1:11 and it says go from the beginning to five. 
1:13 So we should get the same output, and we do. 
1:16 Down here, if we want to actually go from the second to the seventh item, 
1:19 we'll say just print it out like this, let's be clear what we mean here, 
1:23 we mean the thing at index 2 up to and including the thing at index 7. 
1:26 So we'll say 2 and that would give us just the item index 2 
1:27 So we'll say 2 and that would give us just the item index 2 
1:30 or the third item up to, now for slices, 
1:33 it does not include the value put here so you want to put one higher, 
1:38 let's just verify this, ok so if we count by index 0, 1, 2 so then 2 is here, 
1:44 seventh index item is 21, so we did get exactly 
1:44 3, 4, 5, 6 seventh index item is 21, so we did get exactly 
1:50 what we were looking for using 2 to 8. 
1:53 We can do more interesting stuff, we can also go and get the n 
1:55 so I could say nums and I could put something here, 
2:00 we could start by saying len of nums, 
2:02 and then this value this is not so amazing but we could say like minus 3 here, 
2:00 we could start by saying "len of nums"
2:02 and then this value, this is not so amazing but we could say like -3 here, 
2:08 and this will kind of work, so here we've got the last 3, great, 
2:12 we could use our convention that I pointed out before,
2:15 that if you are at the beginning or the end 
2:17 you can omit it so we could write it like this, 
2:15 that if you are at the beginning or the end, 
2:17 you can omit it, so we could write it like this, 
2:21 also still get the last three, but in true Pythonic fashion, 
2:24 we could do better than that. 
2:27 We can say we'd like to start 3 items back and then go to the end. 
2:26 We can say we'd like to start 3 items back and then go to the end. 
2:30 Beautiful, that way we don't even care, 
2:33 have to look or know what the length is, so that's really nice. 
2:38 So let's put this under the absolutely Pythonic last 3 version. 
2:43 All of this stuff is working within memory lists 
2:45 and we could do with strings and other types as well, 
2:48 but the title was slicing collections and more, 
2:51 so how far can we take this more- well, 
2:53 this is pretty interesting from the database, 
2:51 so how far can we take this more - well, 
2:53 this is pretty interesting - from the database, 
2:57 if we look over here in this slicing support file, 
3:01 we are not going to cover SQLAlchemy in detail in this course, 
3:04 but we've got a class that we are mapping to a SQLAlchemy database 
3:08 as an id an xy and a value, these values I believe are between 0 and 1, 
3:13 ok and it stores this into a local slicing db sql lite file and it connects to it, 
3:08 as an id an x,y and a value, these values I believe are between 0 and 1, 
3:13 OK and it stores this into a local slicing_db.sqlite file and it connects to it, 
3:20 we are going to import this session factory here 
3:22 to actually create a connection to the database. 
3:25 Now, if we look at the database, ok there is nothing over here, right now, 
3:24 Now, if we look at the database, ok there is nothing over here, right now, 
3:28 but if we go to our project and we drop this in here,
3:33 you can see we have all this data 
3:35 and the part that we are going to look at is this value, 
3:36 so we would like to use slicing to get the top 3 highest values 
3:40 out of this database, 
3:43 ok, so let's see how that works. 
3:42 OK, so let's see how that works. 
3:45 Up at the top, we are importing session factory and measurement, 
3:49 so let's create a session I'll say session factory we'll call that, 
3:54 and at the end let's remember to close the session,
3:59 we'll create a query here, we'll say session.query of measurement
4:07 and we want to say filter measurement.value is greater than point 9
4:12 we are going to order by measurement.value.descending
3:58 we'll create a query here, we'll say "session.query of Measurement"
4:07 and we want to say "filter(Measurement.value) is greater than .9"
4:12 we are going to order by Measurement.value.desc()
4:17 Ok, so this is great, maybe we could just come over here 
4:21 and do a .all to get this back as a list 
4:21 and do a ".all" to get this back as a list 
4:25 and then I could just print out the query, 
4:28 I am not sure it's really query but whatever, if we run this,
4:33 you realize what are the challenges you have to spell filter correctly, 
4:27 I am not sure it's really a query but whatever, if we run this,
4:33 you realize what are the challenges - you have to spell filter correctly, 
4:38 here we go, 
4:41 once that works, you can see we have a bunch of these back 
4:43 from the database and we could even order it. 
4:48 Let's go over here to our engine and turn on tracing, 
4:47 Let's go over here to our engine and turn on tracing, 
4:50 what this will do is this will actually show us 
4:54 the sql commands going against our database, 
4:57 so in the end down here you can see basically select star 
5:02 from measurements order by you know, 
5:04 where the value is greater than point 9 order by the value descending, ok. 
4:54 the SQL commands going against our database, 
4:57 so in the end down here you can see basically "SELECT * 
5:02 FROM measurements ORDER by", you know, 
5:04 where the value is greater than .9, order by the value descending, OK.
5:08 Oops, here is the select part, so this is pretty cool, 
5:10 how does slicing have anything to do with this, all right, 
5:15 so if let's take away this all, if I want just the top 3 measurements,
5:11 how does slicing have anything to do with this, all right, 
5:14 so if let's take away this all, if I want just the top 3 measurements,
5:21 I can actually use slicing to say give me a subset of the results
5:25 from the database,
5:27 all right, so I could say something like this, 
5:30 top 3 = and I could say query, 
5:33 go from the beginning up to 3 and that would give me the top 3 records, 
5:26 all right, so I could say something like this: 
5:30 "top 3 = " and I could say "query, 
5:33 go from the beginning up to 3" and that would give me the top 3 records, 
5:37 let's just print those out really quick, so look at this, 
5:42 select star from table where value is this, 
5:45 order by that limit in offset and the two values 
5:48 we're passing are 0.9 for the where clause 
5:52 and then 3 for the limit and 0 for the offset
5:55 so first 3 order bu descending, so those are going to be the top 3, 
5:42 SELECT * FROM table WHERE value is this, 
5:45 ORDER BY that, LIMIT and OFFSET and the two values
5:48 we're passing are .9 for the WHERE clause 
5:52 and then 3 for the LIMIT and 0 for the OFFSET
5:55 so first 3 ORDER by descending, so those are going to be the top 3, 
5:58 now we could actually go a little farther and say well, 
6:01 what we are really looking for is just the measurements, 
6:03 so I could say m.value for m in query up to 3
6:00 what we are really looking for is just the measurements, 
6:03 so I could say "m.value for m in query up to 3"
6:08 like that and then I'll actually show just the values. 
6:12 So those are the highest 3 values and you can see 
6:14 they are very high and descending, how amazing is that? 
6:17 So, the reason I took the time to do this SQLAlchemy example 
6:22 is slicing is cool and at first it feels like ok
6:22 is slicing is cool and at first it feels like OK
6:25 this is a cool way to work with like lists and strings, 
6:28 but slicing is so much more than that, 
6:30 slicing is deeply integrated into many of the APIs and packages 
Expand All @@ -116,10 +116,10 @@
6:48 So let's see this in a graphic. 
6:50 All right, so here is our numbers again that we want the values at index 2 to 7 
6:54 or we would say 2:8 remember, the last item is not included, 
6:57 we want the last 3, the best way to dot hat would be I want to go back 3 
7:01 and then the end so -3:end, that worked perfectly, 
6:57 we want the last 3, the best way to do that would be "I want to go back 3
7:01 and then the end", so -3:end, that worked perfectly, 
7:05 we could even create a database query 
7:07 and get to say give me the top 3 measurements :3 so 0 to 3 
7:12 and you can see that actual query generator is select star from measurements 
7:15 where measurement da da da da limit 3 offset 0. 
7:12 and you can see that actual query generator is SELECT * FROM Measurements 
7:15 WHERE measurement... da da da da LIMIT 3 OFFSET 0. 
7:19 Amazing. 
Loading

0 comments on commit ba3a366

Please sign in to comment.