I have a set of filter objects, which inherit the properties of a Filter
base class
class Filter():
def __init__(self):
self.filterList = []
def __add__(self,f):
self.filterList += f.filterList
def match(self, entry):
for f in self.filterList:
if not f(entry):
return False
return True
class thisFilter(Filter):
def __init__(self, args):
super().__init__()
....
def thisFilterFunc(entry)->bool:
return True
self.filterList.append(thisFilterFunc)
This filter classes are used by various functions to filter entries
def myfunc(myfilter, arg1, ...):
...
for entry in entries:
if myfilter.match(entry):
... do something
Multiple filters can be added (logical and) by adding instances of these filters:
bigFilter = filter1 + filter2 + ...
This is all comming together quite well, but I would love to generalize this in a way to handle more complex logical constraints, e.g.
bigFilter = (filter1 and filter2) or (filter3 and not filter4)
It feels like this should somehow be possible with overwriting __bool__
of the class instead of using __add__
but the boolean value of the class is only known for a given entry and not during assemly of the filter.
Any ideas how to make this possible? Or is there maybe a more pythonic way do do this?