<s> import operator <EOL> from . . sql import operators <EOL> from . . import util <EOL> class UnevaluatableError ( Exception ) : <EOL> pass <EOL> _straight_ops = set ( getattr ( operators , op ) <EOL> for op in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> _notimplemented_ops = set ( getattr ( operators , op ) <EOL> for op in ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , <EOL> '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) <EOL> class EvaluatorCompiler ( object ) : <EOL> def __init__ ( self , target_cls = None ) : <EOL> self . target_cls = target_cls <EOL> def process ( self , clause ) : <EOL> meth = getattr ( self , "<STR_LIT>" % clause . __visit_name__ , None ) <EOL> if not meth : <EOL> raise UnevaluatableError ( <EOL> "<STR_LIT>" % type ( clause ) . __name__ ) <EOL> return meth ( clause ) <EOL> def visit_grouping ( self , clause ) : <EOL> return self . process ( clause . element ) <EOL> def visit_null ( self , clause ) : <EOL> return lambda obj : None <EOL> def visit_false ( self , clause ) : <EOL> return lambda obj : False <EOL> def visit_true ( self , clause ) : <EOL> return lambda obj : True <EOL> def visit_column ( self , clause ) : <EOL> if '<STR_LIT>' in clause . _annotations : <EOL> parentmapper = clause . _annotations [ '<STR_LIT>' ] <EOL> if self . target_cls and not issubclass ( <EOL> self . target_cls , parentmapper . class_ ) : <EOL> util . warn ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" % parentmapper . class_ <EOL> ) <EOL> key = parentmapper . _columntoproperty [ clause ] . key <EOL> else : <EOL> key = clause . key <EOL> get_corresponding_attr = operator . attrgetter ( key ) <EOL> return lambda obj : get_corresponding_attr ( obj ) <EOL> def visit_clauselist ( self , clause ) : <EOL> evaluators = list ( map ( self . process , clause . clauses ) ) <EOL> if clause . operator is operators . or_ : <EOL> def evaluate ( obj ) : <EOL> has_null = False <EOL> for sub_evaluate in evaluators : <EOL> value = sub_evaluate ( obj ) <EOL> if value : <EOL> return True <EOL> has_null = has_null or value is None <EOL> if has_null : <EOL> return None <EOL> return False <EOL> elif clause . operator is operators . and_ : <EOL> def evaluate ( obj ) : <EOL> for sub_evaluate in evaluators : <EOL> value = sub_evaluate ( obj ) <EOL> if not value : <EOL> if value is None : <EOL> return None <EOL> return False <EOL> return True <EOL> else : <EOL> raise UnevaluatableError ( <EOL> "<STR_LIT>" % <EOL> clause . operator ) <EOL> return evaluate <EOL> def visit_binary ( self , clause ) : <EOL> eval_left , eval_right = list ( map ( self . process , <EOL> [ clause . left , clause . right ] ) ) <EOL> operator = clause . operator <EOL> if operator is operators . is_ : <EOL> def evaluate ( obj ) : <EOL> return eval_left ( obj ) == eval_right ( obj ) <EOL> elif operator is operators . isnot : <EOL> def evaluate ( obj ) : <EOL> return eval_left ( obj ) != eval_right ( obj ) <EOL> elif operator in _straight_ops : <EOL> def evaluate ( obj ) : <EOL> left_val = eval_left ( obj ) <EOL> right_val = eval_right ( obj ) <EOL> if left_val is None or right_val is None : <EOL> return None <EOL> return operator ( eval_left ( obj ) , eval_right ( obj ) ) <EOL> else : <EOL> raise UnevaluatableError ( <EOL> "<STR_LIT>" % <EOL> ( type ( clause ) . __name__ , clause . operator ) ) <EOL> return evaluate <EOL> def visit_unary ( self , clause ) : <EOL> eval_inner = self . process ( clause . element ) <EOL> if clause . operator is operators . inv : <EOL> def evaluate ( obj ) : <EOL> value = eval_inner ( obj ) <EOL> if value is None : <EOL> return None <EOL> return not value <EOL> return evaluate <EOL> raise UnevaluatableError ( <EOL> "<STR_LIT>" % <EOL> ( type ( clause ) . __name__ , clause . operator ) ) <EOL> def visit_bindparam ( self , clause ) : <EOL> val = clause . value <EOL> return lambda obj : val </s>
