Fast UUID v4, v5, v7 generation in Rust
Project description
PyUUID7
Fast UUID v4, v5, and v7 generation for Python, implemented in Rust with PyO3.
pyuuid7 is useful when you need standard UUID strings from Python code while keeping generation fast and dependency-light. It supports random UUIDs, deterministic name-based UUIDs, time-sortable UUIDs, validation, version detection, and canonical parsing.
Installation
pip install pyuuid7
Quick Start
import pyuuid7
user_id = pyuuid7.uuid7()
request_id = pyuuid7.uuid4()
tenant_id = pyuuid7.uuid5(
"6ba7b810-9dad-11d1-80b4-00c04fd430c8",
"example.com",
)
print(user_id)
print(request_id)
print(tenant_id)
All generated values are returned as canonical UUID strings:
019389a1-2b3c-7d4e-8f5a-6b7c8d9e0f1a
Features
| Function | Returns | Use it for |
|---|---|---|
pyuuid7.uuid4() |
str |
Random identifiers such as request IDs, trace IDs, and public tokens. |
pyuuid7.uuid5(namespace, name) |
str | None |
Deterministic IDs derived from a namespace UUID and a name. |
pyuuid7.uuid7() |
str |
Time-sortable IDs for database primary keys and event records. |
pyuuid7.is_valid(value) |
bool |
Checking whether a string is any valid UUID. |
pyuuid7.get_version(value) |
int | None |
Detecting the UUID version of a valid UUID string. |
pyuuid7.parse(value) |
str | None |
Normalizing UUID input to canonical lowercase format. |
Common Use Cases
Generate Time-Sortable IDs
UUID v7 includes a Unix timestamp and random data. The resulting strings sort naturally by creation time, which makes them a good fit for database primary keys, logs, events, and queues.
import pyuuid7
order = {
"id": pyuuid7.uuid7(),
"status": "pending",
}
print(order["id"])
Use UUID v7 in Application Models
from dataclasses import dataclass, field
from typing import Optional
import pyuuid7
@dataclass
class Event:
id: str = field(default_factory=pyuuid7.uuid7)
name: str = ""
payload: Optional[dict] = None
event = Event(name="user.created", payload={"user_id": "123"})
Create Deterministic IDs with UUID v5
UUID v5 returns the same UUID every time you pass the same namespace and name. This is useful when you want stable IDs for external resources, slugs, tenant names, or imported records.
import uuid
import pyuuid7
customer_id = pyuuid7.uuid5(str(uuid.NAMESPACE_DNS), "customer.example.com")
if customer_id is None:
raise ValueError("Invalid namespace UUID")
Validate and Normalize User Input
Use parse() when you want to accept UUID input in supported formats and store it in canonical lowercase form.
import pyuuid7
def normalize_uuid(value: str) -> str:
parsed = pyuuid7.parse(value)
if parsed is None:
raise ValueError("Invalid UUID")
return parsed
resource_id = normalize_uuid("urn:uuid:A1B2C3D4-E5F6-4A7B-8C9D-0E1F2A3B4C5D")
print(resource_id)
# a1b2c3d4-e5f6-4a7b-8c9d-0e1f2a3b4c5d
Route Behavior by UUID Version
import pyuuid7
def describe_uuid(value: str) -> str:
version = pyuuid7.get_version(value)
if version is None:
return "invalid UUID"
if version == 7:
return "time-sortable UUID"
if version == 4:
return "random UUID"
if version == 5:
return "name-based UUID"
return f"UUID version {version}"
UUID Versions
| Version | Description | Typical use |
|---|---|---|
| v4 | Random UUID with 122 random bits. | Request IDs, trace IDs, public identifiers. |
| v5 | Name-based UUID using SHA-1 over a namespace and name. | Stable deterministic identifiers. |
| v7 | Time-sortable UUID using Unix epoch milliseconds plus random data. | Database IDs, events, logs, sortable records. |
Error Handling
Functions that can fail because of invalid input return None:
import pyuuid7
assert pyuuid7.uuid5("invalid", "example.com") is None
assert pyuuid7.get_version("invalid") is None
assert pyuuid7.parse("invalid") is None
is_valid() returns False for invalid input:
import pyuuid7
assert pyuuid7.is_valid("invalid") is False
API Reference
uuid4() -> str
Generate a random UUID v4 string.
uuid5(namespace: str, name: str) -> str | None
Generate a deterministic UUID v5 string from a namespace UUID and a name. Returns None when namespace is not a valid UUID.
uuid7() -> str
Generate a time-sortable UUID v7 string.
is_valid(uuid_str: str) -> bool
Return True when uuid_str can be parsed as a UUID.
get_version(uuid_str: str) -> int | None
Return the UUID version number, or None when the input is invalid.
parse(uuid_str: str) -> str | None
Parse a UUID string and return the canonical lowercase representation, or None when the input is invalid.
Development
This project uses mise for tool versions and uv for Python dependency management.
# Install pinned tools
mise install
# Setup the development environment
mise run setup
# Build and install the extension in editable mode
mise run dev
# Run Python tests
mise run test
# Run Rust tests
mise exec -- cargo test
# Build distribution artifacts
mise run build
License
MIT
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distributions
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pyuuid7-0.1.1.tar.gz.
File metadata
- Download URL: pyuuid7-0.1.1.tar.gz
- Upload date:
- Size: 32.1 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
eb462b15b6ecbf217c3c2d43d7dbf437521d25a714b9da5001049682466a6267
|
|
| MD5 |
692f2555e8472d75d8545383ec614ff8
|
|
| BLAKE2b-256 |
6b7e7ae981623a4fd1df66a1bfedbef784a7e53b6f478efdf48c34e374a6a9c3
|
File details
Details for the file pyuuid7-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pyuuid7-0.1.1-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 250.9 kB
- Tags: CPython 3.12, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8b086cc7665fbb806e6ede8489e4c2b7b66912454b227a0753f66593aefab65f
|
|
| MD5 |
bb8f11476a480fef2278f2feff9fa36c
|
|
| BLAKE2b-256 |
d831c6e8c91e23ecfef77c134eacee02f6d13ece929af2b6ea7a2497d1c7486c
|
File details
Details for the file pyuuid7-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pyuuid7-0.1.1-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 251.3 kB
- Tags: CPython 3.11, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
825b7cc09d73f5152e0e4a6dfba65bdc3a8274eaa0f0e446432ea3e4ed29c21d
|
|
| MD5 |
3e48fd4bf65b86e2867732a52b72f720
|
|
| BLAKE2b-256 |
c081b6bc2b91402d5d06a14dbddf9c3d37a5f262109c11072b4bb93c3de7ccbf
|
File details
Details for the file pyuuid7-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pyuuid7-0.1.1-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 251.6 kB
- Tags: CPython 3.10, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
1a642769b207018d382cabf619eefe4ca3cf12b194d960f77160fc64bcff5152
|
|
| MD5 |
cd188a6e206b0b81a73aa3cb37a9ad42
|
|
| BLAKE2b-256 |
5916a42b335b066e16aca348d4b65497a1646f9dedbc8c68ed8cd1b73de4bd2c
|
File details
Details for the file pyuuid7-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.
File metadata
- Download URL: pyuuid7-0.1.1-cp39-cp39-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
- Upload date:
- Size: 251.5 kB
- Tags: CPython 3.9, manylinux: glibc 2.17+ x86-64
- Uploaded using Trusted Publishing? No
- Uploaded via: maturin/1.14.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6ab3f46a4d27be16cc626bd37758fa9be527f33f9573c0d2ab304802eb376590
|
|
| MD5 |
c5890bb9741cd381db60beebcf2c0cdb
|
|
| BLAKE2b-256 |
d9da6c49f10832a46d3b9cf08da31e164aad762c2854c8617be95ccc50af63c0
|