Spaces:
Sleeping
Sleeping
| from __future__ import annotations | |
| from dataclasses import dataclass | |
| from pathlib import Path | |
| from typing import Dict, List | |
| import yaml | |
| CONFIG_PATH = Path(__file__).resolve().parent.parent / "config" / "error_categories.yaml" | |
| class ErrorCategory: | |
| id: int | |
| name: str | |
| description: str | |
| def load_categories(config_path: Path = CONFIG_PATH) -> List[ErrorCategory]: | |
| with open(config_path) as f: | |
| data = yaml.safe_load(f) | |
| return [ | |
| ErrorCategory(id=c["id"], name=c["name"], description=c["description"]) | |
| for c in data["categories"] | |
| ] | |
| def id_to_name(categories: List[ErrorCategory] | None = None) -> Dict[int, str]: | |
| cats = categories or load_categories() | |
| return {c.id: c.name for c in cats} | |
| def name_to_id(categories: List[ErrorCategory] | None = None) -> Dict[str, int]: | |
| cats = categories or load_categories() | |
| return {c.name: c.id for c in cats} | |