简体   繁体   中英

Checking a list against different criteria rules

Given a list of items, I want to create a function to check if it matches a certain criteria and return true or false.

Here is an example of the criteria and expected output:

    #the to check against with criteria[1]
    langList = ['spanish','english','russian','persian']

    criteria = ['any'] #matches any language
    result: match

    #langList does not contain one or all
    criteria = ['any-but-or',[english,chinese]] 
    result: no match

    #langList does not contain all
    criteria = ['any-but-and',[english,chinese]] 
    result: match

    #langList contains one or all
    criteria = ['any-with-or',[english,chinese]] 
    result: match

    #langList contains all
    criteria = ['any-with-and',[english,chinese]] 
    result: no match

    #langList contains one or all and no others
    criteria = ['only-with-or',[english,chinese]] 
    result: no match

    #langList contains all and no others
    criteria = ['only-with-and',[english,chinese]] 
    result: no match

What's the best way to do this? My idea is something like below but I'm haven't mastered list comprehension yet which I think is key here.

    def check_criteria_match(criteria, languageList):

        rule = criteria[0]
        criteriaLanguages = criteria[1]
        match = True

        if rule != 'any':
            continue
        elif rule = 'any-but-or' #check languageList:
            match = False; break

        #[...]

        return match

EDIT: Based on Praveen Gollakota's answer , here is the final function, isn't she pretty :)

def check_rule(rule, o, p):
    # o = original, p = pattern
    if rule == 'any':
        return True
    elif rule == 'any-but-or':
        return not bool(set(p)-set(o))
    elif rule == 'any-but-and':
        return len(set(p)-set(o)) != 0
    elif rule == 'any-with-or':
        return len(set(p)-set(o)) <= 1
    elif rule == 'any-with-and':
        return len(set(p)-set(o)) == 0
    elif rule == 'only-with-or':
        return len(set(o)-set(p)) <= 1
    elif rule == 'only-with-and':
        return len(set(o)-set(p)) == 0

You want to do membership testing, for which sets are the appropriate data structures and set operations work well.

You can use sets and lambda functions to build a rule based dispatcher like this.

>>> rule_checker = {'any': lambda o, p: True, 
...                 'any-but-or': lambda o, p: not bool(set(p) - set(o)),
...                 'any-but-and': lambda o, p: len(set(p) - set(o)) != 0,}

>>> lang_list = ['spanish','english','russian','persian']
>>> rule_checker['any-but-and'](lang_list, ['english','chinese'])
True
>>> rule_checker['any-but-or'](lang_list, ['english','chinese'])
False

Of course, you need to add the rest of the rules in there.

EDIT: You can also write the rules inside a function like this.

>>> def check_rule(rule, o, p):
...     # o = original, p = pattern
...     if rule == 'any':
...         return True
...     elif rule == 'any-but-or':
...         return not bool(set(p)-set(o))
...     elif rule == 'any-but-and':
...         return len(set(p)-set(o)) != 0
... 
>>> check_rule('any-but-and', lang_list, ['english','chinese'])
True

You could use a library like asq :

asq is a simple implementation of a LINQ -inspired API for Python which operates over Python iterables, including a parallel version implemented in terms of the Python standard library multiprocessing module. The API sports feature equivalence with LINQ for objects, 100% statement test coverage and comprehensive documentation .

Examples

>>> from asq.initiators import query
>>> langList = ['spanish', 'english', 'russian', 'persian']

# langlist contains 'russian'
>>> query(langList).contains('russian')
True

# langlist contains 'english' or 'italian'
>>> query(langList).any(lambda x : x == 'english' or x == 'italian')
True

# langlist contains 'english' and 'russian'
>>> not query(('english', 'russian')).difference(langList).any()
True

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM