Spaces:
Sleeping
Sleeping
File size: 3,308 Bytes
64ccc23 | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 | """
app/data_loader.py
==================
Loads task datasets from JSON files and provides fast lookup by id and difficulty.
All datasets are loaded once at import time (module-level singleton) so that
repeated reset() calls are O(1) dict lookups, not file I/O.
"""
from __future__ import annotations
import json
from pathlib import Path
from typing import Dict, List, Optional
from app.models import Difficulty, Task
# Resolve data directory relative to project root
_DATA_DIR = Path(__file__).parent.parent / "data"
# ---------------------------------------------------------------------------
# Internal registry
# ---------------------------------------------------------------------------
# Primary index: task_id → Task
_TASK_BY_ID: Dict[str, Task] = {}
# Secondary index: difficulty → list of Tasks (preserves JSON order)
_TASKS_BY_DIFFICULTY: Dict[Difficulty, List[Task]] = {
Difficulty.easy: [],
Difficulty.medium: [],
Difficulty.hard: [],
}
def _load_all() -> None:
"""Load all three dataset files into the registries. Called once at import."""
for diff in Difficulty:
path = _DATA_DIR / f"{diff.value}.json"
if not path.exists():
raise FileNotFoundError(
f"Dataset file not found: {path}\n"
f"Expected files: data/easy.json, data/medium.json, data/hard.json"
)
raw: List[dict] = json.loads(path.read_text(encoding="utf-8"))
for item in raw:
task = Task(**item)
if task.id in _TASK_BY_ID:
raise ValueError(
f"Duplicate task id '{task.id}' found in {path.name}. "
f"All task IDs must be globally unique."
)
_TASK_BY_ID[task.id] = task
_TASKS_BY_DIFFICULTY[task.difficulty].append(task)
# Run at import time
_load_all()
# ---------------------------------------------------------------------------
# Public API
# ---------------------------------------------------------------------------
def get_task(task_id: str) -> Task:
"""
Return the Task with the given id.
Raises
------
KeyError
If no task with that id exists.
"""
if task_id not in _TASK_BY_ID:
available = ", ".join(sorted(_TASK_BY_ID.keys()))
raise KeyError(
f"Unknown task_id '{task_id}'. Available task IDs: {available}"
)
return _TASK_BY_ID[task_id]
def get_tasks_by_difficulty(difficulty: Difficulty) -> List[Task]:
"""Return all tasks for the given difficulty level (ordered as in JSON)."""
return list(_TASKS_BY_DIFFICULTY[difficulty])
def get_all_tasks() -> List[Task]:
"""Return every task across all difficulties."""
return list(_TASK_BY_ID.values())
def task_exists(task_id: str) -> bool:
"""Return True if a task with the given id exists."""
return task_id in _TASK_BY_ID
def task_count(difficulty: Optional[Difficulty] = None) -> int:
"""
Return total task count, optionally filtered by difficulty.
Parameters
----------
difficulty : Difficulty | None
If None, returns total across all difficulties.
"""
if difficulty is None:
return len(_TASK_BY_ID)
return len(_TASKS_BY_DIFFICULTY[difficulty])
|