1
00:00:00,240 --> 00:00:03,870
Now what may seem like a long time ago,

2
00:00:04,350 --> 00:00:07,530
we created a password generator project.

3
00:00:07,620 --> 00:00:12,620
So this was in fact on day 5 where we took three lists,

4
00:00:13,110 --> 00:00:14,640
letters, numbers, and symbols,

5
00:00:15,180 --> 00:00:20,010
and we used it to generate a random list of letters, symbols,

6
00:00:20,010 --> 00:00:20,843
and numbers

7
00:00:21,000 --> 00:00:25,860
and then we shuffled all of those together and we turned it into a string to

8
00:00:25,860 --> 00:00:28,320
create a very secure password.

9
00:00:29,760 --> 00:00:32,009
Now I've modified the code a little bit,

10
00:00:32,189 --> 00:00:34,970
just so that we don't need to use any inputs.

11
00:00:34,970 --> 00:00:38,190
So we don't have to type anything in the console. Instead,

12
00:00:38,220 --> 00:00:43,220
I've created a random number between 8 and 10 to pick 8 or 10 letters

13
00:00:44,580 --> 00:00:49,470
and then 2 to 4 numbers and 2 to 4 symbols.

14
00:00:50,370 --> 00:00:52,980
So what I want you to do is the head over to this link

15
00:00:53,220 --> 00:00:54,900
which is in the course resources,

16
00:00:55,560 --> 00:01:00,560
and simply just copy everything that's in there into your password generator

17
00:01:01,140 --> 00:01:01,973
section,

18
00:01:02,220 --> 00:01:07,220
because we're going to be using this code in our password manager so that when

19
00:01:08,070 --> 00:01:09,750
the user clicks on this button,

20
00:01:09,780 --> 00:01:14,780
we can generate them a random password and already pre-populate that field.

21
00:01:14,910 --> 00:01:16,380
So they don't have to type anything

22
00:01:16,530 --> 00:01:20,700
and they don't have to come up with a really complex password like this.

23
00:01:21,810 --> 00:01:26,810
Once we've put our password generator code into the password generator section,

24
00:01:27,360 --> 00:01:31,680
let's go ahead and reformat it. So let's put our imports at the top.

25
00:01:33,660 --> 00:01:37,770
And as a challenge, I want you to take a look at these three lines,

26
00:01:38,190 --> 00:01:39,420
look at what they're doing.

27
00:01:39,990 --> 00:01:44,990
We've created an empty list and we're adding a random letter from this list of

28
00:01:47,310 --> 00:01:50,100
letters. And then we have this for-loop

29
00:01:50,220 --> 00:01:54,420
which basically creates a range from this random number,

30
00:01:54,690 --> 00:01:56,880
so anywhere between 8 to 10,

31
00:01:57,420 --> 00:02:02,420
and then we add a random letter to that password list. So this is a really

32
00:02:02,790 --> 00:02:06,480
good opportunity for using list comprehensions instead,

33
00:02:06,900 --> 00:02:11,400
because we can get rid of a lot of these for loops and do each of these things

34
00:02:11,430 --> 00:02:15,990
in just one single line of code. It's also a good point for

35
00:02:15,990 --> 00:02:16,890
a bit of revision.

36
00:02:17,520 --> 00:02:22,520
I want you to have a think about how these things work and see if you can do the

37
00:02:22,530 --> 00:02:27,530
same thing by changing these three sections into three list comprehensions.

38
00:02:30,180 --> 00:02:32,640
If you're successful, once you run the code,

39
00:02:32,790 --> 00:02:35,280
it should still work exactly the same way

40
00:02:35,580 --> 00:02:39,780
and you should see a random password being generated and printed down here.

41
00:02:40,590 --> 00:02:42,360
Pause the video and give that a go.

42
00:02:42,380 --> 00:02:43,213
All right.

43
00:02:48,830 --> 00:02:52,490
All right. So what I'm going to do is instead of creating an empty list,

44
00:02:52,640 --> 00:02:54,830
I'm going to create three new lists.

45
00:02:55,340 --> 00:03:00,340
One is going to be the password_letters and this list is going to be created,

46
00:03:03,340 --> 00:03:03,880
of course,

47
00:03:03,880 --> 00:03:08,880
using less comprehension and replacing these two lines of code.

48
00:03:09,580 --> 00:03:11,320
Remember our keyword formats.

49
00:03:11,350 --> 00:03:16,350
We have our new item for item in list.

50
00:03:17,230 --> 00:03:20,200
Now, in this case, our list is actually not a list.

51
00:03:20,380 --> 00:03:25,330
It's actually going to be a range because we're going to create a range using

52
00:03:25,360 --> 00:03:30,360
this random number. So we generate a range between zero and this number minus

53
00:03:31,540 --> 00:03:36,010
one. So we can pass in our number letters into here.

54
00:03:37,210 --> 00:03:40,810
Each of the items is actually not important.

55
00:03:40,990 --> 00:03:44,800
We don't actually need to use it so we can just use an underscore to replace it.

56
00:03:45,430 --> 00:03:48,490
But what is important is what the new item is going to be,

57
00:03:48,910 --> 00:03:51,880
because this is going to be created from random.choice

58
00:03:52,210 --> 00:03:57,210
and we're going to be passing in our list of letters up here so that we pick a

59
00:03:58,180 --> 00:04:03,160
random letter and we put it into our new list, password_letters.

60
00:04:03,640 --> 00:04:07,240
And then we're going to check to see how many times we need to do this by

61
00:04:07,240 --> 00:04:12,240
looking at the range. That line will replace these two lines of code,

62
00:04:13,660 --> 00:04:17,589
and we can go ahead and do the same thing for the other two sections.

63
00:04:22,140 --> 00:04:22,973
Yeah.

64
00:04:23,610 --> 00:04:28,610
These three lines of code uses list comprehension to replace these for loops. So

65
00:04:29,490 --> 00:04:33,450
we can delete those for loops and now we end up with three lists.

66
00:04:34,050 --> 00:04:38,310
Now we want to combine these three lists into one list so that we can actually

67
00:04:38,310 --> 00:04:42,720
shuffle it. So I'm going to create, again, this password_list,

68
00:04:43,380 --> 00:04:48,380
and it's going to be created by adding our password_letters to our password_

69
00:04:49,980 --> 00:04:53,400
symbols and our password_numbers.

70
00:04:54,000 --> 00:04:58,170
So we're basically adding all these three lists together and putting it into one

71
00:04:58,170 --> 00:05:02,670
big list, and then we're shuffling it up in order to get the final random list.

72
00:05:03,180 --> 00:05:07,050
Now we can actually cut down on this code even further if we wanted to.

73
00:05:07,620 --> 00:05:10,680
For example, instead of creating a separate variable here,

74
00:05:10,710 --> 00:05:13,410
we could actually put it straight in like this.

75
00:05:13,920 --> 00:05:16,500
And instead of calling random.randint,

76
00:05:17,100 --> 00:05:21,330
we can actually say from random import the methods that we need,

77
00:05:21,660 --> 00:05:26,660
which is choice and randint and also down here we're using the shuffle function

78
00:05:28,950 --> 00:05:29,783
as well.

79
00:05:30,990 --> 00:05:35,990
So now we can delete all the places where we have random and we can replace

80
00:05:36,840 --> 00:05:39,810
these with just simply the randint,

81
00:05:42,060 --> 00:05:45,840
and we can delete these three lines of code. And finally,

82
00:05:45,840 --> 00:05:50,520
it's simply just shuffle and we shuffle our password_list. Now,

83
00:05:50,520 --> 00:05:54,810
one last thing that I think is a good thing to point out is here

84
00:05:54,810 --> 00:05:57,270
we've created an empty string called password

85
00:05:57,620 --> 00:05:59,870
and then for each of the characters in our list,

86
00:05:59,900 --> 00:06:04,550
we've basically added it to this password. Now that's easy enough,

87
00:06:04,580 --> 00:06:09,580
but these three lines could actually be done in a more Pythonic way. In

88
00:06:10,160 --> 00:06:12,890
Python there is a method called join

89
00:06:13,160 --> 00:06:15,680
which is available on every single string.

90
00:06:16,070 --> 00:06:20,480
So you could take a string like the pound sign and you could call the join

91
00:06:20,480 --> 00:06:21,313
method on it.

92
00:06:21,710 --> 00:06:26,710
And what it does is it will create a new string that combines all of the

93
00:06:27,920 --> 00:06:30,890
elements in this iterable, John, Peter, and Vicky,

94
00:06:31,220 --> 00:06:35,360
and it will separate them by whatever character you put here. Now,

95
00:06:35,390 --> 00:06:37,850
this doesn't actually have to be a tuple.

96
00:06:37,880 --> 00:06:41,120
It could be a dictionary and it could also be a list.

97
00:06:41,450 --> 00:06:43,760
Let me convert this into a list.

98
00:06:45,230 --> 00:06:50,230
And now if I go ahead and use the pound sign to join my list and run this code,

99
00:06:52,330 --> 00:06:54,100
ou can see, 

100
00:06:54,460 --> 00:06:57,220
I end up with the same results. Now,

101
00:06:57,220 --> 00:07:01,780
what if I actually delete this pound sign and I just have an empty string?

102
00:07:02,200 --> 00:07:05,350
Well, this will work as well. And what it does,

103
00:07:05,410 --> 00:07:10,410
it will join all of the elements in that list with no separation.

104
00:07:11,770 --> 00:07:16,270
That's basically what we're trying to do here, right? So instead of all of this,

105
00:07:17,080 --> 00:07:19,060
I can simply just write

106
00:07:19,540 --> 00:07:22,360
" ".join,

107
00:07:22,840 --> 00:07:26,410
and then pass in my list which is my password_list.

108
00:07:26,920 --> 00:07:29,800
And this would be equal to my password.

109
00:07:30,850 --> 00:07:35,560
And that replaces all of these lines of code. Right now

110
00:07:35,590 --> 00:07:38,980
if I run my code as it is, you can see

111
00:07:39,040 --> 00:07:44,040
I still generate my password and it still is super secure and super long

112
00:07:44,740 --> 00:07:49,450
and in fact, you can modify that by changing how many password letters you want,

113
00:07:49,450 --> 00:07:51,400
how many symbols, how many numbers,

114
00:07:51,880 --> 00:07:55,390
and you can change these up to make it even more secure.

115
00:07:55,780 --> 00:08:00,780
But we've greatly shortened this code from our day 5's work because we now

116
00:08:00,970 --> 00:08:03,190
know things like list comprehensions,

117
00:08:03,340 --> 00:08:05,830
and we've now learned the join method as well.

118
00:08:06,550 --> 00:08:11,050
So this entire section is our password generator mechanism.

119
00:08:11,530 --> 00:08:15,520
If we embed all of this inside a function

120
00:08:15,550 --> 00:08:19,330
so we could call it generate_password

121
00:08:19,390 --> 00:08:21,490
which is what it's pretty much going to do,

122
00:08:22,000 --> 00:08:27,000
then we can call this function when the user presses on that generate password

123
00:08:27,280 --> 00:08:28,113
button.

124
00:08:28,300 --> 00:08:33,299
So let's add another command and add in our generate_password function here.

125
00:08:35,799 --> 00:08:39,789
Now, what we don't want though is we don't want to print out the password,

126
00:08:39,789 --> 00:08:44,049
that's not very useful for the user who's using a graphical user interface.

127
00:08:44,500 --> 00:08:48,580
Instead, we want to populate this entry with the password.

128
00:08:49,000 --> 00:08:50,260
Do you remember how to do that?

129
00:08:50,620 --> 00:08:53,680
If you do, pause the video and complete this as a challenge.

130
00:08:54,100 --> 00:08:56,100
If you don't, I'm to show you how to do it.

131
00:08:58,020 --> 00:09:01,890
So it's the same way as what we did with our email.

132
00:09:02,280 --> 00:09:06,750
Remember how we wanted our email entry to have a starting value,

133
00:09:07,080 --> 00:09:10,110
the user's email. Well, we'll do the same thing here.

134
00:09:10,470 --> 00:09:15,390
So we can tap into our password entry and we call the method insert.

135
00:09:15,900 --> 00:09:19,650
Now the position that we want to insert our text is going to be at the very

136
00:09:19,650 --> 00:09:20,220
start,

137
00:09:20,220 --> 00:09:25,220
so the zeroth character, and the text that we want to insert into it is of course

138
00:09:25,410 --> 00:09:29,160
our newly created password. So now

139
00:09:29,160 --> 00:09:32,640
if I run my code, you can see as soon as I click on this button,

140
00:09:32,940 --> 00:09:37,940
a random and beautifully complex password gets generated and populated in here.

141
00:09:39,120 --> 00:09:42,180
Now, whenever I sign up for a new website,

142
00:09:42,960 --> 00:09:45,390
then I can simply just type in the name of the website,

143
00:09:45,660 --> 00:09:49,770
generate my password and hit add, and I'm already done.

144
00:09:50,490 --> 00:09:54,390
Now we've incorporated our generate password functionality,

145
00:09:54,660 --> 00:09:58,050
we're able to save all data to our data file.

146
00:09:58,350 --> 00:10:00,270
The very last thing I want to show you

147
00:10:00,270 --> 00:10:05,270
which is a really neat trick that you can do with Python is the ability to put

148
00:10:05,430 --> 00:10:10,430
strings into the clipboard so that once we click generate_password,

149
00:10:11,280 --> 00:10:12,570
this entire string,

150
00:10:12,600 --> 00:10:17,250
I don't actually have to go ahead and highlight it and then copy it and then get

151
00:10:17,250 --> 00:10:21,150
hold of it. Because in most cases, once I've generated my password,

152
00:10:21,210 --> 00:10:25,620
I want to be able to immediately paste it into somewhere where I'm signing up

153
00:10:25,620 --> 00:10:27,930
for, right? I want to be able to just simply hit

154
00:10:28,550 --> 00:10:29,383
paste.

155
00:10:30,410 --> 00:10:33,830
So how can we do that? Well, we can use a Python project

156
00:10:34,130 --> 00:10:38,840
which is called pyperclip. And this is a cross-platform Python module

157
00:10:39,170 --> 00:10:43,580
that just makes it so easy to work with copy and pasting for clipboard

158
00:10:43,580 --> 00:10:47,870
functions. All we have to do is import it

159
00:10:48,260 --> 00:10:52,490
and then we say, copy, and we pass in the text to be copied.

160
00:10:52,880 --> 00:10:56,510
Or you can hit paste and it'll paste whatever is in the clipboard.

161
00:10:56,990 --> 00:10:59,420
We're only really interested in these two parts.

162
00:10:59,540 --> 00:11:04,310
So two lines of code gets us that functionality. Going back to our code,

163
00:11:04,340 --> 00:11:05,630
scrolling to the very top,

164
00:11:05,690 --> 00:11:09,680
let's go ahead and import our pyperclip.

165
00:11:09,710 --> 00:11:13,640
Make sure that you spelled it right. You can check it against this link

166
00:11:13,670 --> 00:11:15,500
which I've got in the course resources.

167
00:11:16,370 --> 00:11:20,780
And once we type that you can see that PyCharm is already telling us you don't

168
00:11:20,780 --> 00:11:23,330
have this package. So go ahead and install it.

169
00:11:23,990 --> 00:11:27,320
Once it's been successfully installed (it's a very small module)

170
00:11:27,620 --> 00:11:28,880
then we can use it.

171
00:11:29,420 --> 00:11:33,440
And the part where we're going to use it is when we generate our password.

172
00:11:33,950 --> 00:11:38,600
So once we've generated our password, we're going to call pyperclip

173
00:11:41,360 --> 00:11:45,410
and we're going to call the copy method. Now, inside here,

174
00:11:45,410 --> 00:11:48,440
we have to put the text that we want to copy into the clipboard,

175
00:11:48,680 --> 00:11:50,660
which is going to be the password.

176
00:11:51,860 --> 00:11:56,860
So now when we run and I generate a new password, right now

177
00:11:57,160 --> 00:11:59,170
that password is already in my clipboard

178
00:11:59,230 --> 00:12:04,230
and I can show you just by pasting it into here with command + v. That makes

179
00:12:04,330 --> 00:12:07,450
it so much easier to work with our password manager

180
00:12:07,750 --> 00:12:12,750
because all we have to do when we want to sign up to a new account is to simply

181
00:12:12,820 --> 00:12:15,430
specify the website name

182
00:12:15,730 --> 00:12:20,080
and then we've already got our email saved and we've got our password generated,

183
00:12:20,320 --> 00:12:21,940
ready to paste into here.

184
00:12:22,870 --> 00:12:27,870
So that should make it a lot easier to have very secure passwords and also a

185
00:12:28,000 --> 00:12:31,930
place to check on what those passwords are when you forget.

186
00:12:32,740 --> 00:12:35,230
I hope you enjoyed building this project with me today

187
00:12:35,590 --> 00:12:39,010
and that you are going to put this into good use and make sure that all of your

188
00:12:39,010 --> 00:12:44,010
websites and all of your data and all of your accounts are secured with a good

189
00:12:44,650 --> 00:12:45,610
quality password.

190
00:12:45,630 --> 00:12:45,930
All right.

