title: Easier way to check conditions?
body: <p>I'm new to python and I need to simplify this checker. How can I change:</p>
  
  <pre><code>...
  if c == '&gt;' and ( prevTime &gt; currentTime ):
      c2 = True
  elif c == '&gt;=' and ( prevTime &gt;= currentTime ):
      c2 = True
  ...
  </code></pre>
  
  <p>to something like:</p>
  
  <pre><code> if  prevTime | condition |  currentTime:
      doSomething()
  </code></pre>
  
  <p>I've tried to use either <strong>evaluate</strong> or <strong>compile</strong>, but during creation of string there is conversion between datetime object to string ( <strong>str</strong> on datetime object). For example:</p>
  
  <pre><code>&gt;&gt;&gt; 'result = %s %s %s' % (datetime.now(), '&gt;', datetime.utcfromtimestamp(41))
  'result = 2011-04-07 14:13:34.819317 &gt; 1970-01-01 00:00:41'
  </code></pre>
  
  <p>and it can't be compared.</p>
  
  <p>Can someone help me with this? Below working example:</p>
  
  <pre><code>def checkEvent( prevEvent, currentEvent, prevTime, currentTime ):
  
      def checkCondition( condition ):
  
          #condition format
          #tuple ( (oldEvent, newEvent), time, ip)
          # eg: (('co', 'co'), '&gt;=', '!=')
  
          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 == '&gt;' and ( prevTime &gt; currentTime ):
                  c2 = True
              elif c == '&gt;=' and ( prevTime &gt;= currentTime ):
                  c2 = True
              elif c == '&lt;' and ( prevTime &lt; currentTime ):
                  c2 = True
              elif c == '&lt;=' and ( prevTime &lt;= 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' ), '&lt;'  ),  ( add, changeState ) ),
                      ( ( ( 'co', 'co' ), '&gt;=' ),  ( add, changeState, finish ) ),
                      ( ( ( 'co', 'co' ), '&gt;=' ),  ( update, ) ),
                      ( ( ( 'co', 're' ), '&gt;=' ),  ( changeState, finish ) ),
                      ( ( ( 'co', 'ex' ), '&gt;=' ),  ( 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) )
  </code></pre>