Skip to content

Commit af7c290

Browse files
committed
Add EvalExpr script.
1 parent 30baef4 commit af7c290

4 files changed

Lines changed: 59 additions & 3 deletions

File tree

‎.gitignore‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
__pycache__

‎README.md‎

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ A collection of simple Python scripts.
55
### Python 3.3
66
+ [ScrapeCountries.py](http://bamos.github.io/2013/05/03/scraping-tables-python/) -
77
Scraping HTML tables with HTMLParser.
8-
9-
Tests using the [unittest](http://docs.python.org/3.3/library/unittest.html)
10-
framework are located in `ScrapeCountriesTest.py`.
8+
Tests are located in `ScrapeCountriesTest.py`.
9+
+ [EvalExpr.py](http://bamos.github.io/2013/08/07/python-expression-evaluator/)
10+
A module to evaluate a mathematical expression using Python's AST.
11+
Tests are located in `EvalExprTest.py`.

‎python3.3/EvalExpr.py‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# EvalExpr.py
2+
# A module to evaluate a mathematical expression using Python's AST.
3+
#
4+
# Original by: J. Sebastian at `http://stackoverflow.com/questions/2371436`
5+
# Modifications by: Brandon Amos <http://bamos.github.io>
6+
7+
import ast
8+
import operator as op
9+
10+
class EvalExpr:
11+
def __init__(self, varMap):
12+
self.ops = {ast.Add: op.add, ast.Sub: op.sub, ast.Mult: op.mul,
13+
ast.Div: op.truediv, ast.Mod: op.mod, ast.Pow: op.pow,
14+
ast.LShift: op.lshift, ast.RShift: op.rshift, ast.BitOr: op.or_,
15+
ast.BitXor: op.xor, ast.BitAnd: op.and_, ast.FloorDiv: op.floordiv,
16+
ast.Invert: op.invert, ast.Not: op.not_, ast.UAdd: op.pos, ast.USub: op.neg}
17+
self.varMap = varMap
18+
19+
def evalExpr(self, expr):
20+
return self.__eval(ast.parse(expr).body[0].value)
21+
22+
def __eval(self, node):
23+
if isinstance(node, ast.Num):
24+
return node.n
25+
elif type(node) in self.ops:
26+
return self.ops[type(node)]
27+
elif isinstance(node, ast.UnaryOp):
28+
return self.__eval(node.op)(self.__eval(node.operand))
29+
elif isinstance(node, ast.BinOp):
30+
return self.__eval(node.op)(self.__eval(node.left),
31+
self.__eval(node.right))
32+
elif node.id in self.varMap:
33+
return self.varMap[node.id]
34+
else:
35+
raise TypeError(node)

‎python3.3/EvalExprTest.py‎

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#!/usr/bin/env python3.3
2+
#
3+
# EvalExprTest.py
4+
# Tests for the EvalExpr.py module.
5+
6+
import unittest
7+
from EvalExpr import EvalExpr
8+
9+
class EvalExprTest(unittest.TestCase):
10+
def testExpressions(self):
11+
varMap = {"pi": 3.14}
12+
e = EvalExpr(varMap)
13+
self.assertAlmostEqual(e.evalExpr("-pi+1"), -2.14)
14+
self.assertAlmostEqual(e.evalExpr("(20+10)*3/2-3"), 42.0)
15+
self.assertAlmostEqual(e.evalExpr("1 << 4"), 16.0)
16+
self.assertAlmostEqual(e.evalExpr("1+(-2*3)"), -5.0)
17+
18+
if __name__ == '__main__':
19+
unittest.main()

0 commit comments

Comments
 (0)