Files
microsoft--promptflow/examples/flows/standard/maths-to-code/code_execution.py
T
wehub-resource-sync e768098d0e
Flake8 Lint / flake8 (push) Waiting to run
Spell check CI / Spell_Check (push) Waiting to run
tools_continuous_delivery / Private PyPI non-main branch release (push) Has been skipped
tools_continuous_delivery / Private PyPI main branch release (push) Failing after 2m42s
Publish Promptflow Doc / Build (push) Has been cancelled
Publish Promptflow Doc / Deploy (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 13:39:52 +08:00

34 lines
1.0 KiB
Python

from promptflow.core import tool
import sys
from io import StringIO
@tool
def func_exe(code_snippet: str):
if code_snippet == "JSONDecodeError" or code_snippet.startswith("Unknown Error:"):
return code_snippet
# Define the result variable before executing the code snippet
old_stdout = sys.stdout
redirected_output = sys.stdout = StringIO()
# Execute the code snippet
try:
exec(code_snippet.lstrip())
except Exception as e:
sys.stdout = old_stdout
return str(e)
sys.stdout = old_stdout
return redirected_output.getvalue().strip()
if __name__ == "__main__":
print(func_exe("print(5+3)"))
print(func_exe("count = 0\nfor i in range(100):\n if i % 8 == 0:\n count += 1\nprint(count)"))
print(func_exe("sum = 0\ni = 0\nwhile 3**i < 100:\n sum += 3**i\n i += 1\nprint(sum)"))
print(func_exe("speed_A = 80\nspeed_B = 120\ndistance = 2000\ntime = distance / (speed_A + speed_B)\nprint(time)"))
print(func_exe("Unknown Error"))
print(func_exe("JSONDecodeError"))