summaryrefslogtreecommitdiff
path: root/examples/excelExpr.py
diff options
context:
space:
mode:
authorJon Dufresne <jon.dufresne@gmail.com>2019-10-31 21:10:28 -0700
committerPaul McGuire <ptmcg@users.noreply.github.com>2019-10-31 23:10:28 -0500
commit53d1b4a6f48a53c4c4ec4ac7031362b691c0366d (patch)
tree088ad3cf3561b78a00af4fb2fd474f4a2b8ca70c /examples/excelExpr.py
parent41752aa52cc97c710474bb2972cceab057b52ad4 (diff)
downloadpyparsing-git-53d1b4a6f48a53c4c4ec4ac7031362b691c0366d.tar.gz
Blacken the project (#141)
Diffstat (limited to 'examples/excelExpr.py')
-rw-r--r--examples/excelExpr.py85
1 files changed, 55 insertions, 30 deletions
diff --git a/examples/excelExpr.py b/examples/excelExpr.py
index 86237ef..cea2eea 100644
--- a/examples/excelExpr.py
+++ b/examples/excelExpr.py
@@ -4,36 +4,64 @@
#
# A partial implementation of a parser of Excel formula expressions.
#
-from pyparsing import (CaselessKeyword, Suppress, Word, alphas,
- alphanums, nums, Optional, Group, oneOf, Forward,
- infixNotation, opAssoc, dblQuotedString, delimitedList,
- Combine, Literal, QuotedString, ParserElement, pyparsing_common as ppc)
+from pyparsing import (
+ CaselessKeyword,
+ Suppress,
+ Word,
+ alphas,
+ alphanums,
+ nums,
+ Optional,
+ Group,
+ oneOf,
+ Forward,
+ infixNotation,
+ opAssoc,
+ dblQuotedString,
+ delimitedList,
+ Combine,
+ Literal,
+ QuotedString,
+ ParserElement,
+ pyparsing_common as ppc,
+)
+
ParserElement.enablePackrat()
-EQ,LPAR,RPAR,COLON,COMMA = map(Suppress, '=():,')
-EXCL, DOLLAR = map(Literal,"!$")
-sheetRef = Word(alphas, alphanums) | QuotedString("'",escQuote="''")
-colRef = Optional(DOLLAR) + Word(alphas,max=2)
+EQ, LPAR, RPAR, COLON, COMMA = map(Suppress, "=():,")
+EXCL, DOLLAR = map(Literal, "!$")
+sheetRef = Word(alphas, alphanums) | QuotedString("'", escQuote="''")
+colRef = Optional(DOLLAR) + Word(alphas, max=2)
rowRef = Optional(DOLLAR) + Word(nums)
-cellRef = Combine(Group(Optional(sheetRef + EXCL)("sheet") + colRef("col") +
- rowRef("row")))
+cellRef = Combine(
+ Group(Optional(sheetRef + EXCL)("sheet") + colRef("col") + rowRef("row"))
+)
-cellRange = (Group(cellRef("start") + COLON + cellRef("end"))("range")
- | cellRef | Word(alphas,alphanums))
+cellRange = (
+ Group(cellRef("start") + COLON + cellRef("end"))("range")
+ | cellRef
+ | Word(alphas, alphanums)
+)
expr = Forward()
COMPARISON_OP = oneOf("< = > >= <= != <>")
condExpr = expr + COMPARISON_OP + expr
-ifFunc = (CaselessKeyword("if")
- - LPAR
- + Group(condExpr)("condition")
- + COMMA + Group(expr)("if_true")
- + COMMA + Group(expr)("if_false")
- + RPAR)
+ifFunc = (
+ CaselessKeyword("if")
+ - LPAR
+ + Group(condExpr)("condition")
+ + COMMA
+ + Group(expr)("if_true")
+ + COMMA
+ + Group(expr)("if_false")
+ + RPAR
+)
-statFunc = lambda name : Group(CaselessKeyword(name) + Group(LPAR + delimitedList(expr) + RPAR))
+statFunc = lambda name: Group(
+ CaselessKeyword(name) + Group(LPAR + delimitedList(expr) + RPAR)
+)
sumFunc = statFunc("sum")
minFunc = statFunc("min")
maxFunc = statFunc("max")
@@ -44,22 +72,18 @@ multOp = oneOf("* /")
addOp = oneOf("+ -")
numericLiteral = ppc.number
operand = numericLiteral | funcCall | cellRange | cellRef
-arithExpr = infixNotation(operand,
- [
- (multOp, 2, opAssoc.LEFT),
- (addOp, 2, opAssoc.LEFT),
- ])
+arithExpr = infixNotation(
+ operand, [(multOp, 2, opAssoc.LEFT), (addOp, 2, opAssoc.LEFT),]
+)
textOperand = dblQuotedString | cellRef
-textExpr = infixNotation(textOperand,
- [
- ('&', 2, opAssoc.LEFT),
- ])
+textExpr = infixNotation(textOperand, [("&", 2, opAssoc.LEFT),])
expr << (arithExpr | textExpr)
-(EQ + expr).runTests("""\
+(EQ + expr).runTests(
+ """\
=3*A7+5
=3*Sheet1!$A$7+5
=3*'Sheet 1'!$A$7+5"
@@ -67,4 +91,5 @@ expr << (arithExpr | textExpr)
=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))
=if("T"&a2="TTime", "Ready", "Not ready")
-""")
+"""
+)