[python-win32] My username generator

Brian Jarrett bjarrett@garcoschools.org
Mon, 7 Apr 2003 14:33:18 -0700


Below is my code listing for generating a list of possible usernames.  I =
think I've made this modular enough to really work in just about any =
situation that was needed.  I changed the kludgy was I was defining what =
to use in each string with a function so you can do whatever you want to =
each string, get them all concatenated and then spit out a list of =
these.

I don't know if it could be used for things other than generating =
usernames, but it should work.  (Maybe generating a directory name for =
user's web pages?)

Again, if you see any way for me to further improve this, let me know.  =
I'm happy enough with it now to start using it as is.

Brian

P.S.  Thanks for the tip of creating function lists, D.W.  that did work =
out well.


start code ######################

import string

#a sample dictionary for testing only
#the keys used in the dictionary need to be referenced in rule elements.
mydata =3D =
{'fname':'Bob','mname':'Charles','lname':'Jones','gradyear':'2003'}

#add rule elements here
#these must be functions that will work with the value
#of one key from the dictionary.

def finitial(inputdata):
    ''' Takes first name and returns lowercase first letter '''
    text =3D inputdata['fname'][0]
    text =3D string.lower(text)
    return text

def lfull(inputdata):
    ''' Takes last name and returns it in lowercase '''
    text =3D inputdata['lname']
    text =3D string.lower(text)
    return text

def minitial(inputdata):
    ''' Takes middle name and returns lowercase first letter '''
    text =3D inputdata['mname'][0]
    text =3D string.lower(text)
    return text

def abbrgradyear (inputdata):
    ''' Takes the graduation year and returns the last 2 digits '''
    text =3D inputdata['gradyear'][2:]
    return text

#now build rules from the rule elements
#a rule element can be used in more than one rule
#the rule elements will be applied in order

student1 =3D [finitial, lfull, abbrgradyear]
student2 =3D [finitial,minitial,lfull,abbrgradyear]

employee1 =3D [finitial, lfull]
employee2 =3D [finitial, minitial, lfull]

#now build a ruleset placing the rules in the order you wish them to be =
tried

student =3D [student1,student2]
employee =3D [employee1,employee2]


#functions to handle rules
def applyrule(inputdata, rule):
    ''' Returns a string made from the rule elements specified in the =
rule'''
    fulltext =3D ''
    for func in rule:
        fulltext =3D fulltext + func(inputdata)
    return fulltext

def applyruleset(inputdata,ruleset):
    ''' This function returns a list of strings created from the ruleset =
selected '''
    textlist =3D []
    for rule in ruleset:
        textlist.append(applyrule(inputdata,rule))
    return textlist