-
Notifications
You must be signed in to change notification settings - Fork 80
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
Add logical and bitwise operators for math_eval #301
Conversation
Adds the ability to provide additional operators to the math_eval function. Only adds the `and`, `&`, `or`, and `|` operators. This is a feature needed in an upcoming version of YANK
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good!
I wonder if we do want to support also and
and or
because I've noticed now that they are not very intuitive with sets.
>>> a = set([1, 2, 3, 4])
>>> b = set([2, 3, 4, 5])
>>> a and b
{2, 3, 4, 5}
>>> a & b
{2, 3, 4}
>>> b and a
{1, 2, 3, 4}
Sorry if I noticed only now! If we do decide to support them, we definitely need to document this in bold because I believe users will tend to use those instead of &
and |
.
Can you maybe add a couple of test cases in |
They are both supported right now but both are set operations (so bitwise) because this is only suppose to be a set operator, not logical operator. But I can make that even more clear now if you want |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are both supported right now but both are set operations (so bitwise) because this is only suppose to be a set operator, not logical operator.
Oh! Sorry I didn't notice that you assigned ast.BitAnd
to operator.and_
. I understand now.
Feel free to merge! This is not essential at all, but you could also come up with a recursive implementation, instead of unrolling the loop, for consistency with the rest of the function. Something like this may work for example (untested)
elif isinstance(node, ast.BoolOp):
if len(node.values) > 2:
# Left-to-right precedence.
left_value = copy.deepcopy(node)
left_value.values.pop(-1)
else:
left_value = node.values[0]
return operators[type(node.op)](_math_eval(left_value), _math_eval(node.values[-1]))
I liked your solution better, I had tried the recursion but only parsed the values, so it threw an error the first time I tried it. Your's seems to work |
Adds the ability to provide additional operators to the math_eval function. Only adds the
and
,&
,or
, and|
operators.This is a feature needed in an upcoming version of YANK