def _check_expression(text, allowed_variables=[]):
 try:
  module = parse(text)
 except SyntaxError:
  return False
 if (not isinstance(module, Module)):
  return False
 statements = module.body
 if (not (len(statements) == 1)):
  return False
 expression = statements[0]
 if (expression.__class__.__name__ != 'Expr'):
  return False
 for ast_node in walk(expression):
  ast_node_class = ast_node.__class__.__name__
  if (ast_node_class not in AST_NODE_TYPE_WHITELIST):
   return False
  if (ast_node_class == 'Name'):
   if (not _check_name(ast_node, allowed_variables)):
    return False
  elif (ast_node_class == 'Call'):
   if (not _check_call(ast_node)):
    return False
  elif (ast_node_class == 'Attribute'):
   if (not _check_attribute(ast_node)):
    return False
 return True

def _check_expression ( text , allowed_variables = [ ] ) : try : module = parse ( text ) except SyntaxError : return False if ( not isinstance ( module , Module ) ) : return False statements = module . body if ( not ( len ( statements ) == 1 ) ) : return False expression = statements [ 0 ] if ( expression . __class__ . __name__ != 'Expr' ) : return False for ast_node in walk ( expression ) : ast_node_class = ast_node . __class__ . __name__ if ( ast_node_class not in AST_NODE_TYPE_WHITELIST ) : return False if ( ast_node_class == 'Name' ) : if ( not _check_name ( ast_node , allowed_variables ) ) : return False elif ( ast_node_class == 'Call' ) : if ( not _check_call ( ast_node ) ) : return False elif ( ast_node_class == 'Attribute' ) : if ( not _check_attribute ( ast_node ) ) : return False return True
make sure an expression is not an empty string parameters expr : object an object that can be converted to a string raises valueerror * if expr is an empty string .