WEBVTT

0
00:00.240 --> 00:03.870
Now what may seem like a long time ago,

1
00:04.350 --> 00:07.530
we created a password generator project.

2
00:07.620 --> 00:12.620
So this was in fact on day 5 where we took three lists,

3
00:13.110 --> 00:14.640
letters, numbers, and symbols,

4
00:15.180 --> 00:20.010
and we used it to generate a random list of letters, symbols,

5
00:20.010 --> 00:20.843
and numbers

6
00:21.000 --> 00:25.860
and then we shuffled all of those together and we turned it into a string to

7
00:25.860 --> 00:28.320
create a very secure password.

8
00:29.760 --> 00:32.010
Now I've modified the code a little bit,

9
00:32.190 --> 00:34.970
just so that we don't need to use any inputs.

10
00:34.970 --> 00:38.190
So we don't have to type anything in the console. Instead,

11
00:38.220 --> 00:43.220
I've created a random number between 8 and 10 to pick 8 or 10 letters

12
00:44.580 --> 00:49.470
and then 2 to 4 numbers and 2 to 4 symbols.

13
00:50.370 --> 00:52.980
So what I want you to do is the head over to this link

14
00:53.220 --> 00:54.900
which is in the course resources,

15
00:55.560 --> 01:00.560
and simply just copy everything that's in there into your password generator

16
01:01.140 --> 01:01.973
section,

17
01:02.220 --> 01:07.220
because we're going to be using this code in our password manager so that when

18
01:08.070 --> 01:09.750
the user clicks on this button,

19
01:09.780 --> 01:14.780
we can generate them a random password and already pre-populate that field.

20
01:14.910 --> 01:16.380
So they don't have to type anything

21
01:16.530 --> 01:20.700
and they don't have to come up with a really complex password like this.

22
01:21.810 --> 01:26.810
Once we've put our password generator code into the password generator section,

23
01:27.360 --> 01:31.680
let's go ahead and reformat it. So let's put our imports at the top.

24
01:33.660 --> 01:37.770
And as a challenge, I want you to take a look at these three lines,

25
01:38.190 --> 01:39.420
look at what they're doing.

26
01:39.990 --> 01:44.990
We've created an empty list and we're adding a random letter from this list of

27
01:47.310 --> 01:50.100
letters. And then we have this for-loop

28
01:50.220 --> 01:54.420
which basically creates a range from this random number,

29
01:54.690 --> 01:56.880
so anywhere between 8 to 10,

30
01:57.420 --> 02:02.420
and then we add a random letter to that password list. So this is a really

31
02:02.790 --> 02:06.480
good opportunity for using list comprehensions instead,

32
02:06.900 --> 02:11.400
because we can get rid of a lot of these for loops and do each of these things

33
02:11.430 --> 02:15.990
in just one single line of code. It's also a good point for

34
02:15.990 --> 02:16.890
a bit of revision.

35
02:17.520 --> 02:22.520
I want you to have a think about how these things work and see if you can do the

36
02:22.530 --> 02:27.530
same thing by changing these three sections into three list comprehensions.

37
02:30.180 --> 02:32.640
If you're successful, once you run the code,

38
02:32.790 --> 02:35.280
it should still work exactly the same way

39
02:35.580 --> 02:39.780
and you should see a random password being generated and printed down here.

40
02:40.590 --> 02:42.360
Pause the video and give that a go.

41
02:42.380 --> 02:43.213
<v 1>All right.</v>

42
02:48.830 --> 02:52.490
<v 0>All right. So what I'm going to do is instead of creating an empty list,</v>

43
02:52.640 --> 02:54.830
I'm going to create three new lists.

44
02:55.340 --> 03:00.340
One is going to be the password_letters and this list is going to be created,

45
03:03.340 --> 03:03.880
of course,

46
03:03.880 --> 03:08.880
using less comprehension and replacing these two lines of code.

47
03:09.580 --> 03:11.320
Remember our keyword formats.

48
03:11.350 --> 03:16.350
We have our new item for item in list.

49
03:17.230 --> 03:20.200
Now, in this case, our list is actually not a list.

50
03:20.380 --> 03:25.330
It's actually going to be a range because we're going to create a range using

51
03:25.360 --> 03:30.360
this random number. So we generate a range between zero and this number minus

52
03:31.540 --> 03:36.010
one. So we can pass in our number letters into here.

53
03:37.210 --> 03:40.810
Each of the items is actually not important.

54
03:40.990 --> 03:44.800
We don't actually need to use it so we can just use an underscore to replace it.

55
03:45.430 --> 03:48.490
But what is important is what the new item is going to be,

56
03:48.910 --> 03:51.880
because this is going to be created from random.choice

57
03:52.210 --> 03:57.210
and we're going to be passing in our list of letters up here so that we pick a

58
03:58.180 --> 04:03.160
random letter and we put it into our new list, password_letters.

59
04:03.640 --> 04:07.240
And then we're going to check to see how many times we need to do this by

60
04:07.240 --> 04:12.240
looking at the range. That line will replace these two lines of code,

61
04:13.660 --> 04:17.590
and we can go ahead and do the same thing for the other two sections.

62
04:22.140 --> 04:22.973
<v 2>Yeah.</v>

63
04:23.610 --> 04:28.610
<v 0>These three lines of code uses list comprehension to replace these for loops. So</v>

64
04:29.490 --> 04:33.450
we can delete those for loops and now we end up with three lists.

65
04:34.050 --> 04:38.310
Now we want to combine these three lists into one list so that we can actually

66
04:38.310 --> 04:42.720
shuffle it. So I'm going to create, again, this password_list,

67
04:43.380 --> 04:48.380
and it's going to be created by adding our password_letters to our password_

68
04:49.980 --> 04:53.400
symbols and our password_numbers.

69
04:54.000 --> 04:58.170
So we're basically adding all these three lists together and putting it into one

70
04:58.170 --> 05:02.670
big list, and then we're shuffling it up in order to get the final random list.

71
05:03.180 --> 05:07.050
Now we can actually cut down on this code even further if we wanted to.

72
05:07.620 --> 05:10.680
For example, instead of creating a separate variable here,

73
05:10.710 --> 05:13.410
we could actually put it straight in like this.

74
05:13.920 --> 05:16.500
And instead of calling random.randint,

75
05:17.100 --> 05:21.330
we can actually say from random import the methods that we need,

76
05:21.660 --> 05:26.660
which is choice and randint and also down here we're using the shuffle function

77
05:28.950 --> 05:29.783
as well.

78
05:30.990 --> 05:35.990
So now we can delete all the places where we have random and we can replace

79
05:36.840 --> 05:39.810
these with just simply the randint,

80
05:42.060 --> 05:45.840
and we can delete these three lines of code. And finally,

81
05:45.840 --> 05:50.520
it's simply just shuffle and we shuffle our password_list. Now,

82
05:50.520 --> 05:54.810
one last thing that I think is a good thing to point out is here

83
05:54.810 --> 05:57.270
we've created an empty string called password

84
05:57.620 --> 05:59.870
and then for each of the characters in our list,

85
05:59.900 --> 06:04.550
we've basically added it to this password. Now that's easy enough,

86
06:04.580 --> 06:09.580
but these three lines could actually be done in a more Pythonic way. In

87
06:10.160 --> 06:12.890
Python there is a method called join

88
06:13.160 --> 06:15.680
which is available on every single string.

89
06:16.070 --> 06:20.480
So you could take a string like the pound sign and you could call the join

90
06:20.480 --> 06:21.313
method on it.

91
06:21.710 --> 06:26.710
And what it does is it will create a new string that combines all of the

92
06:27.920 --> 06:30.890
elements in this iterable, John, Peter, and Vicky,

93
06:31.220 --> 06:35.360
and it will separate them by whatever character you put here. Now,

94
06:35.390 --> 06:37.850
this doesn't actually have to be a tuple.

95
06:37.880 --> 06:41.120
It could be a dictionary and it could also be a list.

96
06:41.450 --> 06:43.760
Let me convert this into a list.

97
06:45.230 --> 06:50.230
And now if I go ahead and use the pound sign to join my list and run this code,

98
06:52.330 --> 06:54.100
ou can see, 

99
06:54.460 --> 06:57.220
<v 0>I end up with the same results. Now,</v>

100
06:57.220 --> 07:01.780
what if I actually delete this pound sign and I just have an empty string?

101
07:02.200 --> 07:05.350
Well, this will work as well. And what it does,

102
07:05.410 --> 07:10.410
it will join all of the elements in that list with no separation.

103
07:11.770 --> 07:16.270
That's basically what we're trying to do here, right? So instead of all of this,

104
07:17.080 --> 07:19.060
I can simply just write

105
07:19.540 --> 07:22.360
" ".join,

106
07:22.840 --> 07:26.410
and then pass in my list which is my password_list.

107
07:26.920 --> 07:29.800
And this would be equal to my password.

108
07:30.850 --> 07:35.560
And that replaces all of these lines of code. Right now

109
07:35.590 --> 07:38.980
if I run my code as it is, you can see

110
07:39.040 --> 07:44.040
I still generate my password and it still is super secure and super long

111
07:44.740 --> 07:49.450
and in fact, you can modify that by changing how many password letters you want,

112
07:49.450 --> 07:51.400
how many symbols, how many numbers,

113
07:51.880 --> 07:55.390
and you can change these up to make it even more secure.

114
07:55.780 --> 08:00.780
But we've greatly shortened this code from our day 5's work because we now

115
08:00.970 --> 08:03.190
know things like list comprehensions,

116
08:03.340 --> 08:05.830
and we've now learned the join method as well.

117
08:06.550 --> 08:11.050
So this entire section is our password generator mechanism.

118
08:11.530 --> 08:15.520
If we embed all of this inside a function

119
08:15.550 --> 08:19.330
so we could call it generate_password

120
08:19.390 --> 08:21.490
which is what it's pretty much going to do,

121
08:22.000 --> 08:27.000
then we can call this function when the user presses on that generate password

122
08:27.280 --> 08:28.113
button.

123
08:28.300 --> 08:33.300
So let's add another command and add in our generate_password function here.

124
08:35.800 --> 08:39.790
Now, what we don't want though is we don't want to print out the password,

125
08:39.790 --> 08:44.050
that's not very useful for the user who's using a graphical user interface.

126
08:44.500 --> 08:48.580
Instead, we want to populate this entry with the password.

127
08:49.000 --> 08:50.260
Do you remember how to do that?

128
08:50.620 --> 08:53.680
If you do, pause the video and complete this as a challenge.

129
08:54.100 --> 08:56.100
If you don't, I'm to show you how to do it.

130
08:58.020 --> 09:01.890
So it's the same way as what we did with our email.

131
09:02.280 --> 09:06.750
Remember how we wanted our email entry to have a starting value,

132
09:07.080 --> 09:10.110
the user's email. Well, we'll do the same thing here.

133
09:10.470 --> 09:15.390
So we can tap into our password entry and we call the method insert.

134
09:15.900 --> 09:19.650
Now the position that we want to insert our text is going to be at the very

135
09:19.650 --> 09:20.220
start,

136
09:20.220 --> 09:25.220
so the zeroth character, and the text that we want to insert into it is of course

137
09:25.410 --> 09:29.160
our newly created password. So now

138
09:29.160 --> 09:32.640
if I run my code, you can see as soon as I click on this button,

139
09:32.940 --> 09:37.940
a random and beautifully complex password gets generated and populated in here.

140
09:39.120 --> 09:42.180
Now, whenever I sign up for a new website,

141
09:42.960 --> 09:45.390
then I can simply just type in the name of the website,

142
09:45.660 --> 09:49.770
generate my password and hit add, and I'm already done.

143
09:50.490 --> 09:54.390
Now we've incorporated our generate password functionality,

144
09:54.660 --> 09:58.050
we're able to save all data to our data file.

145
09:58.350 --> 10:00.270
The very last thing I want to show you

146
10:00.270 --> 10:05.270
which is a really neat trick that you can do with Python is the ability to put

147
10:05.430 --> 10:10.430
strings into the clipboard so that once we click generate_password,

148
10:11.280 --> 10:12.570
this entire string,

149
10:12.600 --> 10:17.250
I don't actually have to go ahead and highlight it and then copy it and then get

150
10:17.250 --> 10:21.150
hold of it. Because in most cases, once I've generated my password,

151
10:21.210 --> 10:25.620
I want to be able to immediately paste it into somewhere where I'm signing up

152
10:25.620 --> 10:27.930
for, right? I want to be able to just simply hit

153
10:28.550 --> 10:29.383
paste.

154
10:30.410 --> 10:33.830
<v 0>So how can we do that? Well, we can use a Python project</v>

155
10:34.130 --> 10:38.840
which is called pyperclip. And this is a cross-platform Python module

156
10:39.170 --> 10:43.580
that just makes it so easy to work with copy and pasting for clipboard

157
10:43.580 --> 10:47.870
functions. All we have to do is import it

158
10:48.260 --> 10:52.490
and then we say, copy, and we pass in the text to be copied.

159
10:52.880 --> 10:56.510
Or you can hit paste and it'll paste whatever is in the clipboard.

160
10:56.990 --> 10:59.420
We're only really interested in these two parts.

161
10:59.540 --> 11:04.310
So two lines of code gets us that functionality. Going back to our code,

162
11:04.340 --> 11:05.630
scrolling to the very top,

163
11:05.690 --> 11:09.680
let's go ahead and import our pyperclip.

164
11:09.710 --> 11:13.640
Make sure that you spelled it right. You can check it against this link

165
11:13.670 --> 11:15.500
which I've got in the course resources.

166
11:16.370 --> 11:20.780
And once we type that you can see that PyCharm is already telling us you don't

167
11:20.780 --> 11:23.330
have this package. So go ahead and install it.

168
11:23.990 --> 11:27.320
Once it's been successfully installed (it's a very small module)

169
11:27.620 --> 11:28.880
then we can use it.

170
11:29.420 --> 11:33.440
And the part where we're going to use it is when we generate our password.

171
11:33.950 --> 11:38.600
So once we've generated our password, we're going to call pyperclip

172
11:41.360 --> 11:45.410
and we're going to call the copy method. Now, inside here,

173
11:45.410 --> 11:48.440
we have to put the text that we want to copy into the clipboard,

174
11:48.680 --> 11:50.660
which is going to be the password.

175
11:51.860 --> 11:56.860
So now when we run and I generate a new password, right now

176
11:57.160 --> 11:59.170
that password is already in my clipboard

177
11:59.230 --> 12:04.230
and I can show you just by pasting it into here with command + v. That makes

178
12:04.330 --> 12:07.450
it so much easier to work with our password manager

179
12:07.750 --> 12:12.750
because all we have to do when we want to sign up to a new account is to simply

180
12:12.820 --> 12:15.430
specify the website name

181
12:15.730 --> 12:20.080
and then we've already got our email saved and we've got our password generated,

182
12:20.320 --> 12:21.940
ready to paste into here.

183
12:22.870 --> 12:27.870
So that should make it a lot easier to have very secure passwords and also a

184
12:28.000 --> 12:31.930
place to check on what those passwords are when you forget.

185
12:32.740 --> 12:35.230
I hope you enjoyed building this project with me today

186
12:35.590 --> 12:39.010
and that you are going to put this into good use and make sure that all of your

187
12:39.010 --> 12:44.010
websites and all of your data and all of your accounts are secured with a good

188
12:44.650 --> 12:45.610
quality password.

189
12:45.630 --> 12:45.930
<v 1>All right.</v>