From ed9f2f421b1475b2f9a24b7a395005fe562bbf3d Mon Sep 17 00:00:00 2001 From: David Ormsbee Date: Tue, 14 Feb 2012 12:10:09 -0500 Subject: [PATCH] sort keys so evaluation takes longer terms first - vars like 'e2' will be substituted in before mathematical constants like 'e' --- courseware/capa/calc.py | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/courseware/capa/calc.py b/courseware/capa/calc.py index 41ced03e58..be44050888 100644 --- a/courseware/capa/calc.py +++ b/courseware/capa/calc.py @@ -119,7 +119,10 @@ def evaluator(variables, functions, string): # Handle variables passed in. E.g. if we have {'R':0.5}, we make the substitution. # Special case for no variables because of how we understand PyParsing is put together if len(all_variables)>0: - varnames = sreduce(lambda x,y:x|y, map(lambda x: CaselessLiteral(x), all_variables.keys())) + # We sort the list so that var names (like "e2") match before + # mathematical constants (like "e"). This is kind of a hack. + all_variables_keys = sorted(all_variables.keys(), key=len, reverse=True) + varnames = sreduce(lambda x,y:x|y, map(lambda x: CaselessLiteral(x), all_variables_keys)) varnames.setParseAction(lambda x:map(lambda y:all_variables[y], x)) else: varnames=NoMatch()