Skip to content

Instantly share code, notes, and snippets.

@bryanchow
Last active October 1, 2019 04:34
Show Gist options
  • Save bryanchow/8b5bd3c90ab41c82f68d95f7d96f28ed to your computer and use it in GitHub Desktop.
Save bryanchow/8b5bd3c90ab41c82f68d95f7d96f28ed to your computer and use it in GitHub Desktop.
# https://gist.github.com/bryanchow/8b5bd3c90ab41c82f68d95f7d96f28ed
import shortuuid
from django.conf import settings
DEFAULT_ALPHABET = "0123456789abcdef"
def make_unique_code(length=None, namespace=None, alphabet=None):
"""
Generate a concise, unique identifier code.
"""
alphabet = (
alphabet or getattr(settings, 'UNIQUE_CODE_ALPHABET', DEFAULT_ALPHABET)
)
shortuuid.set_alphabet(alphabet)
if namespace:
code = shortuuid.uuid(name=namespace)
else:
code = shortuuid.uuid()
return code[:length] if length is not None else code
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment