|
import inspect |
|
|
|
|
|
|
|
def code_interpreter(code_markdown: str) -> dict | str: |
|
""" |
|
Execute the provided Python code string on the terminal using exec. |
|
|
|
The string should contain valid, executable and pure Python code in markdown syntax. |
|
Code should also import any required Python packages. |
|
|
|
Args: |
|
code_markdown (str): The Python code with markdown syntax to be executed. |
|
For example: ```python\n<code-string>\n``` |
|
|
|
Returns: |
|
dict | str: A dictionary containing variables declared and values returned by function calls, |
|
or an error message if an exception occurred. |
|
|
|
Note: |
|
Use this function with caution, as executing arbitrary code can pose security risks. Use it only for numerical calculations. |
|
""" |
|
try: |
|
|
|
code_lines = code_markdown.split('\n')[1:-1] |
|
code_without_markdown = '\n'.join(code_lines) |
|
|
|
|
|
exec_namespace = {} |
|
|
|
|
|
exec(code_without_markdown, exec_namespace) |
|
|
|
|
|
result_dict = {} |
|
for name, value in exec_namespace.items(): |
|
if callable(value): |
|
try: |
|
result_dict[name] = value() |
|
except TypeError: |
|
|
|
arg_names = inspect.getfullargspec(value).args |
|
args = {arg_name: exec_namespace.get(arg_name) for arg_name in arg_names} |
|
result_dict[name] = value(**args) |
|
elif not name.startswith('_'): |
|
result_dict[name] = value |
|
|
|
return result_dict |
|
|
|
except Exception as e: |
|
error_message = f"An error occurred: {e}" |
|
return error_message |
|
|