I have an lxml.objectify
data structure I get from a RESTful web service. I need to change a setting if it exists and create it if it doesn't. Right now I have something along the lines of the following, but I feel like it's ugly. The structure I'm looking in has a list of subelements which all have the same structure, so I can't just look for a specific tag unfortunately.
thing_structure = lxml.objectify(get_from_REST_service())
found_thing = False
if thing_structure.find('settings') is not None:
for i, foo in enumerate(thing_structure.settings):
if foo.is_what_I_want:
modify(thing_structure.settings[i])
found_thing = True
if not found_thing:
new = lxml.etree.SubElement(thing_structure, 'setting')
modify(new)
send_to_REST_service(thing_structure)