what the heck this code is doing?

John Gordon gordon at panix.com
Fri Jul 1 14:06:15 EDT 2016


In <3bbddafc-dcd6-4d5c-84f4-94077b5bccc8 at googlegroups.com> Basant Dagar <dagar.basant2 at gmail.com> writes:

> def lookup(d, keyval):
>     found = False
>     for child in d:
>         if found : return child.text
>         if child.tag == 'key' and child.text == keyval :
>             found = True
>     return None

> trackID = lookup(entry, 'Track ID')

> Below is the main part of input xml file data, it passes to lookup function:

> <key>Track ID</key><integer>369</integer>

> what I don't get is. How in the world it returns value 369 for the variable 'trackID'??

It loops through the child items in entry, looking for one with a
'key' value of 'Track ID'.  If it finds one, it sets found=True and
loops one more time, returning the text of the *next* child element.

It depends on the 'key' element being directly followed by the 'integer'
element within entry.

-- 
John Gordon                   A is for Amy, who fell down the stairs
gordon at panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"




More information about the Python-list mailing list