Modern, sortable IDs — UUIDv7, ULID, nanoid. Pure Python, zero dependencies.
Random UUIDv4s are unguessable but scatter across a database index, hurting insert
performance and locality. Time-ordered ids fix that: they sort by creation
time, cluster in the index, and let you read the timestamp back out. The stdlib
uuid module has none of these — larzuuid adds the three you actually want.
from larzuuid import uuid7, ulid, nanoid
uuid7() # 018f5e...-7... a real UUID, but time-sortable
ulid() # 01HZY3K5... 26-char Crockford base32, sortable
nanoid() # V1StGXR8_Z5jdHi6B-myT compact, URL-safe- UUIDv7 (RFC 9562) — a standard
uuid.UUID, but the first 48 bits are a millisecond timestamp, so it sorts by time and works as a great primary key. Read the time back withuuid7_timestamp(). - ULID — 26 lexicographically-sortable Crockford-base32 chars;
MonotonicULIDguarantees strictly increasing ids even within the same millisecond. - nanoid — a compact, URL-safe random id (21 chars by default), uniform via rejection sampling, with a custom alphabet option.
- Zero dependencies. No
ulid-py, nonanoid, nouuid6.
pip install larzuuidfrom larzuuid import uuid7, uuid7_timestamp, ulid, ulid_timestamp, MonotonicULID, nanoid
pk = uuid7() # use as a DB key; sorts by creation time
uuid7_timestamp(pk) # datetime (UTC)
u = ulid(); ulid_timestamp(u)
gen = MonotonicULID(); gen.new(); gen.new() # strictly increasing
nanoid() # 21 chars
nanoid(10, alphabet="0123456789abcdef")python -m unittest discover -s tests -v # 13 testsOne of 30+ pure-Python, zero-dependency libraries at github.com/larz-scripter.
MIT © larz-scripter