Is there a way to convert a string to a function in Python while maintaining indentation, newline characters etc.?
For eg., take in "def fn1():\n\tprint("Hello World")"
and convert it to a function:
def fn1():
print("Hello World)
My use case is to allow a user to pass in a function defined by them as a JSON object which is then used in an otherwise static engine.
An example would be:
def run_user_code(fn_str):
fn=convert_to_fn(fn_str)
return fn # or call fn here like fn()
Similar to this question but in Python