Skip to content

Instantly share code, notes, and snippets.

@mitsuhiko
Created November 28, 2012 12:27
Show Gist options
  • Save mitsuhiko/4160894 to your computer and use it in GitHub Desktop.
Save mitsuhiko/4160894 to your computer and use it in GitHub Desktop.
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)
@eloyz
Copy link

eloyz commented Nov 28, 2012

Wow. Thanks. You might be interested in this as well for your alphabet.
http://www.catonmat.net/blog/my-favorite-regex/

@mahmoudimus
Copy link

base94 :P

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment