From f538d36947384c8cdfd78ae473b73b4834e8b671 Mon Sep 17 00:00:00 2001 From: Robert Zajac Date: Sun, 21 Oct 2018 14:06:56 -0400 Subject: [PATCH 1/2] Basic multiaddr representation. --- network/multiaddr.py | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 network/multiaddr.py diff --git a/network/multiaddr.py b/network/multiaddr.py new file mode 100644 index 0000000..38c826d --- /dev/null +++ b/network/multiaddr.py @@ -0,0 +1,39 @@ +class Multiaddr: + + def __init__(self, addr): + if not addr[0] == "/": + raise MultiaddrValueError("Invalid input multiaddr.") + + addr = addr[1:] + protocol_map = dict() + split_addr = addr.split("/") + + if len(split_addr) == 0 or len(split_addr) % 2 != 0: + raise MultiaddrValueError("Invalid input multiaddr.") + + is_protocol = True + curr_protocol = "" + + for addr_part in split_addr: + if is_protocol: + curr_protocol = addr_part + else: + protocol_map[curr_protocol] = addr_part + is_protocol = not is_protocol + + self.protocol_map = protocol_map + self.addr = addr + + def get_protocols(self): + return list(self.protocol_map.keys()) + + def get_protocol_value(self, protocol): + if protocol not in self.protocol_map: + return None + + return self.protocol_map[protocol] + + +class MultiaddrValueError(ValueError): + """Raised when the input string to the Multiaddr constructor was invalid.""" + pass From d59a0907b4418c86a307dfc5e6d84f2d810a30b3 Mon Sep 17 00:00:00 2001 From: Robert Zajac Date: Sun, 21 Oct 2018 14:16:26 -0400 Subject: [PATCH 2/2] Updating with results from pylinter --- network/multiaddr.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/network/multiaddr.py b/network/multiaddr.py index 38c826d..e6d4a5c 100644 --- a/network/multiaddr.py +++ b/network/multiaddr.py @@ -8,7 +8,7 @@ class Multiaddr: protocol_map = dict() split_addr = addr.split("/") - if len(split_addr) == 0 or len(split_addr) % 2 != 0: + if not split_addr or len(split_addr) % 2 != 0: raise MultiaddrValueError("Invalid input multiaddr.") is_protocol = True