"""Execute attached Python files safely-ish and capture final numeric output.""" from __future__ import annotations import re import subprocess import sys from pathlib import Path from typing import Optional def run_python_file(path: Path, timeout: int = 60) -> Optional[str]: proc = subprocess.run( [sys.executable, str(path)], capture_output=True, text=True, timeout=timeout, ) out = (proc.stdout or "").strip().splitlines() if not out: return None last = out[-1].strip() # Prefer a bare integer / float on the last line if re.fullmatch(r"-?\d+(\.\d+)?", last): return last m = re.search(r"-?\d+(\.\d+)?", last) return m.group(0) if m else last