Closed ch3njust1n closed 1 year ago
Use python's ast package to check the syntax of generated code at runtime.
import ast def is_code_correct(code): try: ast.parse(code) return True except SyntaxError: return False # test print(is_code_correct('print("Hello, world!")')) # True print(is_code_correct('print("Hello, world!')) # False - missing closing quote
Use python's ast package to check the syntax of generated code at runtime.