summaryrefslogtreecommitdiff
path: root/src/examples/excelExpr.py
diff options
context:
space:
mode:
authorPaul McGuire <ptmcg@austin.rr.com>2016-01-27 23:42:22 +0000
committerPaul McGuire <ptmcg@austin.rr.com>2016-01-27 23:42:22 +0000
commit986c535fca52ce0793ce7848c506b49f95126120 (patch)
tree3c763df22c93908c5d9004c018a5986d06f35646 /src/examples/excelExpr.py
parentfb611ffab05e3fc1d4d3b7dbe2d4905c0d63fcf4 (diff)
downloadpyparsing-git-986c535fca52ce0793ce7848c506b49f95126120.tar.gz
Cleanup examples, fix typo in fourFn.py, convert sample tests to use runTests
Diffstat (limited to 'src/examples/excelExpr.py')
-rw-r--r--src/examples/excelExpr.py32
1 files changed, 14 insertions, 18 deletions
diff --git a/src/examples/excelExpr.py b/src/examples/excelExpr.py
index 0d0c06a..39af365 100644
--- a/src/examples/excelExpr.py
+++ b/src/examples/excelExpr.py
@@ -7,7 +7,8 @@
from pyparsing import (CaselessKeyword, Suppress, Word, alphas,
alphanums, nums, Optional, Group, oneOf, Forward, Regex,
operatorPrecedence, opAssoc, dblQuotedString, delimitedList,
- Combine, Literal, QuotedString)
+ Combine, Literal, QuotedString, ParserElement)
+ParserElement.enablePackrat()
EQ,EXCL,LPAR,RPAR,COLON,COMMA = map(Suppress, '=!():,')
EXCL, DOLLAR = map(Literal,"!$")
@@ -28,10 +29,10 @@ condExpr = expr + COMPARISON_OP + expr
ifFunc = (CaselessKeyword("if") +
LPAR +
Group(condExpr)("condition") +
- COMMA + expr("if_true") +
- COMMA + expr("if_false") + RPAR)
+ COMMA + Group(expr)("if_true") +
+ COMMA + Group(expr)("if_false") + RPAR)
-statFunc = lambda name : CaselessKeyword(name) + LPAR + delimitedList(expr) + RPAR
+statFunc = lambda name : Group(CaselessKeyword(name) + Group(LPAR + delimitedList(expr) + RPAR))
sumFunc = statFunc("sum")
minFunc = statFunc("min")
maxFunc = statFunc("max")
@@ -53,20 +54,15 @@ textExpr = operatorPrecedence(textOperand,
[
('&', 2, opAssoc.LEFT),
])
-expr << (arithExpr | textExpr)
+expr << (arithExpr | textExpr)
-test1 = "=3*A7+5"
-test2 = "=3*Sheet1!$A$7+5"
-test2a ="=3*'Sheet 1'!$A$7+5"
-test2b ="=3*'O''Reilly''s sheet'!$A$7+5"
-test3 = "=if(Sum(A1:A25)>42,Min(B1:B25), " \
- "if(Sum(C1:C25)>3.14, (Min(C1:C25)+3)*18,Max(B1:B25)))"
-test3a = "=sum(a1:a25,10,min(b1,c2,d3))"
-import pprint
-tests = [locals()[t] for t in list(locals().keys()) if t.startswith("test")]
-for test in tests:
- print(test)
- pprint.pprint( (EQ + expr).parseString(test,parseAll=True).asList() )
- print()
+(EQ + expr).runTests("""\
+ =3*A7+5"
+ =3*Sheet1!$A$7+5"
+ =3*'Sheet 1'!$A$7+5"
+ =3*'O''Reilly''s sheet'!$A$7+5"
+ =if(Sum(A1:A25)>42,Min(B1:B25),if(Sum(C1:C25)>3.14, (Min(C1:C25)+3)*18,Max(B1:B25)))"
+ =sum(a1:a25,10,min(b1,c2,d3))
+""") \ No newline at end of file