from netaddr.compat import _zip


def chr_range(low, high):
    """Returns all characters between low and high chars."""
    return [chr(i) for i in range(ord(low), ord(high) + 1)]


#: Base 85 integer index to character lookup table.
BASE_85 = (chr_range('0', '9') + chr_range('A', 'Z') + chr_range('a', 'z') + [
    '!', '#', '$', '%', '&', '(', ')', '*', '+', '-', ';', '<', '=', '>', '?',
    '@', '^', '_', '`', '{', '|', '}', '~'
])

#: Base 85 digit to integer lookup table.
BASE_85_DICT = dict(_zip(BASE_85, range(0, 86)))


def ipv6_to_base85(addr):
    """Convert a regular IPv6 address to base 85."""
    ip = IPAddress(addr)
    int_val = int(ip)

    remainder = []
    while int_val > 0:
        remainder.append(int_val % 85)
        int_val //= 85

    encoded = ''.join([BASE_85[w] for w in reversed(remainder)])
    leading_zeroes = (20 - len(encoded)) * "0"
    return leading_zeroes + encoded
示例#2
0

def chr_range(low, high):
    """Returns all characters between low and high chars."""
    return [chr(i) for i in range(ord(low), ord(high) + 1)]

#: Base 85 integer index to character lookup table.
BASE_85 = (
    chr_range('0', '9') + chr_range('A', 'Z') +
    chr_range('a', 'z') +
    ['!', '#', '$', '%', '&', '(', ')', '*', '+', '-', ';', '<', '=', '>',
     '?', '@', '^', '_', '`', '{', '|', '}', '~']
)

#: Base 85 digit to integer lookup table.
BASE_85_DICT = dict(_zip(BASE_85, range(0, 86)))


def ipv6_to_base85(addr):
    """Convert a regular IPv6 address to base 85."""
    ip = IPAddress(addr)
    int_val = int(ip)

    remainder = []
    while int_val > 0:
        remainder.append(int_val % 85)
        int_val //= 85

    encoded = ''.join([BASE_85[w] for w in reversed(remainder)])
    leading_zeroes = (20 - len(encoded)) * "0"
    return leading_zeroes + encoded
示例#3
0
 def compact(self):
     """
     Compact internal list of `IPNetwork` objects using a CIDR merge.
     """
     cidrs = cidr_merge(list(self._cidrs))
     self._cidrs = dict(_zip(cidrs, [True] * len(cidrs)))
示例#4
0
def test_compat_zip():
    l2 = _zip([0], [1])
    assert hasattr(_zip(l2), 'pop')
    assert l2 == [(0, 1)]
示例#5
0
 def compact(self):
     """
     Compact internal list of L{IPNetwork} objects using a CIDR merge.
     """
     cidrs = cidr_merge(list(self._cidrs))
     self._cidrs = dict(_zip(cidrs, [True] * len(cidrs)))
示例#6
0
def test_compat_zip():
    l2 = _zip([0], [1])
    assert hasattr(_zip(l2), 'pop')
    assert l2 == [(0, 1)]