### Development environment setup and usage Source: https://github.com/oliverlambson/fastnanoid/blob/main/README.md Provides commands for setting up a local development environment for fastnanoid, including creating a virtual environment, installing dependencies, building the project, and running tests. ```sh # local env python -m venv .venv source .venv/bin/activate pip install -r requirements-dev.txt # build and use maturin develop python -c 'import fastnanoid; print(fastnanoid.generate())' # test cargo test pytest mypy ruff check ruff format --check ``` -------------------------------- ### Replace py-nanoid with fastnanoid Source: https://github.com/oliverlambson/fastnanoid/blob/main/README.md Demonstrates how to switch from using the 'nanoid' library to 'fastnanoid' by changing the import statement. This is a drop-in replacement for the generate() function. ```diff - from nanoid import generate + from fastnanoid import generate ``` -------------------------------- ### Convert UUID to URL-friendly string and back Source: https://github.com/oliverlambson/fastnanoid/blob/main/README.md Shows how to use fastnanoid's helper functions to encode a standard UUID into a URL-friendly string (base64url) and decode it back. This is useful when dealing with existing UUIDs. ```python import uuid from fastnanoid import urlid_to_uuid, uuid_to_urlid # Example UUID from a database id_ = uuid.uuid4() # type: uuid.UUID # Encode to base64url for a short string representation urlid = uuid_to_urlid(id_) # type: str # Decode back to a normal UUID decoded_urlid = urlid_to_uuid(urlid) # type: UUID ``` -------------------------------- ### Generate Nano ID with fastnanoid Source: https://context7.com/oliverlambson/fastnanoid/llms.txt Generates secure, URL-friendly unique string IDs using customizable alphabets and lengths. Defaults to a 21-character ID with a 64-character URL-safe alphabet. This function is a drop-in replacement for py-nanoid's generate function. ```python from fastnanoid import generate # Generate a default nano ID (21 characters, URL-safe alphabet) id = generate() print(id) # Output: "V1StGXR8_Z5jdHi6B-myT" # Generate a shorter ID short_id = generate(size=10) print(short_id) # Output: "IRFa-VaY2b" # Generate an ID with a custom alphabet numeric_id = generate(alphabet="0123456789", size=12) print(numeric_id) # Output: "839461728394" # Generate with custom alphabet including emoji custom_id = generate(alphabet="asdf🌍", size=8) print(custom_id) # Output: "🌍asd🌍fa" # Drop-in replacement for py-nanoid # Simply change: from nanoid import generate # To: from fastnanoid import generate ``` -------------------------------- ### Convert URL-Safe ID to UUID with fastnanoid Source: https://context7.com/oliverlambson/fastnanoid/llms.txt Decodes a base64url-encoded string (generated by uuid_to_urlid) back into a standard UUID object. This allows for seamless conversion of short, URL-friendly IDs back to UUIDs for backend operations. ```python import uuid from fastnanoid import urlid_to_uuid, uuid_to_urlid # Convert a URL-safe ID back to UUID url_id = 'XZjVeCcxSk22ZnDKFvEKog' id_ = urlid_to_uuid(url_id) print(id_) # Output: 5d98d578-2731-4a4d-b666-70ca16f10aa2 print(type(id_)) # Output: # Round-trip conversion original_uuid = uuid.uuid4() encoded = uuid_to_urlid(original_uuid) decoded = urlid_to_uuid(encoded) assert original_uuid == decoded # True # Use in web applications def get_user(url_id: str): """Accept short URL ID, query database with UUID""" user_uuid = urlid_to_uuid(url_id) # query database with user_uuid return {"id": str(user_uuid), "url_id": url_id} ``` -------------------------------- ### Convert UUID to URL-Safe ID with fastnanoid Source: https://context7.com/oliverlambson/fastnanoid/llms.txt Converts a standard UUID object into a compact, URL-safe base64-encoded string. This is useful for shortening UUIDs when they are used in URLs or as identifiers in web applications. ```python import uuid from fastnanoid import uuid_to_urlid # Convert an existing UUID to a short URL-safe string id_ = uuid.UUID('5d98d578-2731-4a4d-b666-70ca16f10aa2') url_id = uuid_to_urlid(id_) print(url_id) # Output: "XZjVeCcxSk22ZnDKFvEKog" # Generate a new UUID and convert it new_id = uuid.uuid4() short_url = uuid_to_urlid(new_id) print(f"UUID: {new_id}") # Output: "UUID: 3fa85f64-5717-4562-b3fc-2c963f66afa6" print(f"URL ID: {short_url}") # Output: "URL ID: P6hfZFcXRWKz_CyWP2avpg" # Useful for database IDs in URLs # /users/3fa85f64-5717-4562-b3fc-2c963f66afa6 (36 chars) # /users/P6hfZFcXRWKz_CyWP2avpg (22 chars) ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.