Skip to content

Instantly share code, notes, and snippets.

@amontalenti
Created March 26, 2012 12:06
Show Gist options
  • Save amontalenti/2204671 to your computer and use it in GitHub Desktop.
Save amontalenti/2204671 to your computer and use it in GitHub Desktop.
basic shortener implementation
def to_base(q, alphabet):
if q < 0: raise ValueError, "must supply a positive integer"
l = len(alphabet)
converted = []
while q != 0:
q, r = divmod(q, l)
converted.insert(0, alphabet[r])
return "".join(converted) or '0'
def to62(q):
"""Base 62 identifiers for integers based upon a random shuffling of string.letters + string.digits"""
return to_base(q, '2FQYNEJAUsbGu41zndZTeocMai5H7OIjXkKg8qyt3WC9hLplxfVBm0wSRr6vPD')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment