summaryrefslogtreecommitdiff
path: root/examples/simpleBool.py
diff options
context:
space:
mode:
authorptmcg <ptmcg@austin.rr.com>2021-09-27 13:49:57 -0500
committerptmcg <ptmcg@austin.rr.com>2021-09-27 13:49:57 -0500
commit4ce63a9d8ef8c65af0760a56a09bc091a6354cb1 (patch)
tree83cd93ba6b0f3fcf918b3a65fc3fecb5cb3fddc6 /examples/simpleBool.py
parent4e306edc905c8596436220f72dd2ce04b274bb29 (diff)
downloadpyparsing-git-4ce63a9d8ef8c65af0760a56a09bc091a6354cb1.tar.gz
Fix type annotation for ranges in unicode_sets; make _get_chars_for_ranges a lazyclassproperty
Diffstat (limited to 'examples/simpleBool.py')
-rw-r--r--examples/simpleBool.py74
1 files changed, 43 insertions, 31 deletions
diff --git a/examples/simpleBool.py b/examples/simpleBool.py
index ac75187..ad8e658 100644
--- a/examples/simpleBool.py
+++ b/examples/simpleBool.py
@@ -11,11 +11,15 @@
#
# Copyright 2006, by Paul McGuire
# Updated 2013-Sep-14 - improved Python 2/3 cross-compatibility
+# Updated 2021-Sep-27 - removed Py2 compat; added type annotations
#
+from typing import Sequence, Callable, Union
+
from pyparsing import infixNotation, opAssoc, Keyword, Word, alphas, ParserElement
ParserElement.enablePackrat()
+
# define classes to be built at parse time, as each matching
# expression type is parsed
class BoolOperand:
@@ -23,55 +27,63 @@ class BoolOperand:
self.label = t[0]
self.value = eval(t[0])
- def __bool__(self):
+ def __bool__(self) -> bool:
return self.value
- def __str__(self):
+ def __str__(self) -> str:
return self.label
__repr__ = __str__
-class BoolBinOp:
+class BoolNot:
def __init__(self, t):
- self.args = t[0][0::2]
+ self.arg = t[0][1]
- def __str__(self):
- sep = " %s " % self.reprsymbol
- return "(" + sep.join(map(str, self.args)) + ")"
+ def __bool__(self) -> bool:
+ v = bool(self.arg)
+ return not v
- def __bool__(self):
- return self.evalop(bool(a) for a in self.args)
+ def __str__(self) -> str:
+ return "~" + str(self.arg)
- __nonzero__ = __bool__
+ __repr__ = __str__
-class BoolAnd(BoolBinOp):
- reprsymbol = "&"
- evalop = all
+class BoolBinOp:
+ repr_symbol: str = ""
+ eval_fn: Callable[
+ [Sequence[Union["BoolBinOp", BoolOperand]]], bool
+ ] = lambda _: False
+ def __init__(self, t):
+ self.args = t[0][0::2]
-class BoolOr(BoolBinOp):
- reprsymbol = "|"
- evalop = any
+ def __str__(self) -> str:
+ sep = " %s " % self.repr_symbol
+ return "(" + sep.join(map(str, self.args)) + ")"
+ def __bool__(self) -> bool:
+ return self.eval_fn(bool(a) for a in self.args)
-class BoolNot:
- def __init__(self, t):
- self.arg = t[0][1]
- def __bool__(self):
- v = bool(self.arg)
- return not v
+class BoolAnd(BoolBinOp):
+ repr_symbol = "&"
+ eval_fn = all
- def __str__(self):
- return "~" + str(self.arg)
- __repr__ = __str__
+class BoolOr(BoolBinOp):
+ repr_symbol = "|"
+ eval_fn = any
+# define keywords and simple infix notation grammar for boolean
+# expressions
TRUE = Keyword("True")
FALSE = Keyword("False")
+NOT = Keyword("not")
+AND = Keyword("and")
+OR = Keyword("or")
boolOperand = TRUE | FALSE | Word(alphas, max=1)
boolOperand.setParseAction(BoolOperand).setName("bool_operand")
@@ -80,9 +92,9 @@ boolOperand.setParseAction(BoolOperand).setName("bool_operand")
boolExpr = infixNotation(
boolOperand,
[
- ("not", 1, opAssoc.RIGHT, BoolNot),
- ("and", 2, opAssoc.LEFT, BoolAnd),
- ("or", 2, opAssoc.LEFT, BoolOr),
+ (NOT, 1, opAssoc.RIGHT, BoolNot),
+ (AND, 2, opAssoc.LEFT, BoolAnd),
+ (OR, 2, opAssoc.LEFT, BoolOr),
],
).setName("boolean_expression")
@@ -110,7 +122,7 @@ if __name__ == "__main__":
print("q =", q)
print("r =", r)
print()
- for t, expected in tests:
- res = boolExpr.parseString(t)[0]
+ for test_string, expected in tests:
+ res = boolExpr.parseString(test_string)[0]
success = "PASS" if bool(res) == expected else "FAIL"
- print(t, "\n", res, "=", bool(res), "\n", success, "\n")
+ print(test_string, "\n", res, "=", bool(res), "\n", success, "\n")