<s> import ast <EOL> def _get_attr_node ( names ) : <EOL> """<STR_LIT>""" <EOL> node = ast . Name ( id = names [ <NUM_LIT:0> ] , ctx = ast . Load ( ) ) <EOL> for name in names [ <NUM_LIT:1> : ] : <EOL> node = ast . Attribute ( value = node , attr = name , ctx = ast . Load ( ) ) <EOL> return node <EOL> def _get_long_name ( node ) : <EOL> if isinstance ( node , ast . Name ) : <EOL> return node . id <EOL> elif not isinstance ( node , ast . Attribute ) : <EOL> return None <EOL> val = node . value <EOL> parts = [ node . attr ] <EOL> while True : <EOL> if isinstance ( val , ast . Attribute ) : <EOL> parts . append ( val . attr ) <EOL> val = val . value <EOL> elif isinstance ( val , ast . Name ) : <EOL> parts . append ( val . id ) <EOL> break <EOL> else : <EOL> return None <EOL> return '<STR_LIT:.>' . join ( parts [ : : - <NUM_LIT:1> ] ) <EOL> _op_preds = { } <EOL> _prec = <NUM_LIT:0> <EOL> _op_preds [ ast . Lambda ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . If ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . Or ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . And ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . Not ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . In ] = _prec <EOL> _op_preds [ ast . NotIn ] = _prec <EOL> _op_preds [ ast . Is ] = _prec <EOL> _op_preds [ ast . IsNot ] = _prec <EOL> _op_preds [ ast . Lt ] = _prec <EOL> _op_preds [ ast . LtE ] = _prec <EOL> _op_preds [ ast . Gt ] = _prec <EOL> _op_preds [ ast . GtE ] = _prec <EOL> _op_preds [ ast . NotEq ] = _prec <EOL> _op_preds [ ast . Eq ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . BitOr ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . BitXor ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . BitAnd ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . LShift ] = _prec <EOL> _op_preds [ ast . RShift ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . Add ] = _prec <EOL> _op_preds [ ast . Sub ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . Mult ] = _prec <EOL> _op_preds [ ast . Div ] = _prec <EOL> _op_preds [ ast . FloorDiv ] = _prec <EOL> _op_preds [ ast . Mod ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . UAdd ] = _prec <EOL> _op_preds [ ast . USub ] = _prec <EOL> _op_preds [ ast . Invert ] = _prec <EOL> _prec += <NUM_LIT:1> <EOL> _op_preds [ ast . Pow ] = _prec <EOL> def _pred_cmp ( op1 , op2 ) : <EOL> """<STR_LIT>""" <EOL> return _op_preds [ op1 . __class__ ] - _op_preds [ op2 . __class__ ] <EOL> class ExprPrinter ( ast . NodeVisitor ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> super ( ExprPrinter , self ) . __init__ ( ) <EOL> self . txtlist = [ ] <EOL> self . append = self . txtlist . append <EOL> def get_text ( self ) : <EOL> return '<STR_LIT>' . join ( self . txtlist ) <EOL> def visit_Attribute ( self , node ) : <EOL> self . visit ( node . value ) <EOL> self . append ( "<STR_LIT>" % node . attr ) <EOL> def visit_Assign ( self , node ) : <EOL> for i , t in enumerate ( node . targets ) : <EOL> if i > <NUM_LIT:0> : self . append ( '<STR_LIT:U+002C>' ) <EOL> self . visit ( t ) <EOL> self . append ( '<STR_LIT>' ) <EOL> self . visit ( node . value ) <EOL> def visit_Name ( self , node ) : <EOL> self . append ( node . id ) <EOL> def visit_UnaryOp ( self , node ) : <EOL> if isinstance ( node . operand , ast . BinOp ) : <EOL> self . visit ( node . op ) <EOL> self . append ( '<STR_LIT:(>' ) <EOL> self . visit ( node . operand ) <EOL> self . append ( '<STR_LIT:)>' ) <EOL> else : <EOL> super ( ExprPrinter , self ) . generic_visit ( node ) <EOL> def visit_BinOp ( self , node ) : <EOL> if isinstance ( node . left , ast . BinOp ) and _pred_cmp ( node . left . op , node . op ) < <NUM_LIT:0> : <EOL> self . append ( '<STR_LIT:(>' ) <EOL> self . visit ( node . left ) <EOL> self . append ( '<STR_LIT:)>' ) <EOL> else : <EOL> self . visit ( node . left ) <EOL> self . visit ( node . op ) <EOL> if isinstance ( node . right , ast . BinOp ) : <EOL> pred_comp = _pred_cmp ( node . right . op , node . op ) <EOL> if pred_comp < <NUM_LIT:0> or ( pred_comp == <NUM_LIT:0> and ( isinstance ( node . op , ast . Sub ) or isinstance ( node . op , ast . Div ) ) ) : <EOL> self . append ( '<STR_LIT:(>' ) <EOL> self . visit ( node . right ) <EOL> self . append ( '<STR_LIT:)>' ) <EOL> else : <EOL> self . visit ( node . right ) <EOL> else : <EOL> self . visit ( node . right ) <EOL> def visit_IfExp ( self , node ) : <EOL> self . visit ( node . body ) <EOL> self . append ( '<STR_LIT>' ) <EOL> self . visit ( node . test ) <EOL> self . append ( '<STR_LIT>' ) <EOL> self . visit ( node . orelse ) <EOL> def visit_Call ( self , node ) : <EOL> self . visit ( node . func ) <EOL> self . append ( '<STR_LIT:(>' ) <EOL> total_args = <NUM_LIT:0> <EOL> for arg in node . args : <EOL> if total_args > <NUM_LIT:0> : self . append ( '<STR_LIT:U+002C>' ) <EOL> self . visit ( arg ) <EOL> total_args += <NUM_LIT:1> <EOL> if hasattr ( node , '<STR_LIT>' ) : <EOL> for kw in node . keywords : <EOL> if total_args > <NUM_LIT:0> : self . append ( '<STR_LIT:U+002C>' ) <EOL> self . visit ( kw ) <EOL> total_args += <NUM_LIT:1> <EOL> if hasattr ( node , '<STR_LIT>' ) : <EOL> if node . starargs : <EOL> if total_args > <NUM_LIT:0> : self . append ( '<STR_LIT:U+002C>' ) <EOL> self . append ( '<STR_LIT>' % node . starargs ) <EOL> total_args += <NUM_LIT:1> <EOL> if hasattr ( node , '<STR_LIT>' ) : <EOL> if node . kwargs : <EOL> if total_args > <NUM_LIT:0> : self . append ( '<STR_LIT:U+002C>' ) <EOL> self . append ( '<STR_LIT>' % node . kwargs ) <EOL> self . append ( '<STR_LIT:)>' ) <EOL> def visit_keyword ( self , node ) : <EOL> self . append ( "<STR_LIT>" % node . arg ) <EOL> self . visit ( node . value ) <EOL> def visit_Num ( self , node ) : <EOL> self . append ( str ( node . n ) ) <EOL> def visit_Str ( self , node ) : <EOL> self . append ( "<STR_LIT>" % node . s ) <EOL> def visit_Subscript ( self , node ) : <EOL> self . visit ( node . value ) <EOL> self . append ( '<STR_LIT:[>' ) <EOL> self . visit ( node . slice ) <EOL> self . append ( '<STR_LIT:]>' ) <EOL> def visit_Index ( self , node ) : <EOL> self . visit ( node . value ) <EOL> def visit_Slice ( self , node ) : <EOL> if node . lower is not None : <EOL> if not ( isinstance ( node . lower , ast . Name ) and node . lower . id == '<STR_LIT:None>' ) : <EOL> self . visit ( node . lower ) <EOL> self . append ( '<STR_LIT::>' ) <EOL> if node . upper is not None : <EOL> if not ( isinstance ( node . upper , ast . Name ) and node . upper . id == '<STR_LIT:None>' ) : <EOL> self . visit ( node . upper ) <EOL> self . append ( '<STR_LIT::>' ) <EOL> if node . step is not None : <EOL> if not ( isinstance ( node . step , ast . Name ) and node . step . id == '<STR_LIT:None>' ) : <EOL> self . visit ( node . step ) <EOL> def visit_ExtSlice ( self , node ) : <EOL> for i , d in enumerate ( node . dims ) : <EOL> if i > <NUM_LIT:0> : <EOL> self . append ( '<STR_LIT:U+002C>' ) <EOL> self . visit ( d ) <EOL> def visit_List ( self , node ) : <EOL> self . append ( '<STR_LIT:[>' ) <EOL> for i , e in enumerate ( node . elts ) : <EOL> if i > <NUM_LIT:0> : self . append ( '<STR_LIT:U+002C>' ) <EOL> self . visit ( e ) <EOL> self . append ( '<STR_LIT:]>' ) <EOL> def visit_Dict ( self , node ) : <EOL> self . append ( '<STR_LIT:{>' ) <EOL> for i , tup in enumerate ( zip ( node . keys , node . values ) ) : <EOL> if i > <NUM_LIT:0> : self . append ( '<STR_LIT:U+002C>' ) <EOL> self . append ( "<STR_LIT>" % tup [ <NUM_LIT:0> ] . s ) <EOL> self . visit ( tup [ <NUM_LIT:1> ] ) <EOL> self . append ( '<STR_LIT:}>' ) <EOL> def visit_Tuple ( self , node ) : <EOL> self . append ( '<STR_LIT:(>' ) <EOL> length = len ( node . elts ) <EOL> for i , e in enumerate ( node . elts ) : <EOL> if i > <NUM_LIT:0> : self . append ( '<STR_LIT:U+002C>' ) <EOL> self . visit ( e ) <EOL> if length == <NUM_LIT:1> : self . append ( '<STR_LIT:U+002C>' ) <EOL> self . append ( '<STR_LIT:)>' ) <EOL> def visit_USub ( self , node ) : self . append ( '<STR_LIT:->' ) <EOL> def visit_UAdd ( self , node ) : self . append ( '<STR_LIT:+>' ) <EOL> def visit_And ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_Or ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_Add ( self , node ) : self . append ( '<STR_LIT:+>' ) <EOL> def visit_Sub ( self , node ) : self . append ( '<STR_LIT:->' ) <EOL> def visit_Mult ( self , node ) : self . append ( '<STR_LIT:*>' ) <EOL> def visit_Div ( self , node ) : self . append ( '<STR_LIT:/>' ) <EOL> def visit_Mod ( self , node ) : self . append ( '<STR_LIT:%>' ) <EOL> def visit_Pow ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_LShift ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_Rshift ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_BitOr ( self , node ) : self . append ( '<STR_LIT:|>' ) <EOL> def visit_BitXor ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_BitAnd ( self , node ) : self . append ( '<STR_LIT:&>' ) <EOL> def visit_FloorDiv ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_Eq ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_NotEq ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_Lt ( self , node ) : self . append ( '<STR_LIT:<>' ) <EOL> def visit_LtE ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_Gt ( self , node ) : self . append ( '<STR_LIT:>>' ) <EOL> def visit_GtE ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_Is ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_IsNot ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_In ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def visit_NotIn ( self , node ) : self . append ( '<STR_LIT>' ) <EOL> def _ignore ( self , node ) : <EOL> super ( ExprPrinter , self ) . generic_visit ( node ) <EOL> visit_Module = _ignore <EOL> visit_Expr = _ignore <EOL> visit_Expression = _ignore <EOL> visit_Compare = _ignore <EOL> visit_Load = _ignore <EOL> visit_Store = _ignore <EOL> def generic_visit ( self , node ) : <EOL> raise RuntimeError ( "<STR_LIT>" % node . __class__ . __name__ ) <EOL> class ExprNameTransformer ( ast . NodeTransformer ) : <EOL> def __init__ ( self , mapping ) : <EOL> self . mapping = mapping . copy ( ) <EOL> super ( ExprNameTransformer , self ) . __init__ ( ) <EOL> def visit_Name ( self , node ) : <EOL> return ast . Name ( id = self . mapping . get ( node . id , node . id ) , <EOL> ctx = ast . Load ( ) ) <EOL> def visit_Attribute ( self , node ) : <EOL> long_name = _get_long_name ( node ) <EOL> if long_name is None or long_name not in self . mapping : <EOL> return self . generic_visit ( node ) <EOL> return _get_attr_node ( self . mapping [ long_name ] . split ( '<STR_LIT:.>' ) ) <EOL> def visit_Subscript ( self , node ) : <EOL> p = print_node ( node ) <EOL> xform = self . mapping . get ( p ) <EOL> if xform is not None : <EOL> return _get_attr_node ( xform . split ( '<STR_LIT:.>' ) ) <EOL> return super ( ExprNameTransformer , self ) . generic_visit ( node ) <EOL> def transform_expression ( expr , mapping ) : <EOL> """<STR_LIT>""" <EOL> if expr in mapping : <EOL> return mapping [ expr ] <EOL> new_ast = ExprNameTransformer ( mapping ) . visit ( ast . parse ( expr , mode = '<STR_LIT>' ) ) <EOL> ast . fix_missing_locations ( new_ast ) <EOL> ep = ExprPrinter ( ) <EOL> ep . visit ( new_ast ) <EOL> return ep . get_text ( ) <EOL> def eliminate_expr_ws ( expr ) : <EOL> """<STR_LIT>""" <EOL> node = ast . parse ( expr , mode = '<STR_LIT>' ) <EOL> ep = ExprPrinter ( ) <EOL> ep . visit ( node ) <EOL> return ep . get_text ( ) <EOL> def print_node ( node ) : <EOL> p = ExprPrinter ( ) <EOL> p . visit ( node ) <EOL> return p . get_text ( ) <EOL> if __name__ == '<STR_LIT:__main__>' : <EOL> import sys <EOL> mapping = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> print transform_expression ( sys . argv [ <NUM_LIT:1> ] , mapping ) </s>
