mercredi 26 août 2015

User definable object filters

I have many large collections of objects that need to be filtered. I need the filters to be flexible and user definable.

Hard coded, it might look something like:

selected = set()

for o in objects:
    if o.someproperty == 'ab':
        selected.add(o)

    if o.a == 'a' and 'something' in o.b:
        selected.add(o)

But I need something I can store in the db.

I've seen something referring to this is the Criteria (or Filter) pattern http://ift.tt/1a3MKTK but I can't find much information on it.

I'd like the rules to be flexible and serializable in a simple format.

Maybe the above could look something like:

someproperty == 'ab'
a == 'a', 'something' in b

With each line of the rule being a different set of things that need to meet. If any line in the ruleset matches then the object is included. But should the boolean logic be the other way around (with and between the lines and or within them)? Does this give the flexibility to handle various negations? How would I parse it?

What simple approaches are there to this problem?

Aucun commentaire:

Enregistrer un commentaire