New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Rule chain for if A: return B #113
Comments
Make sure “test_A”, “test_B”, etc. are predicates themselves and it should work. |
Both version works but as I said I prefer the second cleaner version but it is less "efficient" (which perhaps does not matter) because it evaluate all three conditions. I was asking if there is a trick for
returning |
Is
|
Yes, it is. |
Sorry, the demo for the my function version was not written correctly. I am updating it. |
Can you reduce this down to a minimal example (that I can just copy/paste and run) that reproduces this please? |
with output
Note that I know beforehand that |
Thanks, I see now. Seems I misread this bit in your message:
Well, that is expected behaviour, you're not missing anything. Predicates combine and behave similar to regular boolean algebra:
The two forms (ie. "function" and "chain" form) are not equivalent. To achieve the behaviour you describe, you should use the first one. |
Thanks for your clarification, at least I did not miss any trick from the documentation. |
To clarify somewhat, the "chain" form is as if you wrote the "function" form like so: @rules.predicate
def can_do_something(user, target):
if user.is_anonymous and test_A(user, target):
return True
elif user.is_admin and test_B(user, target):
return True
else:
return test_C(user, target) |
I have some rules like
which I would like to translate to
The problem is that in the original implementation if
user.is_anonymous
isTrue
, the checker will return immediately withtest_A
, but in the new implementation the checker will go through all cases ifis_anonymous & test_A
isFalse
. How do I avoid evaluating all three cases? It looks like I am missing something obvious here.The text was updated successfully, but these errors were encountered: