Easier way to check conditions?
Question: I'm new to python and I need to simplify this checker. How can I change:

    
    
    ...
    if c == '>' and ( prevTime > currentTime ):
        c2 = True
    elif c == '>=' and ( prevTime >= currentTime ):
        c2 = True
    ...
    

to something like:

    
    
     if  prevTime | condition |  currentTime:
        doSomething()
    

I've tried to use either **evaluate** or **compile** , but during creation of
string there is conversion between datetime object to string ( **str** on
datetime object). For example:

    
    
    >>> 'result = %s %s %s' % (datetime.now(), '>', datetime.utcfromtimestamp(41))
    'result = 2011-04-07 14:13:34.819317 > 1970-01-01 00:00:41'
    

and it can't be compared.

Can someone help me with this? Below working example:

    
    
    def checkEvent( prevEvent, currentEvent, prevTime, currentTime ):
    
        def checkCondition( condition ):
    
            #condition format
            #tuple ( (oldEvent, newEvent), time, ip)
            # eg: (('co', 'co'), '>=', '!=')
    
            c1 = c2 = False
    
            #check Event
            if prevEvent == condition[0][0] and currentEvent == condition[0][1]:
                c1 = True
            else:
                return False
    
            #check time
            if condition[1]:
                c = condition[1]
    
                if c == '>' and ( prevTime > currentTime ):
                    c2 = True
                elif c == '>=' and ( prevTime >= currentTime ):
                    c2 = True
                elif c == '<' and ( prevTime < currentTime ):
                    c2 = True
                elif c == '<=' and ( prevTime <= currentTime ):
                    c2 = True
                elif c == '==' and ( prevTime == currentTime ):
                    c2 = True
    
            else:
                c2 = True
    
    
            return c1 and c2
    
    
        def add():
            print 'add'
    
        def changeState():
            print 'changeState'
    
        def finish():
            print 'finish'
    
        def update():
            print 'update'    
    
    
        conditions = (\
                        ( ( ( 're', 'co' ), None ),  ( add, changeState ) ),
                        ( ( ( 'ex', 'co' ), None ),  ( add, changeState ) ),
                        ( ( ( 'co', 'co' ), '<'  ),  ( add, changeState ) ),
                        ( ( ( 'co', 'co' ), '>=' ),  ( add, changeState, finish ) ),
                        ( ( ( 'co', 'co' ), '>=' ),  ( update, ) ),
                        ( ( ( 'co', 're' ), '>=' ),  ( changeState, finish ) ),
                        ( ( ( 'co', 'ex' ), '>=' ),  ( changeState, finish ) ) 
                     )  
    
    
        for condition in conditions:
            if checkCondition( condition[0] ):
                for cmd in condition[1]:
                    cmd()
    
    
    from datetime import datetime
    
    checkEvent( 'co', 'co', datetime.utcfromtimestamp(41), datetime.now() )
    checkEvent( 'ex', 'co', datetime.utcfromtimestamp(41), datetime.now() )
    checkEvent( 'co', 'co', datetime.utcfromtimestamp(41), datetime.utcfromtimestamp(40) )
    


Answer: You can try making a map of operators, like this:

    
    
    import operator
    
    compares = {
        '>': operator.gt,
        '>=': operator.ge,
        '<': operator.lt,
        '<=': operator.le,
        '==': operator.eq
    }
    
    def check(c, prev, current):
        func = compares[c]
        return func(prev, current)
    
    print check('>', 5, 3)  # prints: True
    print check('>=', 5, 5) # prints: True
    print check('<', 3, 5)  # prints: True
    print check('<=', 3, 3) # prints: True
    print check('==', 7, 7) # prints: True
    

