Martin Sivák has uploaded a new change for review. Change subject: Improve 'and' and 'or' operators to accept unlimited number of arguments ......................................................................
Improve 'and' and 'or' operators to accept unlimited number of arguments It was not possible to use (and True True True) or (or True False True) and this patch makes it work to make it simpler to write complicated if statements. Signed-off-by: Martin Sivak <msi...@redhat.com> Change-Id: Ie594137084b67790ea290615c65df493803411f7 --- M mom/Policy/Parser.py M tests/ParserTests.py 2 files changed, 22 insertions(+), 4 deletions(-) git pull ssh://gerrit.ovirt.org:29418/mom refs/changes/41/42041/1 diff --git a/mom/Policy/Parser.py b/mom/Policy/Parser.py index c14d10b..aace0de 100644 --- a/mom/Policy/Parser.py +++ b/mom/Policy/Parser.py @@ -464,11 +464,17 @@ def c_shr(self, x, y): return x >> y - def c_and(self, x, y): - return x and y + def c_and(self, *args): + for arg in args: + if not arg: + return arg + return args[-1] - def c_or(self, x, y): - return x or y + def c_or(self, *args): + for arg in args: + if arg: + return arg + return args[-1] def c_not(self, x): return not x diff --git a/tests/ParserTests.py b/tests/ParserTests.py index 9dc4ee4..3b6c426 100644 --- a/tests/ParserTests.py +++ b/tests/ParserTests.py @@ -97,6 +97,18 @@ """ self.verify(pol, [ "", 0, 2, 17, "", True, True ]) + def test_extended_logic(self): + pol = """ # Again, these bahave according to Python rules + (and 1 1 "") # "" evaluates to false + (and 0 0 1) # as does 0 -- the first false value is returned + (and 1 1 2) # If all values are true, the last value is returned + (or "" "" 17) # or returns the first true value encountered + (or "" "" "") # if all values are false, or returns the last one + (and 1 2 3 4 5 6 7 8 9 0) + (or 0) + """ + self.verify(pol, [ "", 0, 2, 17, "", 0, 0 ]) + def test_vars(self): pol = """ (defvar foo "bar") -- To view, visit https://gerrit.ovirt.org/42041 To unsubscribe, visit https://gerrit.ovirt.org/settings Gerrit-MessageType: newchange Gerrit-Change-Id: Ie594137084b67790ea290615c65df493803411f7 Gerrit-PatchSet: 1 Gerrit-Project: mom Gerrit-Branch: master Gerrit-Owner: Martin Sivák <msi...@redhat.com> _______________________________________________ Engine-patches mailing list Engine-patches@ovirt.org http://lists.ovirt.org/mailman/listinfo/engine-patches