(player_name: str, path: str)
| 34 | |
| 35 | |
| 36 | def load_policy_module(player_name: str, path: str): |
| 37 | agent_dir = str(Path(path).resolve().parent) |
| 38 | inserted_agent_dir = agent_dir not in sys.path |
| 39 | if inserted_agent_dir: |
| 40 | sys.path.insert(0, agent_dir) |
| 41 | module_name = f"codeclash_scml_{safe_class_name(player_name).lower()}" |
| 42 | spec = importlib.util.spec_from_file_location(module_name, path) |
| 43 | if spec is None or spec.loader is None: |
| 44 | raise RuntimeError(f"Could not load module spec from {path}") |
| 45 | module = importlib.util.module_from_spec(spec) |
| 46 | try: |
| 47 | spec.loader.exec_module(module) |
| 48 | finally: |
| 49 | if inserted_agent_dir: |
| 50 | sys.path.remove(agent_dir) |
| 51 | if not hasattr(module, "decide") or not callable(module.decide): |
| 52 | raise RuntimeError(f"{path} must define a callable decide(observation)") |
| 53 | return module |
| 54 | |
| 55 | |
| 56 | def to_plain(value): |
no test coverage detected