If you iterate over the author nodes you can check the user name and password of each in turn.
Not tested code!
def authenticateAuthor(author, password):
authorxml = 'author.xml'
path = os.path.join(xml, authorxml)
if not os.path.exists(path):
return False, False
else:
tree = E.ElementTree(file=path)
for authorNode in tree.getiterator('author'):
user = authorNode.find('user').text
pass = authorNode.find('password').text if author == user:
if password == pass:
return True, True
else:
return False, Truereturn False, True
Luis N wrote:
Hi,
This code works, but I don't like it much:
def authenticateAuthor(author, password): authorxml = 'author.xml' path = os.path.join(xml, authorxml) try: if not os.path.exists(path): authorfile = False else: authorfile = True tree = E.ElementTree(file=path) u = tree.getiterator('user') p = tree.getiterator('password')
Here you could say d = dict( (unode.text, pnode.text) for unode, pnode in zip(u, p))
ul = []
pl = []
for unode in u:
ul.append(unode.text)
for pnode in p:
pl.append(pnode.text)
d = {}
for i in range(len(ul)):
d[ul[i]] = pl[i]
if d.has_key(author):
if d.get(author) == password:
auth = True
else:
auth = False
or try: if d[author] == password: auth = True except KeyError: auth = False
or, if you are sure password will not be None, just skip the has_key():
if d.get(author) == password:
...(I hate using has_key() followed by a get()...
return auth, authorfile
It assumes a great deal, such as that there is no chance that there will be more users then there are passwords, etc. given an xml document format such as:
<root> <author> <user>authorname</user> <password>authorpassword</password> </author> </root>
I don't like how I'm making two lists and then turning them into a dictionary. It seems unpythonic.
Suggestions are appreciated. _______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
_______________________________________________ Tutor maillist - [email protected] http://mail.python.org/mailman/listinfo/tutor
