Spaces:
Sleeping
Sleeping
Download app/data_loader.py from skillissue04/sql-repair-env: direct link, hf CLI and curl.
- Browser
- Download file 3.31 kB
-
https://huggingface.co/spaces/skillissue04/sql-repair-env/resolve/refs%2Fpr%2F2/app/data_loader.py
- Command line
-
hf download hf://spaces/skillissue04/sql-repair-env@refs/pr/2/app/data_loader.py
-
curl -L -o data_loader.py https://huggingface.co/spaces/skillissue04/sql-repair-env/resolve/refs%2Fpr%2F2/app/data_loader.py
3.31 kB
| """ | |
| 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]) | |