Skip to content

Instantly share code, notes, and snippets.

@takinbo
Created February 19, 2012 17:57
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takinbo/1864861 to your computer and use it in GitHub Desktop.
Save takinbo/1864861 to your computer and use it in GitHub Desktop.
url shortening snippet
import string
import sys
# list of valid characters to use in the shortener slug
CHARSET = string.digits + string.lowercase + string.uppercase
# convert a numeric id to a url-shortener-type slug
def shorty(id):
slug = ""
while id > 0:
remainder = id % len(CHARSET)
# perform integer division on the id
id /= len(CHARSET)
slug += CHARSET[remainder]
return slug
# do an inverse conversion from a url-shortener-type slug to numeric id
def inv_shorty(slug):
id = 0
for idx, char in enumerate(slug):
id += pow(len(CHARSET), idx) * CHARSET.find(char)
return id
if __name__ == '__main__':
if len(sys.argv) < 2:
print 'Usage: %s id' % (sys.argv[0])
else:
print shorty(int(sys.argv[1]))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment