import ast
import operator as op

# supported operators
operators = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul, ast.Mod: op.mod,
             ast.Div: op.truediv, ast.Pow: op.pow, ast.BitXor: op.xor,
             ast.USub: op.neg}

def eval_expr(expr):
    """
    >>> eval_expr('2^6')
    4
    >>> eval_expr('2**6')
    64
    >>> eval_expr('1 + 2*3**(4^5) / (6 + -7)')
    -5.0
    """

    result = eval_(ast.parse(expr, mode='eval').body)
    if isinstance(result, basestring):
        return '"' + result + '"'
    else:
        return result

def eval_(node):
    if isinstance(node, ast.Num): # <number>
        return node.n
    elif isinstance(node, ast.Str): #string
        return node.s
    elif isinstance(node, ast.BinOp): # <left> <operator> <right>
        return operators[type(node.op)](eval_(node.left), eval_(node.right))
    elif isinstance(node, ast.UnaryOp): # <operator> <operand> e.g., -1
        return operators[type(node.op)](eval_(node.operand))
    elif isinstance(node, ast.Name):
        return node.id
    else:
        raise TypeError(node)


'''
expr = "20 < 30"
operator = "<"
lhs,rhs = map(ast.literal_eval, map(str.strip, expr.split(operator)))
eval("%s %s %s"%(lhs,operator,rhs))
'''