def literal_eval(node_or_string):
 _safe_names = {'None': None, 'True': True, 'False': False}
 if isinstance(node_or_string, basestring):
  node_or_string = parse(node_or_string, mode='eval')
 if isinstance(node_or_string, Expression):
  node_or_string = node_or_string.body
 def _convert(node):
  if isinstance(node, Str):
   return node.s
  elif isinstance(node, Num):
   return node.n
  elif isinstance(node, Tuple):
   return tuple(map(_convert, node.elts))
  elif isinstance(node, List):
   return list(map(_convert, node.elts))
  elif isinstance(node, Dict):
   return dict(((_convert(k), _convert(v)) for (k, v) in zip(node.keys, node.values)))
  elif isinstance(node, Name):
   if (node.id in _safe_names):
    return _safe_names[node.id]
  elif (isinstance(node, BinOp) and isinstance(node.op, (Add, Sub)) and isinstance(node.right, Num) and isinstance(node.right.n, complex) and isinstance(node.left, Num) and isinstance(node.left.n, (int, long, float))):
   left = node.left.n
   right = node.right.n
   if isinstance(node.op, Add):
    return (left + right)
   else:
    return (left - right)
  raise ValueError('malformed string')
 return _convert(node_or_string)

def literal_eval ( node_or_string ) : _safe_names = { 'None' : None , 'True' : True , 'False' : False } if isinstance ( node_or_string , basestring ) : node_or_string = parse ( node_or_string , mode = 'eval' ) if isinstance ( node_or_string , Expression ) : node_or_string = node_or_string . body def _convert ( node ) : if isinstance ( node , Str ) : return node . s elif isinstance ( node , Num ) : return node . n elif isinstance ( node , Tuple ) : return tuple ( map ( _convert , node . elts ) ) elif isinstance ( node , List ) : return list ( map ( _convert , node . elts ) ) elif isinstance ( node , Dict ) : return dict ( ( ( _convert ( k ) , _convert ( v ) ) for ( k , v ) in zip ( node . keys , node . values ) ) ) elif isinstance ( node , Name ) : if ( node . id in _safe_names ) : return _safe_names [ node . id ] elif ( isinstance ( node , BinOp ) and isinstance ( node . op , ( Add , Sub ) ) and isinstance ( node . right , Num ) and isinstance ( node . right . n , complex ) and isinstance ( node . left , Num ) and isinstance ( node . left . n , ( int , long , float ) ) ) : left = node . left . n right = node . right . n if isinstance ( node . op , Add ) : return ( left + right ) else : return ( left - right ) raise ValueError ( 'malformed string' ) return _convert ( node_or_string )
safely evaluate an expression node or a string containing a python expression .