1
00:00:05,000 --> 00:00:05,000
All right.

2
00:00:05,000 --> 00:00:10,000
In this lecture, I'm going to show you three advanced methods of Python generators.

3
00:00:10,000 --> 00:00:12,000
Now, the first one is going to be the send method.

4
00:00:12,000 --> 00:00:15,000
Then we will look at throw method and we'll look at close method.

5
00:00:15,000 --> 00:00:18,000
Now, let's get started with the first method.

6
00:00:18,000 --> 00:00:22,000
Now, to be able to explain it, very detailed, I'm going to go ahead and create a function over here.

7
00:00:22,000 --> 00:00:24,000
In this case, it's going to be the normal function.

8
00:00:24,000 --> 00:00:31,000
So let's say we want to find the numbers that is divided by 555 so you can choose any number.

9
00:00:31,000 --> 00:00:33,000
So in this case, I'm just going to put the number like this.

10
00:00:33,000 --> 00:00:35,000
So it's going to pass the number.

11
00:00:35,000 --> 00:00:38,000
So this function will take as a parameter number.

12
00:00:38,000 --> 00:00:46,000
Then I'm going to check that if this number is divided by 555 is equal to zero.

13
00:00:46,000 --> 00:00:50,000
So in this case, if the remainder is zero, they are just going to return true.

14
00:00:50,000 --> 00:00:56,000
So this is a simple function and otherwise we're just going to put return false so we can do it like

15
00:00:56,000 --> 00:01:01,000
this, or we can just delete this statement and put return like this.

16
00:01:01,000 --> 00:01:05,000
So this is going to return true if the number is divided by 555.

17
00:01:05,000 --> 00:01:08,000
So we can just go ahead and check it like this is divided.

18
00:01:08,000 --> 00:01:11,000
So I'm just going to pass the number itself and run it.

19
00:01:11,000 --> 00:01:13,000
You will see that in this case it is returning true.

20
00:01:13,000 --> 00:01:20,000
Then I'm going to go ahead and use this function inside Python generator and check for infinite division

21
00:01:20,000 --> 00:01:21,000
of this number.

22
00:01:21,000 --> 00:01:27,000
So what I'm going to do, I'll just go ahead and put over here, define so it's going to be infinite

23
00:01:27,000 --> 00:01:30,000
division, then it's not going to take any parameter.

24
00:01:30,000 --> 00:01:33,000
Then I'm going to create a variable over here which should start from zero.

25
00:01:33,000 --> 00:01:35,000
So it's going to the number starts from zero.

26
00:01:35,000 --> 00:01:38,000
Then we are going to write Infinite Loop over here.

27
00:01:38,000 --> 00:01:46,000
So while true, I'm going to check that if this number is divided by 555, so I'm going to pass this

28
00:01:46,000 --> 00:01:48,000
over here, we are going to return the yield.

29
00:01:48,000 --> 00:01:54,000
So as you remember, instead of return statement, we are using yield in case of the python generator.

30
00:01:54,000 --> 00:01:56,000
So that's why I'm going to put yield over here.

31
00:01:56,000 --> 00:01:59,000
So this is going to be the python generator function.

32
00:01:59,000 --> 00:02:04,000
But as I mentioned before, we are going to use the send method of the python generator.

33
00:02:04,000 --> 00:02:10,000
So to be able to use the same method, we have to create a variable over here which will be assigned

34
00:02:10,000 --> 00:02:11,000
with the yield number.

35
00:02:11,000 --> 00:02:17,000
So the reason that we are doing this, when you are sending the values with the send method, the variables

36
00:02:17,000 --> 00:02:19,000
will be received in the yield expression.

37
00:02:19,000 --> 00:02:24,000
Then we need to assign them to a variable to be able to use them inside generator function.

38
00:02:24,000 --> 00:02:28,000
Otherwise the values that is sent by the send method will be lost.

39
00:02:28,000 --> 00:02:33,000
So that's why to be able to do that, I'm going to go ahead and create over here a variable which is

40
00:02:33,000 --> 00:02:36,000
going to be the I that I'm going to put it like this.

41
00:02:36,000 --> 00:02:40,000
So then after getting this, we can go ahead and check that.

42
00:02:40,000 --> 00:02:46,000
If I is not none in this case, we are going to assign this I to the number and we are going to increase

43
00:02:46,000 --> 00:02:47,000
that number.

44
00:02:47,000 --> 00:02:51,000
And at the end of this loop, we are going to increase that number here.

45
00:02:51,000 --> 00:02:52,000
I'm going to put if condition like this.

46
00:02:52,000 --> 00:02:56,000
If I is not none, you will understand why I'm putting this.

47
00:02:56,000 --> 00:02:58,000
Then we're going to assign the I to the number.

48
00:02:58,000 --> 00:03:02,000
Then here I'm going to increase the number by one.

49
00:03:02,000 --> 00:03:03,000
So how this is going to work?

50
00:03:03,000 --> 00:03:10,000
So if we are sending value with the send method over here, in this case, this I will not be none.

51
00:03:10,000 --> 00:03:15,000
So in this case, we are going to assign that to the number, the value that we are getting from the

52
00:03:15,000 --> 00:03:16,000
centimeter method.

53
00:03:16,000 --> 00:03:21,000
So that's why when we are calling this function, we are going to send it to the value.

54
00:03:21,000 --> 00:03:27,000
So when you are writing the yield keyword over here like this, it is not just returning the value from

55
00:03:27,000 --> 00:03:30,000
the function, it is also receiving the value.

56
00:03:30,000 --> 00:03:37,000
So this means that by using send method, we can come here and send any given value to our generator.

57
00:03:37,000 --> 00:03:45,000
So for example, let's say our generator stopped at a value of 1000 so we can interrupt it and start

58
00:03:45,000 --> 00:03:46,000
our generator from 2000.

59
00:03:46,000 --> 00:03:52,000
But if we don't use the send method, it will continue with this number, which is going to be 1001.

60
00:03:52,000 --> 00:03:58,000
But if you want to skip some numbers and continue from the any given number in this case, you can use

61
00:03:58,000 --> 00:04:01,000
the same method to be able to send the number over here.

62
00:04:01,000 --> 00:04:06,000
To demonstrate this, I'm going to interact with this generator by using loop.

63
00:04:06,000 --> 00:04:09,000
So we have learned that there are various ways of interact with the generator.

64
00:04:09,000 --> 00:04:11,000
So in this case I'm going to use loop.

65
00:04:11,000 --> 00:04:13,000
So first we need to create the generate object.

66
00:04:13,000 --> 00:04:18,000
So it's going to be an object is equal to I'm going to call infinite division function.

67
00:04:18,000 --> 00:04:26,000
Then here I'm going to write for I in generator object, then we can go ahead and print out the I.

68
00:04:26,000 --> 00:04:29,000
So let's say, what will be the result over here.

69
00:04:29,000 --> 00:04:33,000
Then I'm going to go ahead and pass some numbers to be able to start over here.

70
00:04:33,000 --> 00:04:36,000
Now we can increase the digits every time.

71
00:04:36,000 --> 00:04:39,000
So that's why what I'm going to do, we can take the length of this.

72
00:04:39,000 --> 00:04:42,000
I so I will just put over here.

73
00:04:42,000 --> 00:04:46,000
Digit is equal to length of SDR.

74
00:04:46,000 --> 00:04:50,000
I then we are going to pass number two inside this yield statement.

75
00:04:50,000 --> 00:04:56,000
So to be able to pass a number, we are going to use generator object to send, then I'm going to put

76
00:04:56,000 --> 00:04:57,000
over here to increase the digits.

77
00:04:57,000 --> 00:05:00,000
So it's going to be ten squared the number of the digits.

78
00:05:00,000 --> 00:05:03,000
So I'm just going to put over here digits.

79
00:05:03,000 --> 00:05:04,000
So every time it's going to start.

80
00:05:05,000 --> 00:05:08,000
Our generator from this number over here.

81
00:05:08,000 --> 00:05:12,000
Now, if I just go ahead and run our code, you will see that it is continuing like this.

82
00:05:12,000 --> 00:05:14,000
Lots of numbers are coming to the console.

83
00:05:14,000 --> 00:05:16,000
So I'm just going to stop this.

84
00:05:16,000 --> 00:05:20,000
So here what we can do to be able to see the numbers in the console, how this is working.

85
00:05:20,000 --> 00:05:25,000
We can put the pulse, so I'm going to import the import time.

86
00:05:25,000 --> 00:05:30,000
Then we can put the 3 seconds slip over here to be able to see the results very well.

87
00:05:30,000 --> 00:05:33,000
So I'm just going to put over here time dot sleep.

88
00:05:33,000 --> 00:05:38,000
So I'm going to put 3 seconds then let's just clear everything from here and I'm just going to run this.

89
00:05:38,000 --> 00:05:40,000
You will see that first number is zero.

90
00:05:40,000 --> 00:05:42,000
Then we are going to wait for 3 seconds.

91
00:05:42,000 --> 00:05:44,000
Then the second number is this one.

92
00:05:44,000 --> 00:05:47,000
Then, as you can see, the number of digits is increased.

93
00:05:47,000 --> 00:05:48,000
Then the third number is this one.

94
00:05:48,000 --> 00:05:52,000
And it continues like so as you can see, every time the number of digits is increased.

95
00:05:52,000 --> 00:05:58,000
But if you do it without send method, it's going to be something 555 is multiplied with this three

96
00:05:58,000 --> 00:06:00,000
so we can check the results like this.

97
00:06:00,000 --> 00:06:05,000
So here we have put the yield statement to be able to get the value with the send method.

98
00:06:05,000 --> 00:06:09,000
Now if I just change it to like this, you just return the number.

99
00:06:09,000 --> 00:06:12,000
And for now I'm just going to comment out this part.

100
00:06:12,000 --> 00:06:14,000
Then let's just comment out this part.

101
00:06:14,000 --> 00:06:16,000
Also, we are not sending anything.

102
00:06:16,000 --> 00:06:20,000
Now, if I run this one, you will see that in this case it's going to be increased one by one.

103
00:06:20,000 --> 00:06:22,000
So as you can see, first is zero.

104
00:06:22,000 --> 00:06:27,000
Then the first number that is divided by 555 is 555.

105
00:06:27,000 --> 00:06:29,000
Then if you multiply it with two, it's going to be this number.

106
00:06:29,000 --> 00:06:33,000
If you multiply it with three, it's going to be this number and it continues like this.

107
00:06:33,000 --> 00:06:36,000
But with the send method, we are just changing these orders.

108
00:06:36,000 --> 00:06:42,000
We are skipping some numbers and we are giving the numbers to be able to start the next iteration from

109
00:06:42,000 --> 00:06:44,000
the number that we want to start.

110
00:06:44,000 --> 00:06:50,000
So by putting the yield statement like this, you can send any given number to be able to start the

111
00:06:50,000 --> 00:06:54,000
next value of the python generator from any given number over here.

112
00:06:54,000 --> 00:06:58,000
Now, the next method that I'm going to show you over here is going to be the throw method.

113
00:06:58,000 --> 00:07:03,000
Now, throw method is very useful when you are using the python generator like this, because over here,

114
00:07:03,000 --> 00:07:05,000
if I run this, it's going to run the infinitely.

115
00:07:05,000 --> 00:07:12,000
Now, if I want to stop somehow this iteration we can put over here the if condition like this.

116
00:07:12,000 --> 00:07:18,000
So I'm going to put if digits then is greater than for example let's put eight then in this case we

117
00:07:18,000 --> 00:07:24,000
can just put generator object dot row, then we can call for example value error.

118
00:07:24,000 --> 00:07:26,000
So we can put any message over here.

119
00:07:26,000 --> 00:07:28,000
The number is long.

120
00:07:28,000 --> 00:07:33,000
Now, if I go ahead and run this code, you will see that it continues to do a certain level and it

121
00:07:33,000 --> 00:07:34,000
will stop there.

122
00:07:34,000 --> 00:07:38,000
So as you can see, when you reach certain level, it says that the number is two.

123
00:07:38,000 --> 00:07:40,000
So that's how this method works.

124
00:07:40,000 --> 00:07:44,000
Now we can use the similar operation by using the close method.

125
00:07:44,000 --> 00:07:49,000
Now in this case it is raising error, but the close method will exhaust our generator.

126
00:07:49,000 --> 00:07:54,000
So in this case, instead of zero, we can go ahead and put over here close.

127
00:07:54,000 --> 00:07:58,000
Then I'm just going to change the second to one, then run our code.

128
00:07:58,000 --> 00:07:59,000
It's going to be much more faster.

129
00:07:59,000 --> 00:08:01,000
Then let's see the output.

130
00:08:01,000 --> 00:08:04,000
You'll see that in this case it's going to raise a different error.

131
00:08:04,000 --> 00:08:05,000
So as you can see, this is the stop iteration.

132
00:08:05,000 --> 00:08:11,000
So we have learned that stop iteration error raises when the old values from the python generator function

133
00:08:11,000 --> 00:08:12,000
exist.

134
00:08:12,000 --> 00:08:15,000
So that's how advanced methods of python generator works.

135
00:08:15,000 --> 00:08:16,000
So hopefully everything is clear.


