lxml.objectify
RESTful Web サービスから取得したデータ構造があります。存在する場合は設定を変更し、存在しない場合は作成する必要があります。現在、次のようなものがありますが、見苦しいと感じています。私が探している構造には、すべて同じ構造を持つサブ要素のリストがあるため、残念ながら特定のタグを探すことはできません。
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)