3422372948
- Fix 1: 移除误 commit 的 __pycache__ 文件 - Fix 2: 创建 pytest.ini 解决 PYTHONPATH 问题 - Fix 3: load_config 添加错误处理(FileNotFoundError + ValueError) Co-Authored-By: Claude <noreply@anthropic.com>
30 lines
813 B
Python
30 lines
813 B
Python
# sanguo_data/config.py
|
|
from dataclasses import dataclass
|
|
import yaml
|
|
|
|
@dataclass(frozen=True)
|
|
class DataConfig:
|
|
data_paths: dict
|
|
data_sources: dict
|
|
validation: dict
|
|
performance: dict
|
|
|
|
def load_config(path: str) -> DataConfig:
|
|
try:
|
|
with open(path, "r", encoding="utf-8") as f:
|
|
raw = yaml.safe_load(f)
|
|
except FileNotFoundError:
|
|
raise FileNotFoundError(f"配置文件不存在: {path}")
|
|
except yaml.YAMLError as e:
|
|
raise ValueError(f"YAML解析失败: {e}")
|
|
|
|
if not raw:
|
|
raise ValueError(f"配置文件为空: {path}")
|
|
|
|
return DataConfig(
|
|
data_paths=raw.get("data_paths", {}),
|
|
data_sources=raw.get("data_sources", {}),
|
|
validation=raw.get("validation", {}),
|
|
performance=raw.get("performance", {}),
|
|
)
|