-
Notifications
You must be signed in to change notification settings - Fork 47
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allowing logical expression on condition statements
- Loading branch information
Showing
3 changed files
with
83 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -36,3 +36,4 @@ tqdm==4.32.1 | |
twine==1.13.0 | ||
urllib3==1.25.3 | ||
webencodings==0.5.1 | ||
pyparsing==2.4.7 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
from pyparsing import Keyword, Word, alphanums | ||
|
||
|
||
class ConditionOperand(object): | ||
check_condition_fn = None | ||
|
||
def __init__(self, t): | ||
self.label = t[0] | ||
|
||
assert self.check_condition_fn is not None, 'The check_condition_fn should should be set' | ||
assert callable(self.check_condition_fn), 'The check_condition_fn should should be callable' | ||
|
||
self.value = self.check_condition_fn(t[0]) | ||
|
||
def __bool__(self): | ||
return self.value | ||
|
||
def __str__(self): | ||
return self.label | ||
|
||
__repr__ = __str__ | ||
__nonzero__ = __bool__ | ||
|
||
|
||
class BoolBinOp(object): | ||
def __init__(self, t): | ||
self.args = t[0][0::2] | ||
|
||
def __str__(self): | ||
sep = " %s " % self.reprsymbol | ||
return "(" + sep.join(map(str, self.args)) + ")" | ||
|
||
def __bool__(self): | ||
return self.evalop(bool(a) for a in self.args) | ||
|
||
__nonzero__ = __bool__ | ||
__repr__ = __str__ | ||
|
||
|
||
class BoolAnd(BoolBinOp): | ||
reprsymbol = '&' | ||
evalop = all | ||
|
||
|
||
class BoolOr(BoolBinOp): | ||
reprsymbol = '|' | ||
evalop = any | ||
|
||
|
||
class BoolNot(object): | ||
def __init__(self, t): | ||
self.arg = t[0][1] | ||
|
||
def __bool__(self): | ||
v = bool(self.arg) | ||
return not v | ||
|
||
def __str__(self): | ||
return "~" + str(self.arg) | ||
|
||
__repr__ = __str__ | ||
__nonzero__ = __bool__ | ||
|
||
|
||
TRUE = Keyword("True") | ||
FALSE = Keyword("False") | ||
boolOperand = TRUE | FALSE | Word(alphanums + '_:.', max=256) |