MCPcopy Create free account
hub / github.com/SAIC-MONTREAL/SAGE / run_code

Function run_code

sage/utils/trigger_server.py:132–154  ·  view source on GitHub ↗

Run some code in a string and return the result Args: code_define (str): the code that does imports, function definitions, etc code_run (str): the one line whose result you want to output.

(code_define: str, code_run: str)

Source from the content-addressed store, hash-verified

130
131
132def run_code(code_define: str, code_run: str) -> Any:
133 """
134 Run some code in a string and return the result
135
136 Args:
137 code_define (str): the code that does imports, function definitions, etc
138 code_run (str): the one line whose result you want to output.
139 """
140 # adding the wrapper function is needed to get the imports to work
141 # add indentation
142 code_define = "\n ".join(code_define.split("\n"))
143 code_run = code_run.strip("\n")
144 wrapper_fn = """
145def wrapper():
146 %s
147
148 return %s
149""" % (
150 code_define,
151 code_run,
152 )
153 exec(wrapper_fn)
154 return eval("wrapper()")
155
156
157def check_conditions(condition_registry: list[dict], code_registry: dict[str, dict]):

Callers 2

_runMethod · 0.90
check_conditionsFunction · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected