slugid

2.0.0 · maintenance · verified Sun Apr 12

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

Install

Imports

Quickstart

Generate both standard and 'nice' URL-safe slugs. The example also briefly touches on the conceptual decoding back to a UUID, though the library primarily focuses on slug generation from UUIDs.

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.

view raw JSON →