From 4f72d07c471fb758592c6b17fca2d9883aa4699e Mon Sep 17 00:00:00 2001 From: 0xflotus <0xflotus@gmail.com> Date: Wed, 11 Jan 2023 00:09:29 +0100 Subject: [PATCH] v0.2.1 --- rfc1924/__init__.py | 2 +- rfc1924/rfc1924.py | 28 +++++++++++++++++----------- rfc1924/test_rfc1924.py | 2 +- setup.py | 2 +- 4 files changed, 20 insertions(+), 14 deletions(-) diff --git a/rfc1924/__init__.py b/rfc1924/__init__.py index 3c90cc4..b2e400b 100644 --- a/rfc1924/__init__.py +++ b/rfc1924/__init__.py @@ -1,4 +1,4 @@ -__version__ = "0.2.0" +__version__ = "0.2.1" __author__ = "0xflotus" from .rfc1924 import encode, decode, savings diff --git a/rfc1924/rfc1924.py b/rfc1924/rfc1924.py index a6d7461..beb2e12 100644 --- a/rfc1924/rfc1924.py +++ b/rfc1924/rfc1924.py @@ -1,4 +1,5 @@ from ipaddress import IPv6Address +from functools import reduce lookup_table = [ "0", @@ -101,17 +102,22 @@ def encode(ipv6): def decode(encoded_ipv6): - exp = 0o23 - sum = 0 - for elem in list( - map(lambda x: lookup_table.index(x), ",".join(encoded_ipv6).split(",")) - ): - sum = sum + elem * 0x55 ** exp - exp -= 1 - return str(IPv6Address(sum)) + return str( + IPv6Address( + reduce( + lambda sum, vec: sum + vec[0] * 0x55 ** (0o23 - vec[1]), + map( + lambda vec: (lookup_table.index(vec[0]), vec[1]), + zip(encoded_ipv6, range(0o24)), + ), + 0, + ) + ) + ) def savings(ipv6): - encoded = encode(ipv6) - saving = int((1 - len(encoded) / len(ipv6)) * 100) - return f"You saved {saving}%" + return f"You saved {int((1 - len(encode(ipv6)) / len(ipv6)) * 100)}%" + + +print(decode("AN?6(i3Y+yVr74uX@J3P")) diff --git a/rfc1924/test_rfc1924.py b/rfc1924/test_rfc1924.py index 7a5f72c..697e2b9 100644 --- a/rfc1924/test_rfc1924.py +++ b/rfc1924/test_rfc1924.py @@ -3,4 +3,4 @@ assert encode("2345:425:2ca1::567:5673:23b5") == "AN?6(i3Y+yVr74uX@J3P" assert decode("AN?6(i3Y+yVr74uX@J3P") == "2345:425:2ca1::567:5673:23b5" -print(savings("2345:425:2ca1::567:5673:23b5")) \ No newline at end of file +print(savings("2345:425:2ca1::567:5673:23b5")) diff --git a/setup.py b/setup.py index ef69846..58b66ee 100644 --- a/setup.py +++ b/setup.py @@ -6,7 +6,7 @@ setup( name="rfc1924", - version="0.2.0", + version="0.2.1", description="Implementation of RFC 1924", long_description=README, long_description_content_type="text/markdown",