-
-
Save atdt/4164399 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import uuid | |
alphabet = ( | |
'!"#$%&\'()*+,-./0123456789:;<=>?@' | |
'ABCDEFGHIJKLMNOPQRSTUVWXYZ' | |
'[\\]^_`' | |
'abcdefghijklmnopqrstuvwxyz{|}~' | |
) | |
def uuid_compress(uuid): | |
num = uuid.int | |
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 uuid_decompress(string): | |
base = len(alphabet) | |
strlen = len(string) | |
num = 0 | |
for idx, char in enumerate(string): | |
power = strlen - (idx + 1) | |
num += alphabet.index(char) * (base ** power) | |
return uuid.UUID(int=num) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment