def group_scopes(scopes):
"""Group scopes by version and type
:type scopes: list[Prefix]
"""
def _prefix_as_int(prefix):
return IP(prefix.net_address).int()
groups = defaultdict(list)
for scope in scopes:
prefix = IP(scope.net_address)
if prefix.iptype() == 'PRIVATE':
groups['private'].append(scope)
elif prefix.version() == 4:
groups['ipv4'].append(scope)
elif prefix.version() == 6:
groups['ipv6'].append(scope)
if any([groups['private'], groups['ipv4'], groups['ipv6']]):
return IpGroup(*[sorted(groups[x], key=_prefix_as_int)
for x in ('private', 'ipv4', 'ipv6')])
else:
return []
评论列表
文章目录