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
455 stars 54 forks source link

Question about set intersections #86

Closed omardipbigdata closed 6 months ago

omardipbigdata commented 7 months ago

Hi,

I would like to extent my gratitude for such a nice library. Found it recently. In my use case i am trying to compare whether a set "a" is a subset of "b" or not. when i am using below, it always returns true where else the regular python works fine with those operators.

program: import rule_engine context = rule_engine.Context(default_value=None,resolver=rule_engine.resolve_attribute) b=set(['Luke', 'Darth']) rr=['first_name <= b'] rule = rule_engine.Rule(rr[0], context) a=set(['ocean']) res1=rule.matches({'first_name': a}) print(res1) print(a<=b)

result: True False

Process finished with exit code 0

zeroSteiner commented 7 months ago

Try again but remove the resolver keyword argument to the context. You don't want to use the attribute resolver in this case.

omardipbigdata commented 7 months ago

that will raise datatype mismatch import rule_engine context = rule_engine.Context(default_value=None) b=set(['Luke', 'Darth']) rr=['first_name <= b'] rule = rule_engine.Rule(rr[0], context) a=set(['ocean']) res1=rule.matches({'first_name': a}) print(res1) print(a<=b)

Traceback (most recent call last): File "/Users/omarchowdhury/omarAddPlatform/git/apdp-cli/rule_engine_first_program_3.py", line 7, in res1=rule.matches({'first_name': a}) File "/Users/omarchowdhury/omarAddPlatform/git/apdp-cli/.venv/lib/python3.9/site-packages/rule_engine/engine.py", line 602, in matches return bool(self.evaluate(thing)) File "/Users/omarchowdhury/omarAddPlatform/git/apdp-cli/.venv/lib/python3.9/site-packages/rule_engine/engine.py", line 591, in evaluate return self.statement.evaluate(thing) File "/Users/omarchowdhury/omarAddPlatform/git/apdp-cli/.venv/lib/python3.9/site-packages/rule_engine/ast.py", line 1121, in evaluate return self.expression.evaluate(thing) File "/Users/omarchowdhury/omarAddPlatform/git/apdp-cli/.venv/lib/python3.9/site-packages/rule_engine/ast.py", line 379, in evaluate return self._evaluator(thing) File "/Users/omarchowdhury/omarAddPlatform/git/apdp-cli/.venv/lib/python3.9/site-packages/rule_engine/ast.py", line 584, in op_arithmetic return self.__op_arithmetic_values(op, left_value, right_value) File "/Users/omarchowdhury/omarAddPlatform/git/apdp-cli/.venv/lib/python3.9/site-packages/rule_engine/ast.py", line 600, in op_arithmetic_values raise errors.EvaluationError('data type mismatch') rule_engine.errors.EvaluationError: data type mismatch

zeroSteiner commented 7 months ago

That sounds right. I don't think you can use the less than operator to find the intersection of sets. For that you need to use the bitwise and operator (&).

Give that a shot now.

omardipbigdata commented 7 months ago

thanks for the clarification. if you can share few example that would be really helpful

zeroSteiner commented 6 months ago

The syntax is a & b. The resulting set will have the members that are in both a and b.