import uuid
import base62
i = uuid.uuid4()
print(i)
print(i.int)
b = base62.encode(i.int)
print(b)
e = base62.decode(b)
print(e)
i2 = uuid.UUID(int=e)
print(i2)
# BASE62 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"
BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
def encode(num, alphabet=BASE62):
"""Encode a positive number in Base X.
Arguments:
`num` (int): The number to encode
`alphabet` (str): The alphabet to use for encoding
"""
if num == 0:
return alphabet[0]
arr = []
base = len(alphabet)
while num:
num, rem = divmod(num, base)
arr.append(alphabet[rem])
arr.reverse()
return ''.join(arr)
def decode(string, alphabet=BASE62):
"""Decode a Base X encoded string into the number.
Arguments:
`string` (str): The encoded string
`alphabet` (str): The alphabet to use for encoding
"""
base = len(alphabet)
strlen = len(string)
num = 0
idx = 0
for char in string:
power = (strlen - (idx + 1))
num += alphabet.index(char) * (base**power)
idx += 1
return num
def verify(original_uuid, base62_str):
"""Verify that the base62-encoded string can be decoded back."""
reconstructed_uuid = uuid.UUID(int=decode(base62_str))
assert str(original_uuid) == str(reconstructed_uuid)
return True