05.27.08

I *heart* python

Posted in scripting, python at 3:07 pm by karl

I recently had to produce a list of password-style codes for e-xamit. They had to be unique and they could not clash with codes that were already in use. I previously used a shell script (that called mkpasswd to generate the codes, remove duplicates and then grep though a list of in-use codes to find any that were already in use. Having recently started using python, I decided to reimplement this (mostly because the shell scripts were on my old computer and I was too lazy to boot it up and find them)…

from random import choice
existingCodes = set(['poipoi', 'blinky']) # in real life we read these from a file
# no 'l' or 'q' as they can be confusing when printed
letters = ['a','b','c','d','e','f','g','h','i','j','k',
    'm','n','o','p','r','s','t','u','v','w','x','y','z']

codes = {} # we use a dict since then we get unique keys without writing any code. Yay!
for i in range(500): # we're making 500 codes here
    tmp = ""
    for j in range(6): # each code has 6 letters
        tmp = tmp + choice(letters)
    codes[tmp] = 0 # we're only interested in the key, not the value
codeList = set(codes.keys())

duplicates = codeList & existingCodes
print codeList
print "CODES ALREADY IN USE: "
print duplicates

OK so it’s not very exciting. But there are 2 beautiful things in this little script:

  1. if we store the generated codes as keys in a dictionary, we will never have any duplicates. For free! No coding! And more importantly, no unit testing of custom duplicate removal code!
  2. the set datatype means we can find the common elements in 2 lists of codes using the & operator. That’s it - no nested for loops. Yay!

Am I glad I didn’t bother spending too much time with PowerShell!