How can we automate a simple task to make our lives easier?

Let's create a UUID generator

Mar 23rd, 2022

python

Introduction

Let's suppose you are given the tedious task of making a unique identifier for each student in a class of 500 and for security purposes you are told these IDs have to be alphanumeric. You're thinking to yourself, how did I end up having the task assigned to me? Relentlessly shrugging your shoulders, you remember that you're a programmer and that you could just code your way out of this problem! Today we'll be walking through how to make a UUID generator whereby when you run the program you get a text file on your desktop that is populated with the number of UUIDs you asked for.

Getting Started

You might be thinking, now I have to figure out how to make a random alphanumeric character generator that I can use in the rest of my program. You could go down that route, writing a function that is responsible for generating this random alphanumeric character of n length and all that jazz. However, programmers are also lazy and there's no need to re-write this function that is publicly available for you to use which generates these IDs. We are going to be doing this using python but this can be done in other languages as well like JavaScript, PHP, Java, etc.

Create a unique ID

There's a UUID module in python that we can import and it gives us access to versions 1,3,4, and 5 of the UUID generator. We can then specify the version of the UUID generator we want to use and store it in a variable.

py

import uuid
myUUID = uuid.uuid4()

This returns an instance of a Python UUID class so we need to convert it to a string to view the generated UUID. Below is a code block that converts the class instance to a string and prints out the result.

py

import uuid
myUUID = uuid.uuid4()
print("The generated uuid is: " + str(myUUID))

Creating our program

We want a function whereby we can pass in a number and generate a list of unique IDs where the length of the list is equal to the number that was passed in.

Below is a block of code that defines the function.

py

import uuid
def UUIDgen(num):
"""
A function that takes in a number and returns a list of UUIDs
where the length of the list is equal to the function that
is passed in.
"""
pass
if __name__=="__main__":
pass

We first need to create the list that is going to hold the UUIDs and then we can set up a loop that adds the newly generated UUID to the list. This loop would continue until it gets to the number that was passed in. Below is the block of code that shows this:

py

import uuid
def UUIDgen(num):
"""
A function that takes in a number and returns a list of UUIDs
where the length of the list is equal to the function that
is passed in.
"""
lstUUID = []
count = 0
while count < num:
lstUUID.append(str(uuid.uuid4()))
count = count + 1
return lstUUID
if __name__=="__main__":
pass

Don't forget that the uuid4 returns a class instance and thus we need to convert it to a string before appending it to the list

For the program, we can ask the user how many UUIDs they want to be generated and store their response in a variable. We can do this by writing lenLst = input("How many UUIDs do you want to be generated?: ")

We can then use our function to generate that list of UUID.

Remember that input() stores responses in a string so before we use it we need to convert the lenLst variable to an int

py

import uuid
def UUIDgen(num):
"""
A function that takes in a number and returns a list of UUIDs
where the length of the list is equal to the function that
is passed in.
"""
lstUUID = []
count = 0
while count < num:
lstUUID.append(str(uuid.uuid4()))
count = count + 1
return lstUUID
if __name__=="__main__":
lenLst = input("How many UUIDs do you want generated?: ")
genUUIDLst = UUIDgen(int(lenLst))

genUUIDLst now holds a list of UUIDs where the list is of length n. We now have to write the items of this list to a text file and we're done.

We can open a new file in write mode by using the open() function and then loop throughout genUUIDLst and write each item to the file. We do this doing the following:

py

if __name__=="__main__":
lenLst = input("How many UUIDs do you want generated?: ")
genUUIDLst = UUIDgen(int(lenLst))
with open("./UUID_Lst.txt","w") as new_file:
for i in range(len(genUUIDLst)):
new_file.write("{}. {} \n \n".format(i+1,genUUIDLst[i]))

After writing all the information to the file, we need to close the file and then we can print a state to know when this has finished executing.

The final code block should look something like this:

py

import uuid
def UUIDgen(num):
"""
A function that takes in a number and returns a list of UUIDs
where the length of the list is equal to the function that
is passed in.
"""
lstUUID = []
count = 0
while count < num:
lstUUID.append(str(uuid.uuid4()))
count = count + 1
return lstUUID
if __name__=="__main__":
lenLst = input("How many UUIDs do you want generated?: ")
genUUIDLst = UUIDgen(int(lenLst))
with open("./UUID_Lst.txt","w") as new_file:
for i in range(len(genUUIDLst)):
new_file.write("{}. {} \n \n".format(i+1,genUUIDLst[i]))
print("Done!")

We are DONE!

Conclusion

Now that this is complete, we have a way to generate a list of UUIDs and save them to a file on our desktop! Question for you, What else can you do with this UUID generator?

Check out the Youtube Channel for tips/tricks and tutorials on programming!

on this page

introductiongetting startedcreate a unique idcreating our programconclusion

Last updated May 23rd, 2022