PrettyId
Pretty cool IDs for your APIs.
poetry add prettyid
Synopsis
Generate prefixed IDs, like Stripe does:
>>> from pretty_id import PrettyId
>>> pid = PrettyId.random("task")
PrettyId("task_068n34jrjdth1fqr2nm9a0sh50")
>>> pid.type, pid.id
('task', '068n34jrjdth1fqr2nm9a0sh50')
>>> pid.bytes
b'...'
Backwards compatible with UUID:
>>> pid = PrettyId.from_uuid("1d4e2ea4-c1ab-4a98-8eeb-898051ef0f71", type="task")
PrettyId("task_3n72x961nd59h3qbh6053vrfe4")
>>> pid.uuid
UUID('1d4e2ea4-c1ab-4a98-8eeb-898051ef0f71')
Works with Pydantic and FastAPI:
@app.route("/tasks/{id}")
def get_task_by_id(id: FriendlyId):
if id.type != "task":
raise HTTPException(status_code=400, detail="ID should start with 'task_'")
return {
"id": id,
"title": "TODO",
}
And with SQLAlchemy:
from pretty_id.ext.sqlalchemy import PrettyIdBinaryType
class TaskModel(Base):
id: Mapped[PrettyId] = mapped_column(
PrettyIdBinaryType("task"),
index=True,
unique=True,
primary_key=True,
default=partial(PrettyId.random, "task")
)
title: Mapped[str]
# Pass PrettyId instance or just string
select(TaskModel).filter(
TaskModel.id == "task_068n34jrjdth1fqr2nm9a0sh50"
)
Design
Generated IDs use UUIDv7 underneath:
>>> u = UUID(bytes=pid.bytes)
UUID('01917192-7b9b-7b10-bf5b-15ec95039128')
>>> u.version
7
This means they inherit some useful properties:
-
Natural Sorting: UUIDv7 values are time-sortable, which means you can sort them in increasing order based on when they were generated. Databases often require additional timestamp columns to sort records based on creation time. With PrettyId, you can achieve this sorting using the ID itself, eliminating the need for extra columns.
-
Optimized Indexing: Since UUIDv7 is time-sortable, database indexing mechanisms can better optimize the storage and retrieval processes, leading to faster query times especially for time-based queries.
-
Concurrency and Distribution: In distributed systems, generating unique, sequential IDs can be a challenge. UUIDv7 can be generated concurrently across multiple nodes without the risk of collisions, making it suitable for distributed architectures.
IDs are encoded as lowercase Base32 using Douglas Crockford’s alphabet. This makes them compact, readable, and case-insensitive.
In database, IDs are stored without prefix using a native UUID type or BINARY(16). (We assume that the prefix can be determined from the table name.)
Metadata
Release files for prettyid 0.3.6
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | |
|---|---|---|---|---|
| prettyid-0.3.6.tar.gz | 4.8 kB | Without provenance | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| prettyid-0.3.6-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 11.1 kB
Release files / prettyid-0.3.6.tar.gz
| Download URL | prettyid-0.3.6.tar.gz |
|---|---|
| Size | 4.8 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
e7b6f8a8faffc5f7220b5d01d5870d63788987befa8c6ab6c84a0e76cc448717
|
|
BLAKE2b-256 checksum How to use checksums |
97a50fd48da1c9da73068c7c498f9e1724f81e415c011910e3cef93d28c44449
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Release files / prettyid-0.3.6-py3-none-any.whl
| Download URL | prettyid-0.3.6-py3-none-any.whl |
|---|---|
| Size | 6.3 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
69a6bc9337a15ad80dd9e043822da9c622f151953e53970595c4512b48a62cbf
|
|
BLAKE2b-256 checksum How to use checksums |
9d9cb3757a944973d5de870f554e1418f97cae08eddad2ebc70fd0a31a74a712
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.11.7 {"installer":{"name":"uv","version":"0.11.7","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}
|
Security for prettyid 0.3.6
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.