ShortUUID
Shortuuid is a Python library that generates concise, unambiguous, and URL-safe UUIDs. It provides a simple API to generate UUIDs, encode integers, and decode strings using a customizable alphabet. The current version is 1.0.13, and releases are infrequent but stable, with a focus on maintenance.
Warnings
- breaking Version 1.0.0 changed the internal byte order for encoding/decoding from Least Significant Byte (LSB) first to Most Significant Byte (MSB) first. This means UUIDs generated with `shortuuid` < 1.0.0 cannot be decoded by `shortuuid` >= 1.0.0, and vice-versa.
- breaking Python 2.x support was dropped in version 1.0.0.
- gotcha Choosing a custom alphabet or a very short length for generated UUIDs can significantly increase the probability of collisions. The default alphabet and length (22 characters) are chosen for a good balance of brevity and uniqueness.
- gotcha While the default alphabet is URL-safe, if you provide a custom alphabet, you are responsible for ensuring its characters are safe for your specific URL or filesystem context (e.g., avoiding characters like '/', '?', '&', '=', etc. without proper encoding).
Install
-
pip install shortuuid
Imports
- shortuuid
import shortuuid
- ShortUUID (for custom alphabet)
import shortuuid su = shortuuid.ShortUUID(alphabet='abcdef0123456789')
Quickstart
import shortuuid
# Generate a standard short UUID (default length 22)
my_uuid = shortuuid.uuid()
print(f"Generated UUID: {my_uuid}")
# Generate a UUID of a specific length
short_uuid_10 = shortuuid.uuid(length=10)
print(f"10-char UUID: {short_uuid_10}")
# Encode an integer to a short UUID string
encoded_str = shortuuid.encode(123456789)
print(f"Encoded integer: {encoded_str}")
# Decode a short UUID string back to an integer
decoded_int = shortuuid.decode(encoded_str)
print(f"Decoded integer: {decoded_int}")
# Use a custom alphabet
custom_su = shortuuid.ShortUUID(alphabet="0123456789abcdef")
custom_uuid = custom_su.uuid()
print(f"Custom alphabet UUID: {custom_uuid}")