Skip to content
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

Merged
merged 5 commits into from
Oct 13, 2017
Merged

Conversation

Lnaden
Copy link
Contributor

@Lnaden Lnaden commented Oct 12, 2017

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

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
@Lnaden Lnaden requested a review from andrrizzi October 12, 2017 23:12
Copy link
Contributor

@andrrizzi andrrizzi left a 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 |.

@andrrizzi
Copy link
Contributor

Can you maybe add a couple of test cases in test_utils.py:test_math_eval before merging?

@Lnaden
Copy link
Contributor Author

Lnaden commented Oct 13, 2017

I wonder if we do want to support also and and or because

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

Copy link
Contributor

@andrrizzi andrrizzi left a 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]))

@Lnaden
Copy link
Contributor Author

Lnaden commented Oct 13, 2017

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

@Lnaden Lnaden merged commit b1e37d2 into master Oct 13, 2017
@Lnaden Lnaden deleted the math_operators branch October 13, 2017 21:21
@Lnaden Lnaden mentioned this pull request Oct 13, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants