本文整理汇总了Python中ipaddress.get_mixed_type_key方法的典型用法代码示例。如果您正苦于以下问题:Python ipaddress.get_mixed_type_key方法的具体用法?Python ipaddress.get_mixed_type_key怎么用?Python ipaddress.get_mixed_type_key使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类ipaddress
的用法示例。
在下文中一共展示了ipaddress.get_mixed_type_key方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_mixed_type_key
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import get_mixed_type_key [as 别名]
def test_mixed_type_key(self):
# with get_mixed_type_key, you can sort addresses and network.
v4_ordered = [self.v4addr, self.v4net, self.v4intf]
v6_ordered = [self.v6addr, self.v6net, self.v6intf]
self.assertEqual(v4_ordered,
sorted(self.v4_objects,
key=ipaddress.get_mixed_type_key))
self.assertEqual(v6_ordered,
sorted(self.v6_objects,
key=ipaddress.get_mixed_type_key))
self.assertEqual(v4_ordered + v6_ordered,
sorted(self.objects,
key=ipaddress.get_mixed_type_key))
self.assertEqual(NotImplemented, ipaddress.get_mixed_type_key(object))
示例2: CollapseAddrList
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import get_mixed_type_key [as 别名]
def CollapseAddrList(addresses, complement_addresses=None):
"""Collapse an array of IP objects.
Example: CollapseAddrList(
[IPv4('1.1.0.0/24'), IPv4('1.1.1.0/24')]) -> [IPv4('1.1.0.0/23')]
Note: this works just as well with IPv6 addresses too.
On platforms that support exclude semantics with most specific match,
this method should _always_ be called with complement addresses supplied.
Not doing so can lead to *reversal* of intent. Consider this case:
destination-address:: 10.0.0.0/8, 10.0.0.0/10
destination-exclude:: 10.0.0.0/9
Without optimization, 10.0.0.1 will _match_. With optimization, most specific
prefix will _not_ match, reversing the intent. Supplying complement_addresses
allows this method to consider those implications.
Args:
addresses: list of ipaddress.IPNetwork objects
complement_addresses: list of ipaddress.IPNetwork objects that, if present,
will be considered to avoid harmful optimizations.
Returns:
list of ipaddress.IPNetwork objects
"""
complements_dict = collections.defaultdict(list)
address_set = set([a.network_address for a in addresses])
for ca in complement_addresses or []:
if ca.network_address in address_set:
complements_dict[ca.network_address].append(ca)
return _CollapseAddrListInternal(
sorted(addresses, key=ipaddress.get_mixed_type_key), complements_dict)
示例3: SortAddrList
# 需要导入模块: import ipaddress [as 别名]
# 或者: from ipaddress import get_mixed_type_key [as 别名]
def SortAddrList(addresses):
"""Return a sorted list of nacaddr objects."""
return sorted(addresses, key=ipaddress.get_mixed_type_key)