How to Mock an Exposure Event Log Record in Python

Generate a realistic exposure event record with UUID, UTC timestamp, and risk level for testing or experimentation.

Easy Python 3.9+ Aug 9, 2026 A/B testing & experimentation 16 views 0 copies

Python code

19 lines
Python 3.9+
import uuid
from datetime import datetime, timezone


def mock_exposure_event(person_id: str, location: str, duration_minutes: int) -> dict:
    return {
        "event_id": str(uuid.uuid4()),
        "person_id": person_id,
        "location": location,
        "duration_minutes": duration_minutes,
        "timestamp_utc": datetime.now(timezone.utc).isoformat(),
        "risk_level": "high" if duration_minutes >= 15 else "low",
    }


if __name__ == "__main__":
    event = mock_exposure_event("P-1024", "Gym - Weight Room", 22)
    for key, value in event.items():
        print(f"{key}: {value}")

Output

stdout
event_id: 3f2c1b4a-9e8d-4f6b-8a7c-2e5d1f0a9b3c
person_id: P-1024
location: Gym - Weight Room
duration_minutes: 22
timestamp_utc: 2025-01-15T14:30:45.123456+00:00
risk_level: high

How it works

The function uses uuid.uuid4() to generate a unique event ID, ensuring each record is distinguishable. datetime.now(timezone.utc).isoformat() provides a timezone-aware UTC timestamp in ISO 8601 format, which is critical for comparing events across regions. The risk level is derived from the duration threshold (15 minutes), making the record self-contained and realistic. This pattern is ideal for generating test data in A/B testing pipelines or experimentation platforms where consistent event mocking is needed.

Common mistakes

  • Using naive datetime instead of timezone-aware UTC timestamps, causing timezone comparison bugs
  • Forgetting to convert duration_minutes to int, leading to type errors in downstream logic
  • Reusing the same UUID for multiple events, breaking uniqueness assumptions

Variations

  1. Add a `location_id` field to map locations to a normalized dictionary for analytics
  2. Use `secrets.token_hex(16)` for a shorter, URL-safe event identifier

Real-world use cases

  • Generating mock exposure events to validate A/B test bucketing logic before launching a feature experiment.
  • Creating synthetic event streams for load testing a contact-tracing notification system.
  • Feeding fake exposure records into a dashboard to demo risk-level aggregation without real user data.

Sponsored

Run this sample

Open the browser IDE to tweak the example and see results without installing anything.

Open editor

More from A/B testing & experimentation

Related tutorials and quizzes for this topic.