Spaces:
Runtime error
Runtime error
| # File: src/data_loader.py | |
| # Purpose: Generate and load synthetic hospital appointment data for testing | |
| import json | |
| import random | |
| from pathlib import Path | |
| from datetime import datetime, timedelta | |
| import sys | |
| sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) | |
| from config import SYNTHETIC_DATA_PATH, DEPARTMENTS, AVAILABLE_SLOTS | |
| def generate_synthetic_appointments(n: int = 50) -> list[dict]: | |
| """ | |
| Generate n synthetic appointment records to simulate a hospital database. | |
| Each record has: appointment_id, patient_name, department, date, slot, status. | |
| """ | |
| records = [] | |
| base_date = datetime.today() | |
| for i in range(1, n + 1): | |
| offset_days = random.randint(0, 14) | |
| appt_date = (base_date + timedelta(days=offset_days)).strftime("%Y-%m-%d") | |
| records.append({ | |
| "appointment_id": f"APT-{10000 + i}", | |
| "patient_name": f"Patient_{i}", | |
| "department": random.choice(DEPARTMENTS), | |
| "date": appt_date, | |
| "slot": random.choice(AVAILABLE_SLOTS), | |
| "status": "booked", | |
| "doctor": f"Dr. Sample_{random.randint(1, 5)}", | |
| }) | |
| return records | |
| def save_synthetic_data(n: int = 50): | |
| """Save generated appointments to disk for mock API use.""" | |
| SYNTHETIC_DATA_PATH.parent.mkdir(parents=True, exist_ok=True) | |
| records = generate_synthetic_appointments(n) | |
| with open(SYNTHETIC_DATA_PATH, "w") as f: | |
| json.dump(records, f, indent=2) | |
| print(f"Saved {n} synthetic appointments to {SYNTHETIC_DATA_PATH}") | |
| return records | |
| def load_appointments() -> list[dict]: | |
| """Load existing appointment data from disk.""" | |
| if not SYNTHETIC_DATA_PATH.exists(): | |
| print("No data found. Generating synthetic data...") | |
| return save_synthetic_data() | |
| with open(SYNTHETIC_DATA_PATH) as f: | |
| return json.load(f) | |
| if __name__ == "__main__": | |
| records = save_synthetic_data(n=50) | |
| print(f"Sample record: {records[0]}") |