0

I'm quite new to this so apologies in advance if this is a silly question. I'm trying to build a simple module which takes a Pandas dataframe and a set of instructions (in some text format) as an input, and converts this dataframe to a nested JSON representation of the data.

My plan was to essentially leave a section of the code blank and let the user supply the code (instructions) for how to do this conversion. I'm sure this is not a good way to solve the problem, and I'd happily take pointer on how it should be done as well, but is there a way to let the user insert a section of code into the code itself, and then execute the program?

  • 2
    you *could* use python eval to execute arbitrarily any code but it is probably a bad idea h(ow would you protect yourself from malicious code?) It would be better to have an idea of what sort of data manipulations you want to support and build a mini DSL around that. – Will Jun 04 '19 at 20:31
  • 1
    Python functions can accept other functions as arguments. – Blorgbeard Jun 04 '19 at 20:32

1 Answers1

0

Take a look at the exec function.

If you need some control over the validation of the instructions, maybe it's better to do as @will says. Take a look at this similar question