Skip to content

Instantly share code, notes, and snippets.

@whynotavailable
Created March 6, 2025 12:41
Show Gist options
  • Save whynotavailable/424ab3d599a5dfaf5b6a731ee7f33655 to your computer and use it in GitHub Desktop.
Save whynotavailable/424ab3d599a5dfaf5b6a731ee7f33655 to your computer and use it in GitHub Desktop.
def safe_extract(expr: str, globals: typing.Dict[str, typing.Any]):
tree = ast.parse(expr, mode="eval")
code = compile(tree, filename="<ast>", mode="eval")
return eval(code, globals=globals)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment