def arithmeticEval(s):
 node = ast.parse(s, mode=u'eval')
 def _eval(node):
  if isinstance(node, ast.Expression):
   return _eval(node.body)
  elif isinstance(node, ast.Str):
   return node.s
  elif isinstance(node, ast.Num):
   return node.n
  elif isinstance(node, ast.BinOp):
   return _binOps[type(node.op)](_eval(node.left), _eval(node.right))
  else:
   raise Exception(u'Unsupported type {}'.format(node))
 return _eval(node.body)

def arithmeticEval ( s ) : node = ast . parse ( s , mode = u'eval' ) def _eval ( node ) : if isinstance ( node , ast . Expression ) : return _eval ( node . body ) elif isinstance ( node , ast . Str ) : return node . s elif isinstance ( node , ast . Num ) : return node . n elif isinstance ( node , ast . BinOp ) : return _binOps [ type ( node . op ) ] ( _eval ( node . left ) , _eval ( node . right ) ) else : raise Exception ( u'Unsupported type {}' . format ( node ) ) return _eval ( node . body )
a safe eval supporting basic arithmetic operations .