config.py 1.1 KB

12345678910111213141516171819202122232425262728293031323334
  1. from __future__ import annotations
  2. from datetime import date
  3. from pathlib import Path
  4. import yaml
  5. from src.data.exceptions import UnknownInstrumentError
  6. from src.data.models import Instrument
  7. def load_instruments(config_path: Path) -> dict[str, Instrument]:
  8. with config_path.open("r", encoding="utf-8") as handle:
  9. payload = yaml.safe_load(handle) or {}
  10. instruments: dict[str, Instrument] = {}
  11. for key, raw in (payload.get("instruments") or {}).items():
  12. instruments[key] = Instrument(
  13. key=key,
  14. name=raw["name"],
  15. index_code=str(raw["index_code"]),
  16. provider_symbol=str(raw["provider_symbol"]),
  17. exchange=str(raw["exchange"]),
  18. price_type=str(raw["price_type"]),
  19. bootstrap_start=date.fromisoformat(str(raw["bootstrap_start"])),
  20. )
  21. return instruments
  22. def get_instrument(instruments: dict[str, Instrument], key: str) -> Instrument:
  23. try:
  24. return instruments[key]
  25. except KeyError as exc:
  26. raise UnknownInstrumentError(f"Unknown instrument: {key}") from exc