larzuuid
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
Why
- 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.
Install
pip install larzuuid
Usage
from 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")
Tests
python -m unittest discover -s tests -v # 13 tests
The Larz stack
One of 30+ pure-Python, zero-dependency libraries at github.com/larz-scripter.
License
MIT © larz-scripter
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
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 larzuuid-0.1.0.tar.gz.
File metadata
- Download URL: larzuuid-0.1.0.tar.gz
- Upload date:
- Size: 5.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e952f36a22148270dc160202d97e7e4903e362a6533a5f767486da67bdcf949b
|
|
| MD5 |
ec106f184fa2590b650b78df15033c5e
|
|
| BLAKE2b-256 |
f21fa3eb8cdb177616521fc8630ca6f32e36fedb65b363b7f6c1de2c7bb7c5de
|
File details
Details for the file larzuuid-0.1.0-py3-none-any.whl.
File metadata
- Download URL: larzuuid-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
19cdd0b38e047f42e36f9dce95e44925c43bb3bbf3a9359ce1cfe75cf3d6d109
|
|
| MD5 |
63dc18292745bbeaf96fab125bbc4269
|
|
| BLAKE2b-256 |
4692f0360ff391fc050a52a3676eaa83fa71dff115c0aab5b449be541b94b96f
|