title: How to validation a user input expression, make sure the result is Boolean, and limiting the variable and methods used in python?
body: <p>My purpose is to validate if a user input expression is safe, legit and valid. For example, the expression could be: </p>
  
  <pre><code>exp = '''((value[0] == "failed" or value[0] == "blocked" or value[0] == "errored") and value[1] == "passed") '''
  </code></pre>
  
  <p>As pointed out by the comment, eval() is not a good way to to validation. Here is my constraints: </p>
  
  <ul>
  <li>Only the variable 'value' is used in the expression. </li>
  <li><p>Only a limited number of methos is allowed in the dictionary, for example:</p>
  
  <pre><code>safe_methods = ['cos', 'sum', 'sin']
  </code></pre></li>
  <li><p>The expression must generate a boolean result, either True for False.</p></li>
  </ul>
  
  <p>The syntax of the expression may be checked by code:</p>
  
  <pre><code>st = parser.expr(exp)
  </code></pre>
  
  <p>And check if an parser exception is thrown.
  My questions is how apply all these constraints to the expression?</p>