Skip to main content

PFID - A ULID-like identifier format with partition support

Project description

PFID - Python

A ULID-like identifier format with partition support, implemented in Python.

Installation

pip install pfid

Or for development:

pip install -e ".[dev]"

Usage

Basic Generation

from pfid import generate, generate_root, generate_related

# Generate a PFID with a specific partition
pfid = generate(partition=123_456_789)
# e.g., "013xrzp12g3nqk8nzzzzzzzzzzzzzzzz"

# Generate a PFID with a random partition
root_pfid = generate_root()

# Generate a related PFID (same partition as existing)
related_pfid = generate_related(root_pfid)

Partition Extraction

from pfid import extract_partition, generate

pfid = generate(partition=123_456_789)
partition = extract_partition(pfid)
assert partition == 123_456_789

Encoding and Decoding

from pfid import encode, decode, generate_binary

# Generate binary PFID and encode to string
binary = generate_binary(partition=123_456_789)
pfid = encode(binary)

# Decode string PFID back to binary
decoded_binary = decode(pfid)
assert decoded_binary == binary

Validation

from pfid import is_pfid

assert is_pfid("013xrzp12g3nqk8nzzzzzzzzzzzzzzzz")
assert not is_pfid("invalid")
assert not is_pfid(123)

Zero PFID

from pfid import zero

# Get a zero PFID (useful as a placeholder)
zero_pfid = zero()
assert zero_pfid == "00000000000000000000000000000000"

PFID Structure

PFID consists of 160 bits (20 bytes) encoded as 32 Crockford Base32 characters:

  • 48 bits for timestamp (milliseconds since Unix epoch)
  • 30 bits for partition (allows up to 1,073,741,824 partitions)
  • 2 bits padding
  • 80 bits for randomness

API Reference

Functions

  • generate(partition: int) -> str - Generate a PFID with current time
  • generate_with_timestamp(partition: int, timestamp: int) -> str - Generate with specific timestamp
  • generate_example() -> str - Generate an example PFID (for documentation)
  • generate_related(existing_pfid: str) -> str - Generate with same partition
  • generate_root() -> str - Generate with random partition
  • generate_binary(partition: int) -> bytes - Generate binary PFID
  • generate_binary_with_timestamp(partition: int, timestamp: int) -> bytes - Generate binary with timestamp
  • generate_partition() -> int - Generate a random partition
  • encode(binary: bytes) -> str - Encode binary to string
  • decode(pfid: str) -> bytes - Decode string to binary
  • extract_partition(pfid: str) -> int - Extract partition from PFID
  • is_pfid(value: any) -> bool - Check if value is a valid PFID
  • zero() -> str - Return the zero PFID

Exceptions

  • PfidError - Base exception for PFID errors
    • code: PfidErrorCode - The error code
    • PfidErrorCode.INVALID_BINARY - Invalid binary input
    • PfidErrorCode.INVALID_PFID - Invalid PFID string
    • PfidErrorCode.INVALID_PARTITION - Invalid partition value

Development

Setup

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest

# Run tests with coverage
pytest --cov=pfid

# Type check
mypy src/pfid

# Lint
ruff check src/pfid tests

License

MIT License

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

pfid-0.1.0.tar.gz (8.1 kB view details)

Uploaded Source

Built Distribution

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

pfid-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file pfid-0.1.0.tar.gz.

File metadata

  • Download URL: pfid-0.1.0.tar.gz
  • Upload date:
  • Size: 8.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pfid-0.1.0.tar.gz
Algorithm Hash digest
SHA256 4806989cdb08f3889e8889250b818490f97f8d935deb9d12761bbc3d24d91ccf
MD5 e9f9be4cd9fea8882c3aaa87d3a94d82
BLAKE2b-256 48ab47b24290739828437b86f2a5a954e40f2cdb2f1e6e0b944ade1445b98213

See more details on using hashes here.

File details

Details for the file pfid-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: pfid-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.13.5

File hashes

Hashes for pfid-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 8001d049d082171303240b80ae74aa82c3727a2d79532805a30173d6bc742a9e
MD5 bf2d5f08bd07706a0167f08fa7ff9d83
BLAKE2b-256 78b4677b8e739bb6ab967785da05215120a749dc71d4e675b5eaf3393410b2af

See more details on using hashes here.

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