afidgen
A Python library for generating afid ids.
Afids are compact, human-readable IDs made of lowercase letters, digits, and
hyphens. They come in two variants — short (20 chars, 75 bits of randomness) and
long (30 chars, 125 bits) — both shaped as prefix-tag-suffix:
res-2sed3-1p3dpw40ds # short
evt-2sed3-1p3dpw40dsabckdvc7p1 # long
The prefix is yours to assign (typically the resource type); the tag and suffix are random, drawn from a Crockford Base32 alphabet that omits easily-confused characters. See the afid spec for the full grammar and analysis.
Requirements
Python 3.9 or later.
Installing
pip install afidgen
Usage
import afidgen
# Generate a short ID with the prefix "res".
resource_id = afidgen.random_short("res")
# Generate a long ID with the prefix "evt".
event_id = afidgen.random_long("evt")
# Create a generator. This is more efficient for generating multiple afids,
# because it only needs to validate the prefix once when you create the
# generator, instead of every time you generate an ID.
gen = afidgen.Generator.long("evt")
event_ids = [gen() for _ in range(1_000)]
# Generator is also iterable.
import itertools
more_event_ids = list(itertools.islice(gen, 1_000))
# By default, generators use secrets.token_bytes (cryptographically secure).
# You can override it when creating a generator to provide your own source of
# randomness — for example, random.randbytes if you need reproducibility or
# don't need security. The function must take a number of bytes and return
# that many random bytes.
import random
gen = afidgen.Generator.long("txi", randbytes=random.randbytes)
tx_id = gen()
Performance
For bulk generation, build a generator once with Generator.short or
Generator.long and reuse it. The prefix is validated at construction,
so each call only pays for the randomness draw and the encoding.
random_short / random_long are convenience helpers that build a fresh
generator on every call. Use them for ad-hoc IDs; reach for the factories
in tight loops.
Run benchmarks with uv run pytest bench/.
Concurrency
The default randomness source (secrets.token_bytes) is thread-safe, so
a single Generator can be called from multiple threads. If you pass a
custom randbytes callable, concurrent safety follows that callable.
Developing
From libs/python/:
uv run pytest # tests
uv run ruff format --check . # format check
uv run ruff format . # apply formatting
uv run ruff check . # lint
uv run ty check # typecheck
uv run pytest bench/ # benchmarks
Initial setup: uv sync.
Metadata
Release files for afidgen 1.0.0
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 | Actions |
|---|---|---|---|---|
| afidgen-1.0.0.tar.gz | 19.9 kB | With provenance | Details for afidgen-1.0.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| afidgen-1.0.0-py3-none-any.whl | Python 3 | none | any | Details for afidgen-1.0.0-py3-none-any.whl |
Total release size: 25.0 kB
Release files / afidgen-1.0.0.tar.gz
| Download URL | afidgen-1.0.0.tar.gz |
|---|---|
| Size | 19.9 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
fea77730e7fd3f9390e17e586c18cd854befc8af06192a87ae682f69350d5eab
|
|
BLAKE2b-256 checksum How to use checksums |
42d8db277a6e0721c59f2edae07ce15b2e9dd270af0ee49f739f0096c54b919f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 12, 2026.
Transparency logRelease files / afidgen-1.0.0-py3-none-any.whl
| Download URL | afidgen-1.0.0-py3-none-any.whl |
|---|---|
| Size | 5.1 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7cbc4b73c7abe3b05f1d8902011cd52d7ce3e35e6a350d26587b17bb9ebba480
|
|
BLAKE2b-256 checksum How to use checksums |
36771c77aedee70b5c0d8676c16c979e27bd0a205e15f6df56202f7d2c3cc266
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.12
|
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.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 12, 2026.
Transparency logSecurity for afidgen 1.0.0
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.
Full provenance
Both files in this release have been uploaded with provenance metadata.
Attestation for afidgen-1.0.0.tar.gz
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 12, 2026.
Transparency logA 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.
To learn more about protecting yourself from malicious actors, visit our package security documentation.