So, recently, I was asked to changed my password for a service I am subscribed to. And obviously, it couldn’t be something I’ve used before. I tried changing one character, nope it didn’t work. Sigh…listen, I am just very lazy, okay? Changing my password annoys just as much as going grocery shopping. AIN’T NOBODY GOT TIME FOR THAT. On top of that, the password had to be exactly 10 characters long, contain a mixture of upper case, lower case, numbers and special characters. K, bye Felicia.

And here we are… I decided to look deeper into the random module of Python and create a random password generator so I never, ever, ever have to waste those precious 5 minutes thinking of a new password.

The random module is a fantastic module when you need to generate fake data. I use it all the time to test models, create dummy sets, etc. You can use to create all sorts of fake date.  The reason why this is useful is because sometimes you won’t have enough data at a given time to test something out or you need a random pick from a list, etc. Bottomline is, it’s a good library to know about.

Let’s create a simple list to look at what some of the functions in this module can do. We’ll also import the libraries we will use, but I won’t go into those until later.

import string
import random

my_list = ["nib", "pen", "brush", "sumi", "ink", "paper"]

Okay, let’s now showcase some of the functions we can use on this list. Say, we wanted to pick one random element in our list. We’d do it as so,

random.choice(my_list)
"brush"

Or, say we needed a random number, any number. We’d get it as so.

random.randint(0, 10)
5

Or… what if we needed a list of random numbers,

for i in range(3):
    print random.randrange(0, 30, 1)

The piece of code above does the following – it loops three time, so we will get a random number in every iteration. The random number will be between 0 and 30 and I am telling it to use steps of 1.

If we were to need a decimal number, we could use the function below,

random.random() #0.0 - 1.0
random.uniform(1, 10) #1.0+

 

As far as the string module goes, this one will gives the characters. Similar to the random module, the string module is a strong tool to have under your belt. If you work with text data, the string module is your home girl. Here are some of the cool things we can do with this library.

my_string = "dank meme"
string.upper(my_string)
"DANK MEME"

string.upper(my_string)
"dank meme"

string.split(my_string)
['dank', 'meme']

string.replace(my_string, "dank", "savage")
"savage meme"

The split function is GREAT for tokenizing text, btw.

We can also use the string module to get any possible ascii character which will be very useful for our password generator.

string.ascii_letters
'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ'

type(string.ascii_letters)
str

string.digits
'0123456789'

 

Now, with all these tricks, let’s try to put it together to create a password generator. The first thing I want to do is define a function. The reason why I am packaging it up is because I will likely re-use this in some other code down the road so it makes it easier to have it nice and compact.

def random_password_generator(length=10, chars=string.ascii_letters + string.digits + '!@#$%^&*()'):
    """
    Returns a string of random characters. It may contain upper
    and lowercase letters, and numbers.
    The default size is a password of size 10.
    Please note that the characters are made up of A-Z, a-z and 0-9.
    The function takes in the length and character types as arguments.
    """
    password = ''.join(random.choice(chars) for char_val in range(length))
    return password

The way this function works is that if first takes in the length of the password you want to generate. If you don’t give it a value it defaults to 10. It also requires the type of characters. Right now it defaults to string.ascii_letters + string.digits + ‘!@#$%^&*()’) – so uppercase, lowercase, special characters and numbers. If I wanted only uppercase letters, I could do this, string.ascii_uppercase + string.digits + ‘!@#$%^&*()’), so there are ways to play around the arguments. Okay, let’s test it.

#test generator
my_password = random_password_generator(length=8)
'l0CqNEU8)'

Wooooo, yaaas that is SEXY! On top of that, it saves me time? Yes, yes, yes!

Happy coding y’all!

Posted by:Aisha Pectyo

Astrophysicist turned data rockstar who speaks code and has enough yarn and mod podge to survive a zombie apocalypse.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s