<s> '''<STR_LIT>''' <EOL> from __future__ import absolute_import <EOL> from six import string_types <EOL> class OR ( object ) : pass <EOL> class IN ( object ) : pass <EOL> class GT ( object ) : pass <EOL> class LT ( object ) : pass <EOL> class EQ ( object ) : pass <EOL> class GEQ ( object ) : pass <EOL> class LEQ ( object ) : pass <EOL> class NEQ ( object ) : pass <EOL> def match ( obj , selector , context = None ) : <EOL> '''<STR_LIT>''' <EOL> context = context or { } <EOL> for key , val in selector . items ( ) : <EOL> if isinstance ( key , string_types ) : <EOL> if key == "<STR_LIT:type>" : <EOL> if isinstance ( val , dict ) and list ( val . keys ( ) ) == [ IN ] : <EOL> if not any ( isinstance ( obj , x ) for x in val [ IN ] ) : return False <EOL> elif not isinstance ( obj , val ) : return False <EOL> elif key == '<STR_LIT>' : <EOL> if isinstance ( val , string_types ) : <EOL> if val not in obj . tags : return False <EOL> else : <EOL> try : <EOL> if not set ( val ) & set ( obj . tags ) : return False <EOL> except TypeError : <EOL> if val not in obj . tags : return False <EOL> elif not hasattr ( obj , key ) : return False <EOL> else : <EOL> attr = getattr ( obj , key ) <EOL> if callable ( attr ) : <EOL> try : <EOL> if not attr ( val , ** context ) : return False <EOL> except : <EOL> return False <EOL> elif isinstance ( val , dict ) : <EOL> if not match ( attr , val , context ) : return False <EOL> else : <EOL> if attr != val : return False <EOL> elif key is OR : <EOL> if not _or ( obj , val ) : return False <EOL> elif key in _operators : <EOL> if not _operators [ key ] ( obj , val ) : return False <EOL> else : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> return True <EOL> def find ( objs , selector , context = None ) : <EOL> '''<STR_LIT>''' <EOL> return ( obj for obj in objs if match ( obj , selector , context ) ) <EOL> def _or ( obj , selectors ) : <EOL> return any ( match ( obj , selector ) for selector in selectors ) <EOL> _operators = { <EOL> IN : lambda x , y : x in y , <EOL> GT : lambda x , y : x > y , <EOL> LT : lambda x , y : x < y , <EOL> EQ : lambda x , y : x == y , <EOL> GEQ : lambda x , y : x >= y , <EOL> LEQ : lambda x , y : x <= y , <EOL> NEQ : lambda x , y : x != y , <EOL> } </s>
