📄 id_generator.py 1,055 bytes May 02, 2026 📋 Raw

"""
ID Generation Utilities

Provides unique short numeric ID generation with prefixes.
Matches contract examples:
- ath_102938 (athlete)
- cas_554433 (case)
- evt_776655 (event)
- mil_001 (milestone)
- schl_001 (school)
"""
import time
import random

Atomic counter storage per prefix

_counter_lock = {}
_last_timestamp = {}

def generate_id(prefix: str) -> str:
"""
Generate unique short numeric ID with prefix

Uses timestamp + random for uniqueness without DB sequence dependency
Format: prefix + 9-digit number (6 from timestamp + 3 random)

Examples:
    - ath_102938
    - cas_554433
    - evt_776655
"""
# Use timestamp + random for uniqueness
ts = int(time.time() * 1000) % 1000000
rand = random.randint(0, 999)
return f"{prefix}{ts:06d}{rand:03d}"

def generate_id_with_counter(prefix: str, counter: int) -> str:
"""
Generate ID with explicit counter (for testing/sequencing)

Format: prefix + counter padded to 6 digits
"""
return f"{prefix}{counter:06d}"