-
Notifications
You must be signed in to change notification settings - Fork 34
being able to apply expressions to boolean values #73
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
Comments
To add some more context, the new test i wrote are:
|
@gemerden Thanks!
So what you are doing is evaluate a boolean expression by passing custom True/False values for Symbols?
I am not sure why it would complicate subclassing?
Sure. Do you mind crafting a PR with your additions? |
All the credits go to @bastikr ! Now what I like about your approach is that is uses the Python evaluation in a clever way and it looks like it avoids the pitfalls of mixing boolean.py's TRUE and FALSE with Python's True and False, correct? |
|
I have a branch ready for a PR, but i am pretty sure i need to be a collaborator to create a branch/push/PR. |
@gemerden no need to be a collaborator: you just fork push a branch in your forked repo and then submit a PR on that branch |
If anyone is interested in a use case for evaluations, I used a somewhat customized version of # '|' is the OR operator
assert WildPath("start_time|end_time").get_in(agenda) == {"start_time": "10:00", "end_time": "11:00"}
# '&' is the AND operator
assert WildPath("start_*&*_time").get_in(agenda) == {"start_time": "10:00"}
# '!' is the NOT operator:
assert WildPath("!item?").get_in({"item1": "chair", "item2": "table", "count": 2}) == {"count": 2}
# parentheses can be used to indicate precedence:
assert WildPath("!(a|b)") != WildPath("!a|b")
|
I'm not quite sure if this is what you're after, but you can evaluate the expressions by 1) providing your own subclass of
Side note, I think the |
@jnikula as a simplification, we agreed that we should not be able to evaluate a boolean expression as a Python expression (hence why So your approach makes sense alright: in subclassing Symbol you can provide exactly how you would like this evaluation to take place from a Python point of view if you need such thing. You can also of course subclass or tweak |
If anyone needs this kind of functionality, be warned that @jnikula's suggestion fails for certain expressions. If you negate a symbol (ie: 'foobar and ~baz'), then NOT.demorgan() fails during algrebra.parse.simplify():
@gemerden's solution worked very well for me. |
@JonGalarneau I'll have to take your word for it. I ended up using Lark to implement a query language for my needs. |
Maybe i missed something completely, but i could not figure out how to actually use the boolean expressions to boolean values to calculate the result, so i implemented something myself:
I have written basic tests and checked whether the original tests pass (with some basic modifications) by replacing BooleanAlgebra with CallableBooleanAlgebra (
import CallableBooleanAlgebra as BooleanAlgebra
).Compliments for the tight implementation of boolean that made this relatively easy.
So my questions are:
Also comments, improvements, etc. are very welcome!
Cheers, Lars
The text was updated successfully, but these errors were encountered: