How to implement efficient filtering logic in Python?

filter, logic, pyqt, python, sqlite

Solution

If you already query out all the data, an easy way is to simply use the `filter` function :

def predicate(fruit_type, fruit_color, fruit_size):
    def _predicate(fruit):
        if not fruit_type == 'All' and not fruit_type == fruit[1][0]:
            return False
        if not fruit_color == 'All' and not fruit_color == fruit[1][1]:
            return False
        if not fruit_size == 'All' and not fruit_size == fruit[1][2]:
            return False
        return True
    return _predicate

query_type = 'All'
query_color = 'All'
query_size = 'All'
myfruits = {}
my_filtered_fruit = list(filter(predicate(query_type, query_color, query_size), myfruits.items()))

An other way is to defined an object `Predicate` which has a view (the name of the filter) and the filter function associated :

class Predicate:
    def __init__(self, predicate, view):
        self.predicate = predicate
        self.view = view

# Creation of the predicates :
all_color = Predicate(lambda fruit: True, 'All colors')
red_color = Predicate(lambda fruit: fruit[2] == 'red')
# ...

# Then you have to generate your select form. I don't remember exactly the PyQt4 doc but it's not the harder part.

predicates = getAllSelected() # I guess you know how to get this kind of function

myfruits = {}
my_filtered_fruits = myfruits.items()
for pred in predicates:
    my_filtered_fruit = filter(lambda x: pred(x[1]), my_filtered_fruit)
my_filtered_fruit = list(my_filtered_fruit)

Problem

I am trying to create a program that stores - Fruit Name - Fruit Type - Fruit Color - Fruit Size and show them back to the user upon request. The user will be given pre-defined choices to select from. Something like this: My database table will be like this: Now, I am trying to implement a filter function that lets the user select - Fruit Type - Fruit Color - Fruit Size And it will give out all the `Fruit Names` that have the above properties. But now, I have an additional option, "All". Assuming that I have already queried out the data for all the fruits and stored them in dictionary like this: ``` myfruits = { 'apple':('fleshy','red','medium'), 'orange':('fleshy','orange','medium'), 'peanut':('dry','red','small'),...} ``` How do I get the list of fruit names that has the three properties that the user selected? (For example, if the user selected 'fleshy' type , 'All' color, 'All' size -- it should return `['apple','orange']`.) I have thought of using `if` statement, but as the number of properties grow, I would have to write so many lines of `if` and `else` which I don't think is feasible. I am using Python 2.7 with PyQt 4 and the SQLite 3 database on Windows XP SP3 32-bit.

Original source