<s> """<STR_LIT>""" <EOL> import ast <EOL> import time <EOL> import warnings <EOL> from functools import partial <EOL> from datetime import datetime , timedelta <EOL> import numpy as np <EOL> import pandas as pd <EOL> import pandas . core . common as com <EOL> from pandas . compat import u , string_types , DeepChainMap <EOL> from pandas . core . base import StringMixin <EOL> from pandas . formats . printing import pprint_thing , pprint_thing_encoded <EOL> from pandas . computation import expr , ops <EOL> from pandas . computation . ops import is_term , UndefinedVariableError <EOL> from pandas . computation . expr import BaseExprVisitor <EOL> from pandas . computation . common import _ensure_decoded <EOL> from pandas . tseries . timedeltas import _coerce_scalar_to_timedelta_type <EOL> class Scope ( expr . Scope ) : <EOL> __slots__ = '<STR_LIT>' , <EOL> def __init__ ( self , level , global_dict = None , local_dict = None , <EOL> queryables = None ) : <EOL> super ( Scope , self ) . __init__ ( level + <NUM_LIT:1> , global_dict = global_dict , <EOL> local_dict = local_dict ) <EOL> self . queryables = queryables or dict ( ) <EOL> class Term ( ops . Term ) : <EOL> def __new__ ( cls , name , env , side = None , encoding = None ) : <EOL> klass = Constant if not isinstance ( name , string_types ) else cls <EOL> supr_new = StringMixin . __new__ <EOL> return supr_new ( klass ) <EOL> def __init__ ( self , name , env , side = None , encoding = None ) : <EOL> super ( Term , self ) . __init__ ( name , env , side = side , encoding = encoding ) <EOL> def _resolve_name ( self ) : <EOL> if self . side == '<STR_LIT:left>' : <EOL> if self . name not in self . env . queryables : <EOL> raise NameError ( '<STR_LIT>' . format ( self . name ) ) <EOL> return self . name <EOL> try : <EOL> return self . env . resolve ( self . name , is_local = False ) <EOL> except UndefinedVariableError : <EOL> return self . name <EOL> @ property <EOL> def value ( self ) : <EOL> return self . _value <EOL> class Constant ( Term ) : <EOL> def __init__ ( self , value , env , side = None , encoding = None ) : <EOL> super ( Constant , self ) . __init__ ( value , env , side = side , <EOL> encoding = encoding ) <EOL> def _resolve_name ( self ) : <EOL> return self . _name <EOL> class BinOp ( ops . BinOp ) : <EOL> _max_selectors = <NUM_LIT> <EOL> def __init__ ( self , op , lhs , rhs , queryables , encoding ) : <EOL> super ( BinOp , self ) . __init__ ( op , lhs , rhs ) <EOL> self . queryables = queryables <EOL> self . encoding = encoding <EOL> self . filter = None <EOL> self . condition = None <EOL> def _disallow_scalar_only_bool_ops ( self ) : <EOL> pass <EOL> def prune ( self , klass ) : <EOL> def pr ( left , right ) : <EOL> """<STR_LIT>""" <EOL> if left is None : <EOL> return right <EOL> elif right is None : <EOL> return left <EOL> k = klass <EOL> if isinstance ( left , ConditionBinOp ) : <EOL> if ( isinstance ( left , ConditionBinOp ) and <EOL> isinstance ( right , ConditionBinOp ) ) : <EOL> k = JointConditionBinOp <EOL> elif isinstance ( left , k ) : <EOL> return left <EOL> elif isinstance ( right , k ) : <EOL> return right <EOL> elif isinstance ( left , FilterBinOp ) : <EOL> if ( isinstance ( left , FilterBinOp ) and <EOL> isinstance ( right , FilterBinOp ) ) : <EOL> k = JointFilterBinOp <EOL> elif isinstance ( left , k ) : <EOL> return left <EOL> elif isinstance ( right , k ) : <EOL> return right <EOL> return k ( self . op , left , right , queryables = self . queryables , <EOL> encoding = self . encoding ) . evaluate ( ) <EOL> left , right = self . lhs , self . rhs <EOL> if is_term ( left ) and is_term ( right ) : <EOL> res = pr ( left . value , right . value ) <EOL> elif not is_term ( left ) and is_term ( right ) : <EOL> res = pr ( left . prune ( klass ) , right . value ) <EOL> elif is_term ( left ) and not is_term ( right ) : <EOL> res = pr ( left . value , right . prune ( klass ) ) <EOL> elif not ( is_term ( left ) or is_term ( right ) ) : <EOL> res = pr ( left . prune ( klass ) , right . prune ( klass ) ) <EOL> return res <EOL> def conform ( self , rhs ) : <EOL> """<STR_LIT>""" <EOL> if not com . is_list_like ( rhs ) : <EOL> rhs = [ rhs ] <EOL> if isinstance ( rhs , np . ndarray ) : <EOL> rhs = rhs . ravel ( ) <EOL> return rhs <EOL> @ property <EOL> def is_valid ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . lhs in self . queryables <EOL> @ property <EOL> def is_in_table ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . queryables . get ( self . lhs ) is not None <EOL> @ property <EOL> def kind ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self . queryables . get ( self . lhs ) , '<STR_LIT>' , None ) <EOL> @ property <EOL> def meta ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self . queryables . get ( self . lhs ) , '<STR_LIT>' , None ) <EOL> @ property <EOL> def metadata ( self ) : <EOL> """<STR_LIT>""" <EOL> return getattr ( self . queryables . get ( self . lhs ) , '<STR_LIT>' , None ) <EOL> def generate ( self , v ) : <EOL> """<STR_LIT>""" <EOL> val = v . tostring ( self . encoding ) <EOL> return "<STR_LIT>" % ( self . lhs , self . op , val ) <EOL> def convert_value ( self , v ) : <EOL> """<STR_LIT>""" <EOL> def stringify ( value ) : <EOL> if self . encoding is not None : <EOL> encoder = partial ( pprint_thing_encoded , <EOL> encoding = self . encoding ) <EOL> else : <EOL> encoder = pprint_thing <EOL> return encoder ( value ) <EOL> kind = _ensure_decoded ( self . kind ) <EOL> meta = _ensure_decoded ( self . meta ) <EOL> if kind == u ( '<STR_LIT>' ) or kind == u ( '<STR_LIT>' ) : <EOL> if isinstance ( v , ( int , float ) ) : <EOL> v = stringify ( v ) <EOL> v = _ensure_decoded ( v ) <EOL> v = pd . Timestamp ( v ) <EOL> if v . tz is not None : <EOL> v = v . tz_convert ( '<STR_LIT>' ) <EOL> return TermValue ( v , v . value , kind ) <EOL> elif ( isinstance ( v , datetime ) or hasattr ( v , '<STR_LIT>' ) or <EOL> kind == u ( '<STR_LIT:date>' ) ) : <EOL> v = time . mktime ( v . timetuple ( ) ) <EOL> return TermValue ( v , pd . Timestamp ( v ) , kind ) <EOL> elif kind == u ( '<STR_LIT>' ) or kind == u ( '<STR_LIT>' ) : <EOL> v = _coerce_scalar_to_timedelta_type ( v , unit = '<STR_LIT:s>' ) . value <EOL> return TermValue ( int ( v ) , v , kind ) <EOL> elif meta == u ( '<STR_LIT>' ) : <EOL> metadata = com . _values_from_object ( self . metadata ) <EOL> result = metadata . searchsorted ( v , side = '<STR_LIT:left>' ) <EOL> return TermValue ( result , result , u ( '<STR_LIT>' ) ) <EOL> elif kind == u ( '<STR_LIT>' ) : <EOL> v = int ( float ( v ) ) <EOL> return TermValue ( v , v , kind ) <EOL> elif kind == u ( '<STR_LIT:float>' ) : <EOL> v = float ( v ) <EOL> return TermValue ( v , v , kind ) <EOL> elif kind == u ( '<STR_LIT:bool>' ) : <EOL> if isinstance ( v , string_types ) : <EOL> v = not v . strip ( ) . lower ( ) in [ u ( '<STR_LIT:false>' ) , u ( '<STR_LIT:f>' ) , u ( '<STR_LIT>' ) , <EOL> u ( '<STR_LIT:n>' ) , u ( '<STR_LIT:none>' ) , u ( '<STR_LIT:0>' ) , <EOL> u ( '<STR_LIT>' ) , u ( '<STR_LIT:{}>' ) , u ( '<STR_LIT>' ) ] <EOL> else : <EOL> v = bool ( v ) <EOL> return TermValue ( v , v , kind ) <EOL> elif not isinstance ( v , string_types ) : <EOL> v = stringify ( v ) <EOL> return TermValue ( v , stringify ( v ) , u ( '<STR_LIT:string>' ) ) <EOL> return TermValue ( v , stringify ( v ) , u ( '<STR_LIT:string>' ) ) <EOL> def convert_values ( self ) : <EOL> pass <EOL> class FilterBinOp ( BinOp ) : <EOL> def __unicode__ ( self ) : <EOL> return pprint_thing ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . filter [ <NUM_LIT:0> ] , self . filter [ <NUM_LIT:1> ] ) ) <EOL> def invert ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . filter is not None : <EOL> f = list ( self . filter ) <EOL> f [ <NUM_LIT:1> ] = self . generate_filter_op ( invert = True ) <EOL> self . filter = tuple ( f ) <EOL> return self <EOL> def format ( self ) : <EOL> """<STR_LIT>""" <EOL> return [ self . filter ] <EOL> def evaluate ( self ) : <EOL> if not self . is_valid : <EOL> raise ValueError ( "<STR_LIT>" % self ) <EOL> rhs = self . conform ( self . rhs ) <EOL> values = [ TermValue ( v , v , self . kind ) for v in rhs ] <EOL> if self . is_in_table : <EOL> if self . op in [ '<STR_LIT>' , '<STR_LIT>' ] and len ( values ) > self . _max_selectors : <EOL> filter_op = self . generate_filter_op ( ) <EOL> self . filter = ( <EOL> self . lhs , <EOL> filter_op , <EOL> pd . Index ( [ v . value for v in values ] ) ) <EOL> return self <EOL> return None <EOL> if self . op in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> filter_op = self . generate_filter_op ( ) <EOL> self . filter = ( <EOL> self . lhs , <EOL> filter_op , <EOL> pd . Index ( [ v . value for v in values ] ) ) <EOL> else : <EOL> raise TypeError ( <EOL> "<STR_LIT>" % <EOL> self ) <EOL> return self <EOL> def generate_filter_op ( self , invert = False ) : <EOL> if ( self . op == '<STR_LIT>' and not invert ) or ( self . op == '<STR_LIT>' and invert ) : <EOL> return lambda axis , vals : ~ axis . isin ( vals ) <EOL> else : <EOL> return lambda axis , vals : axis . isin ( vals ) <EOL> class JointFilterBinOp ( FilterBinOp ) : <EOL> def format ( self ) : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> def evaluate ( self ) : <EOL> return self <EOL> class ConditionBinOp ( BinOp ) : <EOL> def __unicode__ ( self ) : <EOL> return pprint_thing ( "<STR_LIT>" . format ( self . condition ) ) <EOL> def invert ( self ) : <EOL> """<STR_LIT>""" <EOL> raise NotImplementedError ( "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> def format ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . condition <EOL> def evaluate ( self ) : <EOL> if not self . is_valid : <EOL> raise ValueError ( "<STR_LIT>" % self ) <EOL> if not self . is_in_table : <EOL> return None <EOL> rhs = self . conform ( self . rhs ) <EOL> values = [ self . convert_value ( v ) for v in rhs ] <EOL> if self . op in [ '<STR_LIT>' , '<STR_LIT>' ] : <EOL> if len ( values ) <= self . _max_selectors : <EOL> vs = [ self . generate ( v ) for v in values ] <EOL> self . condition = "<STR_LIT>" % '<STR_LIT>' . join ( vs ) <EOL> else : <EOL> return None <EOL> else : <EOL> self . condition = self . generate ( values [ <NUM_LIT:0> ] ) <EOL> return self <EOL> class JointConditionBinOp ( ConditionBinOp ) : <EOL> def evaluate ( self ) : <EOL> self . condition = "<STR_LIT>" % ( <EOL> self . lhs . condition , <EOL> self . op , <EOL> self . rhs . condition ) <EOL> return self <EOL> class UnaryOp ( ops . UnaryOp ) : <EOL> def prune ( self , klass ) : <EOL> if self . op != '<STR_LIT>' : <EOL> raise NotImplementedError ( "<STR_LIT>" ) <EOL> operand = self . operand <EOL> operand = operand . prune ( klass ) <EOL> if operand is not None : <EOL> if issubclass ( klass , ConditionBinOp ) : <EOL> if operand . condition is not None : <EOL> return operand . invert ( ) <EOL> elif issubclass ( klass , FilterBinOp ) : <EOL> if operand . filter is not None : <EOL> return operand . invert ( ) <EOL> return None <EOL> _op_classes = { '<STR_LIT>' : UnaryOp } <EOL> class ExprVisitor ( BaseExprVisitor ) : <EOL> const_type = Constant <EOL> term_type = Term <EOL> def __init__ ( self , env , engine , parser , ** kwargs ) : <EOL> super ( ExprVisitor , self ) . __init__ ( env , engine , parser ) <EOL> for bin_op in self . binary_ops : <EOL> setattr ( self , '<STR_LIT>' . format ( self . binary_op_nodes_map [ bin_op ] ) , <EOL> lambda node , bin_op = bin_op : partial ( BinOp , bin_op , <EOL> ** kwargs ) ) <EOL> def visit_UnaryOp ( self , node , ** kwargs ) : <EOL> if isinstance ( node . op , ( ast . Not , ast . Invert ) ) : <EOL> return UnaryOp ( '<STR_LIT>' , self . visit ( node . operand ) ) <EOL> elif isinstance ( node . op , ast . USub ) : <EOL> return self . const_type ( - self . visit ( node . operand ) . value , self . env ) <EOL> elif isinstance ( node . op , ast . UAdd ) : <EOL> raise NotImplementedError ( '<STR_LIT>' ) <EOL> def visit_Index ( self , node , ** kwargs ) : <EOL> return self . visit ( node . value ) . value <EOL> def visit_Assign ( self , node , ** kwargs ) : <EOL> cmpr = ast . Compare ( ops = [ ast . Eq ( ) ] , left = node . targets [ <NUM_LIT:0> ] , <EOL> comparators = [ node . value ] ) <EOL> return self . visit ( cmpr ) <EOL> def visit_Subscript ( self , node , ** kwargs ) : <EOL> value = self . visit ( node . value ) <EOL> slobj = self . visit ( node . slice ) <EOL> try : <EOL> value = value . value <EOL> except : <EOL> pass <EOL> try : <EOL> return self . const_type ( value [ slobj ] , self . env ) <EOL> except TypeError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( value , slobj ) ) <EOL> def visit_Attribute ( self , node , ** kwargs ) : <EOL> attr = node . attr <EOL> value = node . value <EOL> ctx = node . ctx . __class__ <EOL> if ctx == ast . Load : <EOL> resolved = self . visit ( value ) <EOL> try : <EOL> resolved = resolved . value <EOL> except ( AttributeError ) : <EOL> pass <EOL> try : <EOL> return self . term_type ( getattr ( resolved , attr ) , self . env ) <EOL> except AttributeError : <EOL> if isinstance ( value , ast . Name ) and value . id == attr : <EOL> return resolved <EOL> raise ValueError ( "<STR_LIT>" . format ( ctx . __name__ ) ) <EOL> def translate_In ( self , op ) : <EOL> return ast . Eq ( ) if isinstance ( op , ast . In ) else op <EOL> def _rewrite_membership_op ( self , node , left , right ) : <EOL> return self . visit ( node . op ) , node . op , left , right <EOL> class Expr ( expr . Expr ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , where , op = None , value = None , queryables = None , <EOL> encoding = None , scope_level = <NUM_LIT:0> ) : <EOL> where = self . parse_back_compat ( where , op , value ) <EOL> self . encoding = encoding <EOL> self . condition = None <EOL> self . filter = None <EOL> self . terms = None <EOL> self . _visitor = None <EOL> local_dict = DeepChainMap ( ) <EOL> if isinstance ( where , Expr ) : <EOL> local_dict = where . env . scope <EOL> where = where . expr <EOL> elif isinstance ( where , ( list , tuple ) ) : <EOL> for idx , w in enumerate ( where ) : <EOL> if isinstance ( w , Expr ) : <EOL> local_dict = w . env . scope <EOL> else : <EOL> w = self . parse_back_compat ( w ) <EOL> where [ idx ] = w <EOL> where = '<STR_LIT>' . join ( [ "<STR_LIT>" % w for w in where ] ) <EOL> self . expr = where <EOL> self . env = Scope ( scope_level + <NUM_LIT:1> , local_dict = local_dict ) <EOL> if queryables is not None and isinstance ( self . expr , string_types ) : <EOL> self . env . queryables . update ( queryables ) <EOL> self . _visitor = ExprVisitor ( self . env , queryables = queryables , <EOL> parser = '<STR_LIT>' , engine = '<STR_LIT>' , <EOL> encoding = encoding ) <EOL> self . terms = self . parse ( ) <EOL> def parse_back_compat ( self , w , op = None , value = None ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( w , dict ) : <EOL> w , op , value = w . get ( '<STR_LIT>' ) , w . get ( '<STR_LIT>' ) , w . get ( '<STR_LIT:value>' ) <EOL> if not isinstance ( w , string_types ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> FutureWarning , stacklevel = <NUM_LIT:10> ) <EOL> if isinstance ( w , tuple ) : <EOL> if len ( w ) == <NUM_LIT:2> : <EOL> w , value = w <EOL> op = '<STR_LIT>' <EOL> elif len ( w ) == <NUM_LIT:3> : <EOL> w , op , value = w <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> FutureWarning , stacklevel = <NUM_LIT:10> ) <EOL> if op is not None : <EOL> if not isinstance ( w , string_types ) : <EOL> raise TypeError ( <EOL> "<STR_LIT>" ) <EOL> if isinstance ( op , Expr ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> w = "<STR_LIT>" . format ( w , op ) <EOL> if value is not None : <EOL> if isinstance ( value , Expr ) : <EOL> raise TypeError ( "<STR_LIT>" ) <EOL> def convert ( v ) : <EOL> if ( isinstance ( v , ( datetime , np . datetime64 , <EOL> timedelta , np . timedelta64 ) ) or <EOL> hasattr ( v , '<STR_LIT>' ) ) : <EOL> return "<STR_LIT>" . format ( v ) <EOL> return v <EOL> if isinstance ( value , ( list , tuple ) ) : <EOL> value = [ convert ( v ) for v in value ] <EOL> else : <EOL> value = convert ( value ) <EOL> w = "<STR_LIT>" . format ( w , value ) <EOL> warnings . warn ( "<STR_LIT>" <EOL> "<STR_LIT>" , <EOL> FutureWarning , stacklevel = <NUM_LIT:10> ) <EOL> return w <EOL> def __unicode__ ( self ) : <EOL> if self . terms is not None : <EOL> return pprint_thing ( self . terms ) <EOL> return pprint_thing ( self . expr ) <EOL> def evaluate ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . condition = self . terms . prune ( ConditionBinOp ) <EOL> except AttributeError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . expr , self ) ) <EOL> try : <EOL> self . filter = self . terms . prune ( FilterBinOp ) <EOL> except AttributeError : <EOL> raise ValueError ( "<STR_LIT>" <EOL> "<STR_LIT>" . format ( self . expr , self ) ) <EOL> return self . condition , self . filter <EOL> class TermValue ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , value , converted , kind ) : <EOL> self . value = value <EOL> self . converted = converted <EOL> self . kind = kind <EOL> def tostring ( self , encoding ) : <EOL> """<STR_LIT>""" <EOL> if self . kind == u ( '<STR_LIT:string>' ) : <EOL> if encoding is not None : <EOL> return self . converted <EOL> return '<STR_LIT>' % self . converted <EOL> return self . converted <EOL> def maybe_expression ( s ) : <EOL> """<STR_LIT>""" <EOL> if not isinstance ( s , string_types ) : <EOL> return False <EOL> ops = ExprVisitor . binary_ops + ExprVisitor . unary_ops + ( '<STR_LIT:=>' , ) <EOL> return any ( op in s for op in ops ) </s>
