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 timegenerate_with_timestamp(partition: int, timestamp: int) -> str- Generate with specific timestampgenerate_example() -> str- Generate an example PFID (for documentation)generate_related(existing_pfid: str) -> str- Generate with same partitiongenerate_root() -> str- Generate with random partitiongenerate_binary(partition: int) -> bytes- Generate binary PFIDgenerate_binary_with_timestamp(partition: int, timestamp: int) -> bytes- Generate binary with timestampgenerate_partition() -> int- Generate a random partitionencode(binary: bytes) -> str- Encode binary to stringdecode(pfid: str) -> bytes- Decode string to binaryextract_partition(pfid: str) -> int- Extract partition from PFIDis_pfid(value: any) -> bool- Check if value is a valid PFIDzero() -> str- Return the zero PFID
Exceptions
PfidError- Base exception for PFID errorscode: PfidErrorCode- The error codePfidErrorCode.INVALID_BINARY- Invalid binary inputPfidErrorCode.INVALID_PFID- Invalid PFID stringPfidErrorCode.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
Release history Release notifications | RSS feed
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)
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
pfid-0.1.0-py3-none-any.whl
(6.7 kB
view details)
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4806989cdb08f3889e8889250b818490f97f8d935deb9d12761bbc3d24d91ccf
|
|
| MD5 |
e9f9be4cd9fea8882c3aaa87d3a94d82
|
|
| BLAKE2b-256 |
48ab47b24290739828437b86f2a5a954e40f2cdb2f1e6e0b944ade1445b98213
|
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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
8001d049d082171303240b80ae74aa82c3727a2d79532805a30173d6bc742a9e
|
|
| MD5 |
bf2d5f08bd07706a0167f08fa7ff9d83
|
|
| BLAKE2b-256 |
78b4677b8e739bb6ab967785da05215120a749dc71d4e675b5eaf3393410b2af
|