How do I convert arithemtic string (like "2+2") to a number?

  • Thread starter Arvid Andersson
  • Start date
A

Arvid Andersson

Hello, I need to convert a string to a number, but the string can
contain +,-,* and / as well as parenthesis. For example, if I have the
string "30/(6+9)" I would like a function that returned the number 2.

I actually wrote a java function that did this a couple of years ago, in
school, as an excersise in "binary trees". I lost it, and most of my
programming knowledge, but I figured perhaps there is a way to do this
easily in python? It seems to me like it could be a common problem.

/Arvid Andersson
 
A

Adam DePrince

Use the eval function:

2

Michael

Sure, if you trust the source of the string you are evaluating. If this
is a form submission on your web site, be wary of the nefarious user who
might submit to you:

commands.getoutput( "rm -rf %(HOME)s"%os.environ )

as the "expression" to evaluate.


Adam DePrince
 
M

Michael Hartl

Adam brings up a good point: eval is a very general function which
evaluates an arbitrary Python expression. As a result, it (and its
close cousin exec) should be used with caution if security is an issue.

Michael
 
R

Reinhold Birkenfeld

Michael said:
Adam brings up a good point: eval is a very general function which
evaluates an arbitrary Python expression. As a result, it (and its
close cousin exec) should be used with caution if security is an issue.

To get a secure eval for simple mathematical expressions, it should
suffice to check the string in the following way:

It does not contain characters other than operators, numbers, dots and
parentheses (perhaps you want to allow 'e' for floats, but you should
make sure that the 'e' is surrounded by numbers or optionally followed
by a +/- sign).

If you want to go a step further, you could parse the string to eval
with the parser/tokenize/... modules and verify the parse tree that it
contains nothing except operators and numbers.

Reinhold
 
A

Adomas

Well, a bit more secure would be

eval(expression, {'__builtins__': {}}, {})

or alike.
 
J

John J. Lee

Adomas said:
Well, a bit more secure would be

eval(expression, {'__builtins__': {}}, {})

or alike.

Don't believe this without (or even with ;-) very careful thought,
anyone. Google for rexec.


John
 
M

Michael Spencer

John said:
Don't believe this without (or even with ;-) very careful thought,
anyone. Google for rexec.


John
This module provides a more systematic way to set up restricted evaluation:


"""Restricted evaluation

Main entry point: r_eval()
For usage see class tests or run them using testall()"""


import types
import compiler
import operator

import sys, os # used only for testing

ast = compiler.ast

class Eval_Error(Exception):
def __init__(self,error,descr = None,node = None):
self.error = error
self.descr = descr

def __repr__(self):
return "%s: %s" % (self.error, self.descr)
__str__ = __repr__


class AbstractVisitor(object):
"""Standard depth-first AST walker - dispatches to methods
based on Node class name"""
def __init__(self):
self._cache = {} # dispatch table

def visit(self, node,**kw):
if node is None: return None
cls = node.__class__
meth = self._cache.setdefault(cls,
getattr(self,'visit'+cls.__name__,self.default))
return meth(node, **kw)

def default(self, node, **kw):
for child in node.getChildNodes():
return self.visit(child, **kw)
visitExpression = default



class Eval(AbstractVisitor):
"""An AST walker that implements a replacement to built-in eval.

See r_eval for entry point/usage.

Provides hooks for managing name resolution, proxying objects,
and controlling attribute access

Does not implement:
List Comprehensions, Generator Expressions, Lambda
Ellipsis (can this be used without numpy?)
"""
def __init__(self, context = globals()):
super(Eval,self).__init__()
self.context = context

# Namespace interface. Safe implementations should override these methods
# to implement restricted evaluation. This implementation simply
# evals the name in self.context and provides no proxying or
# attribute lookup restrictions

def lookup(self, objname):
"""Called only by visitName. Raise an exception here
to prevent any direct name resolution, but note that
objects may be returned by callables or attribute lookups"""
return eval(objname, self.context)

def getObject(self, obj):
"""Called by all name resolvers and by CallFunc. Provides
a hook for proxying unsafe objects"""
return obj

def getAttribute(self,obj,attrname):
"""Called by visitGetattr"""
return getattr(obj,attrname)

# End Namespace interface


# Syntax nodes follow by topic group. Delete methods to disallow
# certain syntax.

# Object constructor nodes
def visitConst(self, node, **kw):
return node.value
def visitDict(self,node,**kw):
return dict([(self.visit(k),self.visit(v)) for k,v in node.items])
def visitTuple(self,node, **kw):
return tuple(self.visit(i) for i in node.nodes)
def visitList(self,node, **kw):
return [self.visit(i) for i in node.nodes]
def visitSliceobj(self,node,**kw):
return slice(*[self.visit(i) for i in node.nodes])
def visitEllipsis(self,node,**kw):
raise NotImplementedError, "Ellipsis"

# Binary Ops
def visitAdd(self,node,**kw):
return self.visit(node.left) + self.visit(node.right)
def visitDiv(self,node,**kw):
return self.visit(node.left) / self.visit(node.right)
def visitFloorDiv(self,node,**kw):
return self.visit(node.left) // self.visit(node.right)
def visitLeftShift(self,node,**kw):
return self.visit(node.left) << self.visit(node.right)
def visitMod(self,node,**kw):
return self.visit(node.left) % self.visit(node.right)
def visitMul(self,node,**kw):
return self.visit(node.left) * self.visit(node.right)
def visitPower(self,node,**kw):
return self.visit(node.left) ** self.visit(node.right)
def visitRightShift(self,node,**kw):
return self.visit(node.left) >> self.visit(node.right)
def visitSub(self,node,**kw):
return self.visit(node.left) - self.visit(node.right)

# Unary ops
def visitNot(self,node,*kw):
return not self.visit(node.expr)
def visitUnarySub(self,node,*kw):
return -self.visit(node.expr)
def visitInvert(self,node,*kw):
return ~self.visit(node.expr)
def visitUnaryAdd(self,node,*kw):
return +self.visit(node.expr)

# Logical Ops
def visitAnd(self,node,**kw):
return reduce(lambda a,b: a and b,[self.visit(arg) for arg in node.nodes])
def visitBitand(self,node,**kw):
return reduce(lambda a,b: a & b,[self.visit(arg) for arg in node.nodes])
def visitBitor(self,node,**kw):
return reduce(lambda a,b: a | b,[self.visit(arg) for arg in node.nodes])
def visitBitxor(self,node,**kw):
return reduce(lambda a,b: a ^ b,[self.visit(arg) for arg in node.nodes])
def visitCompare(self,node,**kw):
comparisons = {
"<": operator.lt, # strictly less than
"<=": operator.le,# less than or equal
">": operator.gt, # strictly greater than
">=": operator.ge, # greater than or equal
"==": operator.eq, # equal
"!=": operator.ne, # not equal
"<>": operator.ne, # not equal
"is": operator.is_, # object identity
"is not": operator.is_not # negated object identity
}
obj = self.visit(node.expr)
for op, compnode in node.ops:
compobj = self.visit(compnode)
if not comparisons[op](obj, compobj):
return False
obj = compobj
return True
def visitOr(self,node,**kw):
return reduce(lambda a,b: a or b,[self.visit(arg) for arg in node.nodes])


# Name resolution
def visitGetattr(self,node,**kw):
obj = self.visit(node.expr)
return self.getAttribute(obj,node.attrname)

def visitName(self,node, **kw):
return self.lookup(node.name)

def visitSlice(self,node,**kw):
obj = self.visit(node.expr)
objslice = obj[self.visit(node.lower):self.visit(node.upper)]
return self.getObject(objslice)

def visitSubscript(self,node,**kw):
obj = self.visit(node.expr)
subs = node.subs
if len(subs) > 1:
raise NotImplementedError, "Subscript must be integer or slice"
assert node.flags == "OP_APPLY"
return self.getObject(operator.getitem(obj,self.visit(subs[0])))


# Callables
def visitCallFunc(self,node,**kw):
func = self.visit(node.node)

args = node.args
kwargs = self.visit(node.dstar_args) or {}
posargs = []
for arg in node.args:
if isinstance(arg, ast.Keyword):
keyword, value = self.visit(arg)
if keyword in kwargs:
raise TypeError, "%s() got multiple values for keyword
argument '%s'" \
% (func,keyword)
kwargs[keyword] = value
else:
posargs.append(self.visit(arg))
posargs.extend(node.star_args or [])

return self.getObject(func(*posargs,**kwargs))

def visitKeyword(self,node,**kw):
return node.name, self.visit(node.expr)

# Miscellaneous

def visitBackquote(self, node, **kw):
return repr(self.visit(node.expr))

# Function/class definition

def visitLambda(self, node, **kw):
raise NotImplementedError, "Lambda"

# Iterator evaluations

def visitGenExpr(self,node,*kw):
raise NotImplemetedError, "GenExpr"
def visitListComp(self,node,*kw):
raise NotImplemetedError, "ListComp"

# Some unexpected node type
def default(self, node, **kw):
raise Eval_Error("Unsupported source construct",
node.__class__,node)




def r_eval(source, context = None):
"""eval partial replacement,

Does not implement:
List Comprehensions, Generator Expressions, Lambda. Ellipsis

"""

walker = Eval(context or globals())
try:
ast = compiler.parse(source,"eval")
except SyntaxError, err:
raise
try:
return walker.visit(ast)
except Eval_Error, err:
raise


class tests(object):

def run(self):
"""Run all the tests"""
for name in dir(self):
if name.startswith("test_"):
getattr(self,name)()
print "%s: OK" % name

def test_const(self):
cases = ["""[1, 2, 'Joe Smith', 8237972883334L, # comment
{'Favorite fruits': ['apple', 'banana', 'pear']}, # another comment
'xyzzy', [3, 5, [3.14159, 2.71828, []]]]"""]
for case in cases:
assert eval(case) == r_eval(case)

def test_algebra(self):
"""Show that r_eval matches eval on constant expressions"""
cases = [
"1+2/3 * 4.0 ** 5 % 2",
"(4 << 2 | 67 >> 2) ^ 0xFF",
]
for case in cases:
assert eval(case) == r_eval(case)
def test_names(self):
cases = [
"sum",
"sys.modules['os']",
]
for case in cases:
assert eval(case) == r_eval(case)

def test_calling(self):
cases = [
"getattr(object, 'subclasses'.join(['_'*2]*2))",
"type('Name', dict = {'a':1}, bases = (object,)).a",
"type(**dict(dict = {'a':1}, name = 'Name', bases = (object,))).a"
]
for case in cases:
assert eval(case) == r_eval(case)


def testall():
tests().run()
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,158
Latest member
Vinay_Kumar Nevatia
Top