zeroSteiner / rule-engine

A lightweight, optionally typed expression language with a custom grammar for matching arbitrary Python objects.
https://zerosteiner.github.io/rule-engine/
BSD 3-Clause "New" or "Revised" License
445 stars 54 forks source link

RuleSyntaxError #2

Closed matias-eb closed 4 years ago

matias-eb commented 4 years ago

I am trying to do something like rule_engine.Rule('number in (1, 2)') and I am getting the following error: RuleSyntaxError: ('syntax error', LexToken(COMMA,',',1,14)) and according to documentation tuples can be used as array structures.

zeroSteiner commented 4 years ago

That sounds like a documentation error then because the error is what I would expect. Python-style tuples using parenthesis are not valid. Array literals need to be defined with brackets. So what you're trying to do would be like:

In [5]: rule_engine.Rule('number in [1, 2]').evaluate(dict(number=1))                                                                                                                                                                                                                                                               
Out[5]: True

In [6]: rule_engine.Rule('number in [1, 2]').evaluate(dict(number=3))                                                                                                                                                                                                                                                               
Out[6]: False
zeroSteiner commented 4 years ago

I couldn't find where in the documentation it specifics that is valid syntax. There's actually no mention of defining array literals which is something I should fix.

You can use both tuples and lists as the value for a valid array. That would look something like:

In [4]: rule_engine.Rule('1 in numbers').evaluate(dict(numbers=(1, 2, 3)))                                                                                                                                                                                                     
Out[4]: True

In [5]: rule_engine.Rule('1 in numbers').evaluate(dict(numbers=[4, 5, 6]))                                                                                                                                                                                                     
Out[5]: False

Maybe that's what you were thinking of.

matias-eb commented 4 years ago

Yes, I would do it in that way. Thank you

NehalDamania commented 1 year ago

Yes we should document the in operator use in the documentation like such rule_engine.Rule('number in [1, 2]').evaluate(dict(number=1))