Created
November 28, 2012 12:27
-
-
Save mitsuhiko/4160894 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
Wow. Thanks. You might be interested in this as well for your alphabet.
http://www.catonmat.net/blog/my-favorite-regex/