Make OD-API for consistent for constraints, LHS patterns, RHS actions.

This commit is contained in:
Joeri Exelmans 2024-11-07 11:05:06 +01:00
parent 1eb8a84553
commit 9c68b288c1
8 changed files with 108 additions and 79 deletions

View file

@ -1,9 +1,13 @@
# based on https://stackoverflow.com/a/39381428
# Parses and executes a block of Python code, and returns the eval result of the last statement
import ast
def exec_then_eval(code, _globals, _locals):
def exec_then_eval(code, _globals={}, _locals={}):
block = ast.parse(code, mode='exec')
# assumes last node is an expression
last = ast.Expression(block.body.pop().value)
exec(compile(block, '<string>', mode='exec'), _globals, _locals)
return eval(compile(last, '<string>', mode='eval'), _globals, _locals)
extended_globals = {
'__builtins__': {'isinstance': isinstance, 'print': print, 'int': int, 'float': float, 'bool': bool, 'str': str, 'tuple': tuple, 'len': len, 'set': set, 'dict': dict },
**_globals,
}
exec(compile(block, '<string>', mode='exec'), extended_globals, _locals)
return eval(compile(last, '<string>', mode='eval'), extended_globals, _locals)