Now what may seem like a long time ago,
we created a password generator project.
So this was in fact on day 5 where we took three lists,
letters, numbers, and symbols,
and we used it to generate a random list of letters, symbols,
and numbers
and then we shuffled all of those together and we turned it into a string to
create a very secure password.
Now I've modified the code a little bit,
just so that we don't need to use any inputs.
So we don't have to type anything in the console. Instead,
I've created a random number between 8 and 10 to pick 8 or 10 letters
and then 2 to 4 numbers and 2 to 4 symbols.
So what I want you to do is the head over to this link
which is in the course resources,
and simply just copy everything that's in there into your password generator
section,
because we're going to be using this code in our password manager so that when
the user clicks on this button,
we can generate them a random password and already pre-populate that field.
So they don't have to type anything
and they don't have to come up with a really complex password like this.
Once we've put our password generator code into the password generator section,
let's go ahead and reformat it. So let's put our imports at the top.
And as a challenge, I want you to take a look at these three lines,
look at what they're doing.
We've created an empty list and we're adding a random letter from this list of
letters. And then we have this for-loop
which basically creates a range from this random number,
so anywhere between 8 to 10,
and then we add a random letter to that password list. So this is a really
good opportunity for using list comprehensions instead,
because we can get rid of a lot of these for loops and do each of these things
in just one single line of code. It's also a good point for
a bit of revision.
I want you to have a think about how these things work and see if you can do the
same thing by changing these three sections into three list comprehensions.
If you're successful, once you run the code,
it should still work exactly the same way
and you should see a random password being generated and printed down here.
Pause the video and give that a go.
1: All right.
0: All right. So what I'm going to do is instead of creating an empty list,
I'm going to create three new lists.
One is going to be the password_letters and this list is going to be created,
of course,
using less comprehension and replacing these two lines of code.
Remember our keyword formats.
We have our new item for item in list.
Now, in this case, our list is actually not a list.
It's actually going to be a range because we're going to create a range using
this random number. So we generate a range between zero and this number minus
one. So we can pass in our number letters into here.
Each of the items is actually not important.
We don't actually need to use it so we can just use an underscore to replace it.
But what is important is what the new item is going to be,
because this is going to be created from random.choice
and we're going to be passing in our list of letters up here so that we pick a
random letter and we put it into our new list, password_letters.
And then we're going to check to see how many times we need to do this by
looking at the range. That line will replace these two lines of code,
and we can go ahead and do the same thing for the other two sections.
2: Yeah.
0: These three lines of code uses list comprehension to replace these for loops. So
we can delete those for loops and now we end up with three lists.
Now we want to combine these three lists into one list so that we can actually
shuffle it. So I'm going to create, again, this password_list,
and it's going to be created by adding our password_letters to our password_
symbols and our password_numbers.
So we're basically adding all these three lists together and putting it into one
big list, and then we're shuffling it up in order to get the final random list.
Now we can actually cut down on this code even further if we wanted to.
For example, instead of creating a separate variable here,
we could actually put it straight in like this.
And instead of calling random.randint,
we can actually say from random import the methods that we need,
which is choice and randint and also down here we're using the shuffle function
as well.
So now we can delete all the places where we have random and we can replace
these with just simply the randint,
and we can delete these three lines of code. And finally,
it's simply just shuffle and we shuffle our password_list. Now,
one last thing that I think is a good thing to point out is here
we've created an empty string called password
and then for each of the characters in our list,
we've basically added it to this password. Now that's easy enough,
but these three lines could actually be done in a more Pythonic way. In
Python there is a method called join
which is available on every single string.
So you could take a string like the pound sign and you could call the join
method on it.
And what it does is it will create a new string that combines all of the
elements in this iterable, John, Peter, and Vicky,
and it will separate them by whatever character you put here. Now,
this doesn't actually have to be a tuple.
It could be a dictionary and it could also be a list.
Let me convert this into a list.
And now if I go ahead and use the pound sign to join my list and run this code,
ou can see,
0: I end up with the same results. Now,
what if I actually delete this pound sign and I just have an empty string?
Well, this will work as well. And what it does,
it will join all of the elements in that list with no separation.
That's basically what we're trying to do here, right? So instead of all of this,
I can simply just write
" ".join,
and then pass in my list which is my password_list.
And this would be equal to my password.
And that replaces all of these lines of code. Right now
if I run my code as it is, you can see
I still generate my password and it still is super secure and super long
and in fact, you can modify that by changing how many password letters you want,
how many symbols, how many numbers,
and you can change these up to make it even more secure.
But we've greatly shortened this code from our day 5's work because we now
know things like list comprehensions,
and we've now learned the join method as well.
So this entire section is our password generator mechanism.
If we embed all of this inside a function
so we could call it generate_password
which is what it's pretty much going to do,
then we can call this function when the user presses on that generate password
button.
So let's add another command and add in our generate_password function here.
Now, what we don't want though is we don't want to print out the password,
that's not very useful for the user who's using a graphical user interface.
Instead, we want to populate this entry with the password.
Do you remember how to do that?
If you do, pause the video and complete this as a challenge.
If you don't, I'm to show you how to do it.
So it's the same way as what we did with our email.
Remember how we wanted our email entry to have a starting value,
the user's email. Well, we'll do the same thing here.
So we can tap into our password entry and we call the method insert.
Now the position that we want to insert our text is going to be at the very
start,
so the zeroth character, and the text that we want to insert into it is of course
our newly created password. So now
if I run my code, you can see as soon as I click on this button,
a random and beautifully complex password gets generated and populated in here.
Now, whenever I sign up for a new website,
then I can simply just type in the name of the website,
generate my password and hit add, and I'm already done.
Now we've incorporated our generate password functionality,
we're able to save all data to our data file.
The very last thing I want to show you
which is a really neat trick that you can do with Python is the ability to put
strings into the clipboard so that once we click generate_password,
this entire string,
I don't actually have to go ahead and highlight it and then copy it and then get
hold of it. Because in most cases, once I've generated my password,
I want to be able to immediately paste it into somewhere where I'm signing up
for, right? I want to be able to just simply hit
paste.
0: So how can we do that? Well, we can use a Python project
which is called pyperclip. And this is a cross-platform Python module
that just makes it so easy to work with copy and pasting for clipboard
functions. All we have to do is import it
and then we say, copy, and we pass in the text to be copied.
Or you can hit paste and it'll paste whatever is in the clipboard.
We're only really interested in these two parts.
So two lines of code gets us that functionality. Going back to our code,
scrolling to the very top,
let's go ahead and import our pyperclip.
Make sure that you spelled it right. You can check it against this link
which I've got in the course resources.
And once we type that you can see that PyCharm is already telling us you don't
have this package. So go ahead and install it.
Once it's been successfully installed (it's a very small module)
then we can use it.
And the part where we're going to use it is when we generate our password.
So once we've generated our password, we're going to call pyperclip
and we're going to call the copy method. Now, inside here,
we have to put the text that we want to copy into the clipboard,
which is going to be the password.
So now when we run and I generate a new password, right now
that password is already in my clipboard
and I can show you just by pasting it into here with command + v. That makes
it so much easier to work with our password manager
because all we have to do when we want to sign up to a new account is to simply
specify the website name
and then we've already got our email saved and we've got our password generated,
ready to paste into here.
So that should make it a lot easier to have very secure passwords and also a
place to check on what those passwords are when you forget.
I hope you enjoyed building this project with me today
and that you are going to put this into good use and make sure that all of your
websites and all of your data and all of your accounts are secured with a good
quality password.
1: All right.