Skip to content

Instantly share code, notes, and snippets.

@jihchi
Forked from afternoon/url_safe_uuid.py
Created September 3, 2013 04:03
Show Gist options
  • Save jihchi/6419619 to your computer and use it in GitHub Desktop.
Save jihchi/6419619 to your computer and use it in GitHub Desktop.
#!/usr/bin/python
# Generate URL-safe UUID/GUIDs in Python, e.g.
#
# ob9G9Ju_Re6SRgxacdUzhw
# k0CWKgThQq-9b2ZcmpVXXA
#
# base64 has an urlsafe encoding
from base64 import urlsafe_b64encode
# any of uuid1, uuid3, uuid4 or uuid5 will do
# see http://docs.python.org/2/library/uuid.html
from uuid import uuid4 as uuid
# generate a uuid and base64 encode it
# drop trailing "==", add it back if decoding
print urlsafe_b64encode(uuid().bytes)[0:22]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment