Skip to content

Commit

Permalink
Merge pull request #16 from z0u/master
Browse files Browse the repository at this point in the history
Inequality operators and other fixes
  • Loading branch information
Vera Mazhuga authored Aug 19, 2016
2 parents d321189 + 241545f commit 56ea2a1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 63 deletions.
92 changes: 29 additions & 63 deletions py_expression_eval/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -532,7 +532,7 @@ def parse(self, expr):
if self.isOperator():
if self.isSign() and expected & self.SIGN:
if self.isNegativeSign():
self.tokenprio = 2
self.tokenprio = 5
self.tokenindex = '-'
noperators += 1
self.addfunc(tokenstack, operstack, TOP1)
Expand Down Expand Up @@ -752,66 +752,32 @@ def isConst(self):
return False

def isOperator(self):
code = self.expression[self.pos]
if code == '+':
self.tokenprio = 0
self.tokenindex = '+'
elif code == '-':
self.tokenprio = 0
self.tokenindex = '-'
elif code == '|':
if self.expression[self.pos] == '|':
self.pos += 1
self.tokenprio = 0
self.tokenindex = '||'
else:
return False
elif code == '=':
if self.expression[self.pos + 1] == "=":
self.pos += 1
self.tokenprio = 1
self.tokenindex = "=="

else:
return False
elif code == "!":
if self.expression[self.pos + 1] == "=":
self.pos += 1
self.tokenprio = 1
self.tokenindex = "!="
else:
return False
elif code == 'a':
if self.expression[self.pos + 1] == 'n' and self.expression[self.pos + 2] == 'd':
self.pos += 2
self.tokenprio = 0
self.tokenindex = "and"
else:
return False
elif code == 'o':
if self.expression[self.pos + 1] == 'r':
self.pos += 1
self.tokenprio = 0
self.tokenindex = "or"
else:
return False

elif code == '*':
self.tokenprio = 1
self.tokenindex = '*'
elif code == '/':
self.tokenprio = 2
self.tokenindex = '/'
elif code == '%':
self.tokenprio = 2
self.tokenindex = '%'
elif code == '^':
self.tokenprio = 3
self.tokenindex = '^'
else:
return False
self.pos += 1
return True
ops = (
('+', 2, '+'),
('-', 2, '-'),
('*', 3, '*'),
(u'\u2219', 3, '*'), # bullet operator
(u'\u2022', 3, '*'), # black small circle
('/', 4, '/'),
('%', 4, '%'),
('^', 6, '^'),
('||', 1, '||'),
('==', 1, '=='),
('!=', 1, '!='),
('<=', 1, '<='),
('>=', 1, '>='),
('<', 1, '<'),
('>', 1, '>'),
('and', 0, 'and'),
('or', 0, 'or'),
)
for token, priority, index in ops:
if self.expression.startswith(token, self.pos):
self.tokenprio = priority
self.tokenindex = index
self.pos += len(token)
return True
return False

def isSign(self):
code = self.expression[self.pos - 1]
Expand Down Expand Up @@ -867,7 +833,7 @@ def isOp1(self):
str += c
if len(str) > 0 and str in self.ops1:
self.tokenindex = str
self.tokenprio = 5
self.tokenprio = 7
self.pos += len(str)
return True
return False
Expand All @@ -882,7 +848,7 @@ def isOp2(self):
str += c
if len(str) > 0 and (str in self.ops2):
self.tokenindex = str
self.tokenprio = 5
self.tokenprio = 7
self.pos += len(str)
return True
return False
Expand Down
13 changes: 13 additions & 0 deletions py_expression_eval/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,18 @@ def test_parser(self):
self.assertEqual(parser.parse('lulu(x,y)').variables(), ['lulu','x','y'])

#evaluate
self.assertEqual(parser.parse('1').evaluate({}), 1)
self.assertEqual(parser.parse('a').evaluate({'a': 2}), 2)
self.assertEqual(parser.parse('2 * 3').evaluate({}), 6)
self.assertEqual(parser.parse(u'2 \u2219 3').evaluate({}), 6)
self.assertEqual(parser.parse(u'2 \u2022 3').evaluate({}), 6)
self.assertEqual(parser.parse('2 ^ x').evaluate({'x': 3}), 8)
self.assertEqual(parser.parse('x < 3').evaluate({'x': 3}), False)
self.assertEqual(parser.parse('x < 3').evaluate({'x': 2}), True)
self.assertEqual(parser.parse('x <= 3').evaluate({'x': 3}), True)
self.assertEqual(parser.parse('x <= 3').evaluate({'x': 4}), False)
self.assertEqual(parser.parse('x > 3').evaluate({'x': 4}), True)
self.assertEqual(parser.parse('x >= 3').evaluate({'x': 3}), True)
self.assertEqual(parser.parse('2 * x + 1').evaluate({'x': 3}), 7)
self.assertEqual(parser.parse('2 + 3 * x').evaluate({'x': 4}), 14)
self.assertEqual(parser.parse('(2 + 3) * x').evaluate({'x': 4}), 20)
Expand Down Expand Up @@ -81,6 +91,9 @@ def test_parser(self):
self.assertEqual(expr.variables(), ['x', 'y'])
self.assertEqual(expr.simplify({'y': 4}).variables(), ['x'])

# list operations
self.assertEqual(parser.parse('a, 3').evaluate({'a': [1, 2]}), [1, 2, 3])

def test_consts(self):
# self.assertEqual(self.parser.parse("PI ").variables(), [""])
self.assertEqual(self.parser.parse("PI").variables(), [])
Expand Down

0 comments on commit 56ea2a1

Please sign in to comment.