MCPcopy Create free account
hub / github.com/disler/benchy / execute_python_code

Function execute_python_code

server/modules/execution_evaluators.py:48–73  ·  view source on GitHub ↗

Execute Python code and return the numeric output as a string.

(code: str)

Source from the content-addressed store, hash-verified

46
47
48def execute_python_code(code: str) -> str:
49 """
50 Execute Python code and return the numeric output as a string.
51 """
52 # Remove any surrounding quotes and whitespace
53 code = code.strip().strip("'").strip('"')
54
55 # Create a temporary file with the code
56 import tempfile
57
58 with tempfile.NamedTemporaryFile(mode="w", suffix=".py", delete=True) as tmp:
59 tmp.write(code)
60 tmp.flush()
61
62 # Execute the temporary file using uv
63 result = execute(f"uv run {tmp.name} --ignore-warnings")
64
65 # Try to parse the result as a number
66 try:
67 # Remove any extra whitespace or newlines
68 cleaned_result = result.strip()
69 # Convert to float and back to string to normalize format
70 return str(float(cleaned_result))
71 except (ValueError, TypeError):
72 # If conversion fails, return the raw result
73 return result
74
75
76def execute(code: str) -> str:

Callers 1

process_single_promptFunction · 0.90

Calls 1

executeFunction · 0.85

Tested by

no test coverage detected