Skip to content

Commit b45ec17

Browse files
committed
allow constant truth values in boolean conditions
1 parent e3ecb6e commit b45ec17

File tree

2 files changed

+24
-1
lines changed

2 files changed

+24
-1
lines changed

pyvdrm/asi2.py

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,18 @@ def __call__(self, mutations):
8383
return Score(not child_score.score, child_score.residues)
8484

8585

86+
class BoolTrue(AsiExpr):
87+
"""Boolean True constant"""
88+
def __call__(self, *args):
89+
return Score(True, [])
90+
91+
92+
class BoolFalse(AsiExpr):
93+
"""Boolean False constant"""
94+
def __call__(self, *args):
95+
return Score(False, [])
96+
97+
8698
class AndExpr(AsiExpr):
8799
"""Fold boolean AND on children"""
88100

@@ -279,8 +291,11 @@ def parser(self, rule):
279291
selectstatement = select + select_quantifier + from_ + residue_list
280292
selectstatement.setParseAction(SelectFrom)
281293

294+
bool_ = Literal('TRUE').suppress().setParseAction(BoolTrue) |\
295+
Literal('FALSE').suppress().setParseAction(BoolFalse)
296+
282297
booleancondition = Forward()
283-
condition = residue | excludestatement | selectstatement
298+
condition = residue | excludestatement | selectstatement | bool_
284299

285300
booleancondition << infixNotation(condition,
286301
[(and_, 2, opAssoc.LEFT, AndExpr),

pyvdrm/tests/test_asi2.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ def test_bool_and(self):
8181
self.assertEqual(rule(VariantCalls("7Y 1G 2T")), True)
8282
self.assertEqual(rule([]), False)
8383

84+
def test_bool_constants(self):
85+
rule = ASI2("TRUE OR 1G")
86+
self.assertEqual(rule(VariantCalls("2G")), True)
87+
rule = ASI2("FALSE AND 1G")
88+
self.assertEqual(rule(VariantCalls("1G")), False)
89+
rule = ASI2("TRUE OR (FALSE AND TRUE)")
90+
self.assertEqual(rule(VariantCalls("1G")), True)
91+
8492
def test_bool_or(self):
8593
rule = ASI2("1G OR (2T OR 7Y)")
8694
self.assertTrue(rule(VariantCalls("2T")))

0 commit comments

Comments
 (0)