Fixed filter composition with or. Now takes account of operator precedence

This commit is contained in:
Juan Pablo Amoroso
2019-05-30 12:27:36 -03:00
parent 16436dc124
commit a3b49bfceb
2 changed files with 18 additions and 10 deletions
+5 -7
View File
@@ -76,17 +76,15 @@ class Filter:
def __init__(self, query):
self.query = query
def _compose_filter(self, operator, other):
def __and__(self, other):
assert isinstance(other, Filter)
new_query = "({}) {} ({})".format(self.query, operator, other.query)
new_query = "({}) & ({})".format(self.query, other.query)
return Filter(query=new_query)
def __and__(self, other):
return self._compose_filter("&", other)
def __or__(self, other):
return self._compose_filter("|", other)
assert isinstance(other, Filter)
new_query = "(({}) | ({}))".format(self.query, other.query)
return Filter(query=new_query)
def __invert__(self):
return Filter("!({})".format(self.query))
+13 -3
View File
@@ -43,7 +43,17 @@ def test_compose_filters_with_and():
def test_compose_filters_with_or():
"""Test composition of two filters with or"""
strike_field = Field("strike", "strike")
ft1 = strike_field >= 100
ft2 = strike_field < 200
ft1 = strike_field >= 200
ft2 = strike_field < 100
composed = ft1 | ft2
assert composed.query == "(strike >= 100) | (strike < 200)"
assert composed.query == "((strike >= 200) | (strike < 100))"
def test_compose_many_filters():
symbol_field = Field("underlying", "underlying")
strike_field = Field("strike", "strike")
ft1 = symbol_field == "SPX"
ft2 = strike_field >= 200
ft3 = strike_field < 100
composed = ft1 & (ft2 | ft3)
assert composed.query == "(underlying == 'SPX') & (((strike >= 200) | (strike < 100)))"