Skip to content

Commit

Permalink
Removed redundant spaces
Browse files Browse the repository at this point in the history
  • Loading branch information
hclivess committed Apr 16, 2017
1 parent b00bf19 commit 8573829
Show file tree
Hide file tree
Showing 8 changed files with 602 additions and 602 deletions.
130 changes: 65 additions & 65 deletions transcripts/05-Generators/1.txt
Original file line number Diff line number Diff line change
@@ -1,73 +1,73 @@
0:01 Let's talk about collections, list comprehension,
0:01 Let's talk about collections, list comprehension,
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:24 you iterate over collections and so on. 
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:24 you iterate over collections and so on.
0:26 There is many built in types that work that way
0:28 such as lists and dictionaries and so on, 
0:31 but if we had our own type we defined, 
0:33 we might want to be able to iterate over it as well. 
0:35 Here is the ShoppingCart class, 
0:28 such as lists and dictionaries and so on,
0:31 but if we had our own type we defined,
0:33 we might want to be able to iterate over it as well.
0:35 Here is the ShoppingCart 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: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: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: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: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: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:35 just to see what the error is. 
1:38 Boom, ShoppingCart object is not iterable. 
1:35 just to see what the error is.
1:38 Boom, ShoppingCart 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: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: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:16 Boom, items in your cart: guitar, cd, iPhone. Beautiful. 
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: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: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: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: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: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__ 
2:55 now you can see we have out items but sorted, 
2:57 not necessarily the same way they were stored before 
3:00 and we could even go and say I'd like the negative price here, 
3:04 so now we have the most expensive ones first. 
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: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. 
3:36 Ok, we saw that in order to add iteration to our shopping cart, 
3:40 we just need to add a __iter__ method here 
3:44 rather than just exposing the underline self.items 
3:47 we are actually exposing a sorted version of it as a generator. 
3:51 So now we come over here we add some items into it and if you want, 
3:55 we can do a for in loop over our cart point out the items as you saw 
3:58 and we can grab once we have them, the name and the price and print those out. 
4:02 So it's super easy to add custom iterations to your type 
4:05 and building on this Python data model with the dunder methods 
4:08 sometimes called magic methods is a very Pythonic thing to do. 
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__
2:55 now you can see we have out items but sorted,
2:57 not necessarily the same way they were stored before
3:00 and we could even go and say I'd like the negative price here,
3:04 so now we have the most expensive ones first.
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: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.
3:36 Ok, we saw that in order to add iteration to our shopping cart,
3:40 we just need to add a __iter__ method here
3:44 rather than just exposing the underline self.items
3:47 we are actually exposing a sorted version of it as a generator.
3:51 So now we come over here we add some items into it and if you want,
3:55 we can do a for in loop over our cart point out the items as you saw
3:58 and we can grab once we have them, the name and the price and print those out.
4:02 So it's super easy to add custom iterations to your type
4:05 and building on this Python data model with the dunder methods
4:08 sometimes called magic methods is a very Pythonic thing to do.
94 changes: 47 additions & 47 deletions transcripts/05-Generators/2.txt
Original file line number Diff line number Diff line change
@@ -1,58 +1,58 @@
0:01 Next, let's talk about testing for containment and various sequences
0:01 Next, let's talk about testing for containment and various sequences
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: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: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: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: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: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: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:48 as a tertiary sort of expression, 
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: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:48 as a tertiary sort of expression,
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 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:14 so this actually goes through and it searches the elements in it 
1:16 and it does a comparison not on index but by value, 
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:14 so this actually goes through and it searches the elements in it
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: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: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, 
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: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: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: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,
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:53 Cat is not in this string. 
2:56 All right, so let's see that in a graphic, 
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 KeyError, if it's not there, 
3:11 so we might want to do this sort of check first style 
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:53 Cat is not in this string.
2:56 All right, so let's see that in a graphic,
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 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 KeyError. 
3:17 then we can safely access it because we know it's not going to KeyError.
Loading

0 comments on commit 8573829

Please sign in to comment.