Repository Public MIT Research preview
1"""Charge une implémentation du laboratoire (starter ou corrigé) sans modifier l'installation de Python.23 modules = charger("solution") # ou "starter"4 modules.recherche.rechercher_bm25(...)56Les trois fichiers du laboratoire (recherche.py, memoire.py, evaluation.py) s'importent entre eux par leur nom7court : le dossier choisi est donc placé en tête de sys.path le temps du chargement, et les modules sont8rechargés à chaque appel pour qu'un starter et un corrigé puissent coexister dans le même processus.9"""10from __future__ import annotations1112import importlib13import sys14from pathlib import Path15from types import ModuleType, SimpleNamespace1617LABS = Path(__file__).resolve().parent18DOSSIERS = {"starter": LABS / "starter", "solution": LABS.parent / "solutions"}19NOMS = ("recherche", "memoire", "evaluation")202122def charger(implementation: str = "solution") -> SimpleNamespace:23 """Importe recherche, memoire et evaluation depuis le dossier demandé."""24 if implementation not in DOSSIERS:25 raise ValueError(f"implémentation inconnue : {implementation!r} (attendu {sorted(DOSSIERS)})")26 dossier = str(DOSSIERS[implementation])27 if str(LABS) not in sys.path:28 sys.path.insert(0, str(LABS))29 for nom in NOMS:30 sys.modules.pop(nom, None)31 sys.path.insert(0, dossier)32 try:33 charges: dict[str, ModuleType] = {nom: importlib.import_module(nom) for nom in NOMS}34 finally:35 sys.path.remove(dossier)36 return SimpleNamespace(**charges, dossier=dossier, implementation=implementation)37