Skip to main content

An ID format designed to meet the trade-off between space and human readability.

Project description

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.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

afidgen-1.0.0rc0.tar.gz (19.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

afidgen-1.0.0rc0-py3-none-any.whl (5.1 kB view details)

Uploaded Python 3

File details

Details for the file afidgen-1.0.0rc0.tar.gz.

File metadata

  • Download URL: afidgen-1.0.0rc0.tar.gz
  • Upload date:
  • Size: 19.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for afidgen-1.0.0rc0.tar.gz
Algorithm Hash digest
SHA256 dae9965e5c5a23601467dec53f9df2a060db3ae85bce11770541176e26907cbe
MD5 40099942b185beb99622b1ecd884c3bb
BLAKE2b-256 4c5e95b754521b2e3457f2896cb5f3db20dcc92a8e2593f341b7129e120bbedd

See more details on using hashes here.

Provenance

The following attestation bundles were made for afidgen-1.0.0rc0.tar.gz:

Publisher: publish.yml on aJanuary/afid

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file afidgen-1.0.0rc0-py3-none-any.whl.

File metadata

  • Download URL: afidgen-1.0.0rc0-py3-none-any.whl
  • Upload date:
  • Size: 5.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for afidgen-1.0.0rc0-py3-none-any.whl
Algorithm Hash digest
SHA256 f2d7fa6b4b40c5d102924173212c11f573b27ddf2ea643f64fd22b290722689d
MD5 38c2fbecc441d2986b0d167f27b1b440
BLAKE2b-256 338753c41d2913e23368100786f37dcb62b488972762fdfcd0f1bda732407c82

See more details on using hashes here.

Provenance

The following attestation bundles were made for afidgen-1.0.0rc0-py3-none-any.whl:

Publisher: publish.yml on aJanuary/afid

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page