本文整理匯總了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)