[Tutor] elementtree, lists, and dictionaries

Luis N tegmine at gmail.com
Fri Feb 11 22:53:40 CET 2005


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')
        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
            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.


More information about the Tutor mailing list