Download tools/python_tool.py from LonghaoWang/Final_Assignment_Template: direct link, hf CLI and curl.
- Browser
- Download file 734 Bytes
-
https://huggingface.co/spaces/LonghaoWang/Final_Assignment_Template/resolve/main/tools/python_tool.py
- Command line
-
hf download hf://spaces/LonghaoWang/Final_Assignment_Template/tools/python_tool.py
-
curl -L -o python_tool.py https://huggingface.co/spaces/LonghaoWang/Final_Assignment_Template/resolve/main/tools/python_tool.py
734 Bytes
| """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 | |