def apply_filters(records, filters):
 operators = {COMPARISON.LT: operator.lt, COMPARISON.MAX: operator.le, COMPARISON.EQ: operator.eq, COMPARISON.NOT: operator.ne, COMPARISON.MIN: operator.ge, COMPARISON.GT: operator.gt, COMPARISON.IN: operator.contains, COMPARISON.EXCLUDE: (lambda x, y: (not operator.contains(x, y))), COMPARISON.LIKE: (lambda x, y: re.search(y, x, re.IGNORECASE))}
 for record in records:
  matches = True
  for f in filters:
   right = f.value
   if (f.field == DEFAULT_ID_FIELD):
    if isinstance(right, int):
     right = str(right)
   left = record
   subfields = f.field.split('.')
   for subfield in subfields:
    if (not isinstance(left, dict)):
     break
    left = left.get(subfield)
   if (f.operator in (COMPARISON.IN, COMPARISON.EXCLUDE)):
    (right, left) = (left, right)
   elif (left is None):
    right_is_number = (isinstance(right, (int, float)) and (right not in (True, False)))
    if right_is_number:
     matches = False
     continue
    else:
     left = ''
   matches = (matches and operators[f.operator](left, right))
  if matches:
   (yield record)

def apply_filters ( records , filters ) : operators = { COMPARISON . LT : operator . lt , COMPARISON . MAX : operator . le , COMPARISON . EQ : operator . eq , COMPARISON . NOT : operator . ne , COMPARISON . MIN : operator . ge , COMPARISON . GT : operator . gt , COMPARISON . IN : operator . contains , COMPARISON . EXCLUDE : ( lambda x , y : ( not operator . contains ( x , y ) ) ) , COMPARISON . LIKE : ( lambda x , y : re . search ( y , x , re . IGNORECASE ) ) } for record in records : matches = True for f in filters : right = f . value if ( f . field == DEFAULT_ID_FIELD ) : if isinstance ( right , int ) : right = str ( right ) left = record subfields = f . field . split ( '.' ) for subfield in subfields : if ( not isinstance ( left , dict ) ) : break left = left . get ( subfield ) if ( f . operator in ( COMPARISON . IN , COMPARISON . EXCLUDE ) ) : ( right , left ) = ( left , right ) elif ( left is None ) : right_is_number = ( isinstance ( right , ( int , float ) ) and ( right not in ( True , False ) ) ) if right_is_number : matches = False continue else : left = '' matches = ( matches and operators [ f . operator ] ( left , right ) ) if matches : ( yield record )
use this method to apply an iterable of filters to a stream .