[python-win32] UPDATE: need help in creating a username generator

Brian Jarrett bjarrett@garcoschools.org
Mon, 7 Apr 2003 08:27:30 -0700


OK, here's the code I've created so far.  Keep in mind that I'm new to =
python and am using what I've learned.  If you have a more elegant (and =
functional) way to do this, please enlighten me!  *grin*  One thing I =
don't like is the limited way I can specify "slicing" operations.  I =
guess to be really fancy I'd have to define each rule element as a =
function like was originally suggested to me.  Any other ideas?

#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
#must use the key used in the dictionary passed followed by a list
#indicating the start and stop points of a slicing operation.  To
#specify the end of a string without knowing the actual length use '*'
finitial =3D ['fname',[0,1]]  # this element will take the first =
character of fname
lfull =3D ['lname',[0,'*']]   # this element will take ALL of lname
minitial =3D ['mname',[0,1]]  # this element takes first character of =
mname
abbrgradyear =3D ['gradyear',[2,'*']]  #this element takes the last 2 =
characters of gradyear,
                                     #which should be a string of 4 =
characters ALWAYS

#now build rules from the rule elements
#a rule element can be used in more than one rule
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 rule elements
=20
def renderelement(inputdata, element):
	text =3D inputdata[element[0]]
	lowerbound =3D element[1][0]
	upperbound =3D element[1][1]
	if upperbound =3D=3D '*':  upperbound =3D len(text)
	return text[lowerbound:upperbound]

def applyrule(inputdata,rule):
	fulltext =3D ''
	for ruleelement in rule:
		fulltext =3D fulltext + renderelement(inputdata,ruleelement)
	return fulltext
=09
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

-----Original Message-----
From: d.w. harks [mailto:dave@psys.org]
Sent: Friday, April 04, 2003 12:57 PM
To: python-win32@python.org
Subject: Re: [python-win32] need help in creating a username generator


I do this, but not in nearly so flexible a manner. Guess I'm lazy. :-)

I just have the function generate three different possible usernames,
based on the user's first, middle and last names, and append the results
to a list. I've never had the situation come up where I can't get a =
unique
username...but I work for a small university, and we only have about =
1500
users.

Then, it checks each one against AD, and the first one that is not =
already
taken gets assigned to the user.

I could see this working with a list of function references which could =
be
selected at runtime from a pool of available username-generation
functions...i.e.:

def stuOne(name):
  <code>

def stuTwo(name):
  <code>

def genUserName(name):
     funcList =3D [ stuOne, stuTwo, ...] # generate this with your setup =
code
     optionList =3D []
     for func in funcList:
          optionList.append(func(name))

Just a thought.

dave

Brian Jarrett said:

> Hello all.
>
> I'm new to Python and I've been working on some scripts to automate =
the
> creation of user accounts on our network.  I work in a school =
district, so
> I want the school secretaries to use a webpage to create student user
> accounts and I will have the administrations staff create employee =
user
> accounts and email boxes.
>
> What I want to do is create some sort of object or function that would
> take a variable number of "rules" and apply them to information =
specified
> (firstname, lastname, middle initial, graduation year) to come up with =
a
> username for the individual.  If the first username is already used, =
then
> it would try applying the next "rule" until it came up with a unique =
name.
>  Since I have two classes of users (Employees and Students) I will =
have
> two separate sets of rules.
>
> I already have code that checks my Active Directory to see if a =
username
> has not been used.  I just need something to generate the username to
> test.
>
> Of course I could hardcode how I'd want to slice up first and last =
names,
> etc, to get a username, but I thought I'd make it more flexible and =
then
> release it for others to use if they wanted.
>
> The rules I'd be using would be something like:
>
> Employees:
> Try first initial + last name
> Try first initial + middle initial + last name
>
> Students:
> Try first initial + last name + last 2 digits of grad year
> Try first initial + middle initial + last name + last 2 digits of grad
> year
> Try first two letters of first name + last name + last 2 digits of =
grad
> year
>
> Any ideas on how to do this would be appreciated.  Mainly I'm =
concerned
> with making the rules persistent but easily changed and flexible =
enough to
> add/remove data to use and how to specify the use of said data.
>
> Brian Jarrett
> Network Administrator
> Garfield County School District 16
>
> _______________________________________________
> Python-win32 mailing list
> Python-win32@python.org
> http://mail.python.org/mailman/listinfo/python-win32
>


--=20
d.w. harks <dave@psys.org> http://dwblog.psys.org

_______________________________________________
Python-win32 mailing list
Python-win32@python.org
http://mail.python.org/mailman/listinfo/python-win32