Friday, January 30, 2009

Tiny URLs

Want tinyurl.com-style links in your application? Need to generate a random sequence of alphanumerics? Here's a quick function in Python:

import random
import string

def short_id(num): return "".join(random.sample(string.digits + string.letters, num))

print short_id(6)

It will return something like AnLrJl or 9mLXut, or even bK5D4O. Simple and powerful.

No comments: