slugid
slugid is a Python library (version 2.0.0, released 2019) for generating URL-safe Base64 encoded UUID v4 slugs. These 22-character slugs are compact and can be easily embedded in URLs, command-line parameters, or used as identifiers in various systems. It provides methods for generating standard v4 slugs and 'nice' slugs which always start with an alphanumeric character. The library is currently in maintenance mode with its GitHub repository archived, meaning it's no longer actively developed but remains functional.
Warnings
- gotcha Choosing between `slugid.v4()` and `slugid.nice()`: `nice()` slugs are guaranteed not to start with a hyphen (`-`), making them safer for use in command-line arguments. However, this comes at a slight cost of entropy (121 bits for `nice()` vs. 122 bits for `v4()`). Consider your specific use case to decide which method is appropriate.
- gotcha This `slugid` library (for encoding UUIDs) is distinct from `python-slugify` (for slugifying arbitrary text strings). Do not confuse their purposes; `slugid` focuses specifically on compact, URL-safe UUID representations.
- deprecated The GitHub repository for the Python `slugid` library (`taskcluster/slugid.py`) has been archived. This indicates that the project is no longer under active development and is in maintenance mode. While it remains functional, new features or significant bug fixes are unlikely.
Install
-
pip install slugid
Imports
- v4
import slugid; slugid.v4()
- nice
import slugid; slugid.nice()
Quickstart
import slugid
# Generate a standard v4 slug
standard_slug = slugid.v4()
print(f"Standard Slug: {standard_slug}")
# Generate a 'nice' slug (guaranteed not to start with '-')
nice_slug = slugid.nice()
print(f"Nice Slug: {nice_slug}")
# Decode a slug back to a UUID string (requires converting to a full UUID object first)
# For full round-trip, you might need to use the `uuid` library directly or a helper function
import uuid
def decode_slugid(slug):
# slugid's internal decode function isn't directly exposed for all use cases
# This demonstrates the principle of converting to UUID
# Note: slugid itself primarily focuses on *generating* slugs from UUIDs
# To decode, you'd typically base64url decode the 22-char string and then reconstruct UUID
# The underlying Python `uuid` library handles the canonical UUID representation.
# A direct decode function for the slugid string to uuid.UUID object is not directly exposed for Python slugid library itself,
# but the concept relies on standard base64url decoding.
# For simplicity, if you have a UUID string, you can convert it to a slugid.
# Let's show how to convert a UUID string to slugid, and then conceptually, it could be reversed if a direct method was exposed.
return uuid.UUID(bytes=uuid.b64decode(slug + '==')) #Conceptual decoding, might need adjustments based on exact implementation
# Example of encoding a UUID to a slug
some_uuid = uuid.uuid4()
encoded_slug = slugid.encode(some_uuid)
print(f"UUID {some_uuid} encoded to slug: {encoded_slug}")
# While slugid.py doesn't expose a direct `decode` for the slug string to uuid.UUID
# the underlying principle is base64url decoding, which can be done with `uuid` module's helper functions.