title: How can I give users the possibility to write scripts (in a secure way)?
body: <p>I currently have the problem where I need to write a function which receives a dictionary where all values are strings or dictionarys and outputs a string. The problem is the logic for creating this output string. I would like to let the user write the logic.</p>
  
  <p>Now, of course, I could just ask the user to create a valid Python script with a function</p>
  
  <pre><code>def generate_string(input_dict):
      # your logic
      return "some_string"
  </code></pre>
  
  <p>The problem is that I don't want the users to be able to execute arbitrary code. Especially working with the file system (reading / writing / deleting files) should not be possible. There should also be a limit in the computation time / memory usage.</p>
  
  <p>Is this possible?</p>
  
  <p>Currently, I let them enter a format string. But this seems to be too limited as it misses <code>if</code> statements.</p>
  
  <h2>Example</h2>
  
  <p>This is just a minimal, abstract example:</p>
  
  <pre><code>def generate_string(input_dict):
      if input_dict['type'] == 1:
          return "I want my date in {d:%Y%m%d}".format(d=input_dict['date'])
      elif input_dict['type'] == 2:
          return "type is {}".format(input_dict['type'])
      return "some_string"
  
  d = {'type': 1, 'date': datetime.date(2017, 1, 14)}
  generate_string(d)
  </code></pre>