def test_encoding(uint_val, separator): quint = uint2quint(uint_val, separator) assert_not_equals(quint, uint_val) assert_equals(11, len(quint)) uint_val1 = quint2uint(quint) assert_equals(uint_val, uint_val1)
def test_presets(): presets = [ ('127.0.0.1', 'lusab-babad'), ('63.84.220.193', 'gutih-tugad'), ('63.118.7.35', 'gutuk-bisog'), ('140.98.193.141', 'mudof-sakat'), ('64.255.6.200', 'haguz-biram'), ('128.30.52.45', 'mabiv-gibot'), ('147.67.119.2', 'natag-lisaf'), ('212.58.253.68', 'tibup-zujah'), ('216.35.68.215', 'tobog-higil'), ('216.68.232.21', 'todah-vobij'), ('198.81.129.136', 'sinid-makam'), ('12.110.110.204', 'budov-kuras') ] quints = [uint2quint(ip2uint_str(t[0])) for t in presets] assert_list_equal(quints, [t[1] for t in presets]) uints = [quint2uint(t[1]) for t in presets] assert_list_equal(uints, [ip2uint_str(t[0]) for t in presets])
def test_presets(): presets = [ ('127.0.0.1', 'lusab-babad'), ('63.84.220.193', 'gutih-tugad'), ('63.118.7.35', 'gutuk-bisog'), ('140.98.193.141', 'mudof-sakat'), ('64.255.6.200', 'haguz-biram'), ('128.30.52.45', 'mabiv-gibot'), ('147.67.119.2', 'natag-lisaf'), ('212.58.253.68', 'tibup-zujah'), ('216.35.68.215', 'tobog-higil'), ('216.68.232.21', 'todah-vobij'), ('198.81.129.136', 'sinid-makam'), ('12.110.110.204', 'budov-kuras')] quints = [uint2quint(ip2uint_str(t[0])) for t in presets] assert_list_equal(quints, [t[1] for t in presets]) uints = [quint2uint(t[1]) for t in presets] assert_list_equal(uints, [ip2uint_str(t[0]) for t in presets])
def convert(str_val, target=None): """Convert between proquint, integer, hex or IPv4 string representations. Tries to guess the representation from input. :param str_val: input representation (string) :return: output representation (string) """ if target is not None and target not in {'uint', 'hex', 'ip'}: raise ValueError('Convert target should be one of: uint, hex, ip') if target == 'uint': return quint2uint_str(str_val) if target == 'hex': return quint2hex_str(str_val) if target == 'ip': return uint2ip_str(quint2uint(str_val)) # try to guess the representation try: return quint2uint_str(str_val) except ValueError: pass try: return uint2quint_str(str_val) except ValueError: pass try: return hex2quint_str(str_val) except ValueError: pass try: return uint2quint_str(ip2uint_str(str_val)) except ValueError: pass raise ValueError('Unrecognized input format: {}'.format(str_val))
def quint2hex_str(quint): return hex(quint2uint(quint))
def quint2uint_str(quint): return str(quint2uint(quint))
def test_incorrect_input(): with assert_raises(ValueError): quint2uint('foo-barr') with assert_raises(ValueError): quint2uint('') with assert_raises(ValueError): quint2uint('1234') with assert_raises(ValueError): quint2uint('foo') with assert_raises(ValueError): quint2uint('xxxx-yyyy') with assert_raises(TypeError): quint2uint(42)