本文整理汇总了Python中xml.etree.cElementTree.SubElement.attrib['distance']方法的典型用法代码示例。如果您正苦于以下问题:Python SubElement.attrib['distance']方法的具体用法?Python SubElement.attrib['distance']怎么用?Python SubElement.attrib['distance']使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类xml.etree.cElementTree.SubElement
的用法示例。
在下文中一共展示了SubElement.attrib['distance']方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: _users_related_locations_for_xml
# 需要导入模块: from xml.etree.cElementTree import SubElement [as 别名]
# 或者: from xml.etree.cElementTree.SubElement import attrib['distance'] [as 别名]
def _users_related_locations_for_xml(self, restore_user, location_relations):
"""Returns a sorted list of location relations:
[
(location_a, [(location_b, distance), ...]),
(location_b, [(location_a, distance), ...])
]
Sorted by the location's name, and each associated list is sorted by its location names.
This is purely for deterministicly ordered outputs, and can be changed if needed.
"""
relations_by_location = defaultdict(list)
for relation in location_relations:
relations_by_location[relation.location_a].append((relation.location_b, relation.distance))
relations_by_location[relation.location_b].append((relation.location_a, relation.distance))
for loc in relations_by_location:
relations_by_location[loc].sort(key=lambda tup: tup[0].name)
related_locations = list(relations_by_location.items())
related_locations.sort(key=lambda tup: tup[0].name)
root_node = Element('fixture', {'id': self.id,
'user_id': restore_user.user_id,
'indexed': 'true'})
outer_node = SubElement(root_node, 'locations')
for location, relations in related_locations:
location_node = SubElement(outer_node, 'location', {'id': location.location_id})
for related_location, distance in relations:
node = SubElement(location_node, 'related_location')
node.text = related_location.location_id
if distance:
node.attrib['distance'] = str(distance)
return root_node