当前位置: 首页>>代码示例>>Python>>正文


Python SubElement.attrib['distance']方法代码示例

本文整理汇总了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
开发者ID:dimagi,项目名称:commcare-hq,代码行数:38,代码来源:fixtures.py


注:本文中的xml.etree.cElementTree.SubElement.attrib['distance']方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。