<s> """<STR_LIT>""" <EOL> from __future__ import division , absolute_import , print_function <EOL> import sys <EOL> from ast import Num , Name , Tuple , Div , Mult , Sub , Add <EOL> if sys . version_info [ <NUM_LIT:0> ] >= <NUM_LIT:3> : <EOL> from io import StringIO <EOL> else : <EOL> from StringIO import StringIO <EOL> def unparse ( ast , single_line_functions = False ) : <EOL> s = StringIO ( ) <EOL> UnparseCompilerAst ( ast , s , single_line_functions ) <EOL> return s . getvalue ( ) . lstrip ( ) <EOL> op_precedence = { '<STR_LIT>' : <NUM_LIT:3> , '<STR_LIT>' : <NUM_LIT:2> , '<STR_LIT>' : <NUM_LIT:2> , <EOL> '<STR_LIT>' : <NUM_LIT:1> , '<STR_LIT>' : <NUM_LIT:1> } <EOL> class UnparseCompilerAst : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , tree , file = sys . stdout , single_line_functions = False ) : <EOL> """<STR_LIT>""" <EOL> self . f = file <EOL> self . _single_func = single_line_functions <EOL> self . _do_indent = True <EOL> self . _indent = <NUM_LIT:0> <EOL> self . _dispatch ( tree ) <EOL> self . _write ( "<STR_LIT:\n>" ) <EOL> self . f . flush ( ) <EOL> def _fill ( self , text = "<STR_LIT>" ) : <EOL> "<STR_LIT>" <EOL> if self . _do_indent : <EOL> self . _write ( "<STR_LIT:\n>" + "<STR_LIT:U+0020>" * self . _indent + text ) <EOL> else : <EOL> self . _write ( text ) <EOL> def _write ( self , text ) : <EOL> "<STR_LIT>" <EOL> self . f . write ( text ) <EOL> def _enter ( self ) : <EOL> "<STR_LIT>" <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _indent += <NUM_LIT:1> <EOL> def _leave ( self ) : <EOL> "<STR_LIT>" <EOL> self . _indent -= <NUM_LIT:1> <EOL> def _dispatch ( self , tree ) : <EOL> "<STR_LIT>" <EOL> if isinstance ( tree , list ) : <EOL> for t in tree : <EOL> self . _dispatch ( t ) <EOL> return <EOL> meth = getattr ( self , "<STR_LIT:_>" + tree . __class__ . __name__ ) <EOL> if tree . __class__ . __name__ == '<STR_LIT>' and not self . _do_indent : <EOL> return <EOL> meth ( tree ) <EOL> def _Add ( self , t ) : <EOL> self . __binary_op ( t , '<STR_LIT:+>' ) <EOL> def _And ( self , t ) : <EOL> self . _write ( "<STR_LIT>" ) <EOL> for i , node in enumerate ( t . nodes ) : <EOL> self . _dispatch ( node ) <EOL> if i != len ( t . nodes ) - <NUM_LIT:1> : <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _write ( "<STR_LIT:)>" ) <EOL> def _AssAttr ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _dispatch ( t . expr ) <EOL> self . _write ( '<STR_LIT:.>' + t . attrname ) <EOL> def _Assign ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _fill ( ) <EOL> for target in t . targets : <EOL> self . _dispatch ( target ) <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _dispatch ( t . value ) <EOL> if not self . _do_indent : <EOL> self . _write ( '<STR_LIT>' ) <EOL> def _AssName ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _Name ( t ) <EOL> def _AssTuple ( self , t ) : <EOL> """<STR_LIT>""" <EOL> for element in t . nodes [ : - <NUM_LIT:1> ] : <EOL> self . _dispatch ( element ) <EOL> self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> last_element = t . nodes [ - <NUM_LIT:1> ] <EOL> self . _dispatch ( last_element ) <EOL> def _AugAssign ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _fill ( ) <EOL> self . _dispatch ( t . node ) <EOL> self . _write ( '<STR_LIT:U+0020>' + t . op + '<STR_LIT:U+0020>' ) <EOL> self . _dispatch ( t . expr ) <EOL> if not self . _do_indent : <EOL> self . _write ( '<STR_LIT:;>' ) <EOL> def _Bitand ( self , t ) : <EOL> """<STR_LIT>""" <EOL> for i , node in enumerate ( t . nodes ) : <EOL> self . _write ( "<STR_LIT:(>" ) <EOL> self . _dispatch ( node ) <EOL> self . _write ( "<STR_LIT:)>" ) <EOL> if i != len ( t . nodes ) - <NUM_LIT:1> : <EOL> self . _write ( "<STR_LIT>" ) <EOL> def _Bitor ( self , t ) : <EOL> """<STR_LIT>""" <EOL> for i , node in enumerate ( t . nodes ) : <EOL> self . _write ( "<STR_LIT:(>" ) <EOL> self . _dispatch ( node ) <EOL> self . _write ( "<STR_LIT:)>" ) <EOL> if i != len ( t . nodes ) - <NUM_LIT:1> : <EOL> self . _write ( "<STR_LIT>" ) <EOL> def _Call ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _dispatch ( t . func ) <EOL> self . _write ( "<STR_LIT:(>" ) <EOL> comma = False <EOL> for e in t . args : <EOL> if comma : self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> else : comma = True <EOL> self . _dispatch ( e ) <EOL> if t . starargs : <EOL> if comma : self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> else : comma = True <EOL> self . _write ( "<STR_LIT:*>" ) <EOL> self . _dispatch ( t . starargs ) <EOL> if t . kwargs : <EOL> if comma : self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> else : comma = True <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _dispatch ( t . kwargs ) <EOL> self . _write ( "<STR_LIT:)>" ) <EOL> def _Compare ( self , t ) : <EOL> self . _dispatch ( t . expr ) <EOL> for op , expr in t . ops : <EOL> self . _write ( "<STR_LIT:U+0020>" + op + "<STR_LIT:U+0020>" ) <EOL> self . _dispatch ( expr ) <EOL> def _Num ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _dispatch ( t . n ) <EOL> def _NameConstant ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _dispatch ( repr ( t . value ) ) <EOL> def _Str ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _dispatch ( t . s ) <EOL> def _Decorators ( self , t ) : <EOL> """<STR_LIT>""" <EOL> for node in t . nodes : <EOL> self . _dispatch ( node ) <EOL> def _Dict ( self , t ) : <EOL> self . _write ( "<STR_LIT:{>" ) <EOL> for i , ( k , v ) in enumerate ( t . items ) : <EOL> self . _dispatch ( k ) <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _dispatch ( v ) <EOL> if i < len ( t . items ) - <NUM_LIT:1> : <EOL> self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> self . _write ( "<STR_LIT:}>" ) <EOL> def _Discard ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _fill ( ) <EOL> self . _dispatch ( t . expr ) <EOL> def _Div ( self , t ) : <EOL> self . __binary_op ( t , '<STR_LIT:/>' ) <EOL> def _Ellipsis ( self , t ) : <EOL> self . _write ( "<STR_LIT>" ) <EOL> def _From ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _fill ( "<STR_LIT>" ) <EOL> self . _write ( t . modname ) <EOL> self . _write ( "<STR_LIT>" ) <EOL> for i , ( name , asname ) in enumerate ( t . names ) : <EOL> if i != <NUM_LIT:0> : <EOL> self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> self . _write ( name ) <EOL> if asname is not None : <EOL> self . _write ( "<STR_LIT>" + asname ) <EOL> def _FunctionDef ( self , t ) : <EOL> """<STR_LIT>""" <EOL> for decorator in t . decorator_list : <EOL> self . _fill ( "<STR_LIT:@>" ) <EOL> self . _dispatch ( decorator ) <EOL> self . _fill ( "<STR_LIT>" + t . name + "<STR_LIT:(>" ) <EOL> defaults = [ None ] * ( len ( t . argnames ) - len ( t . defaults ) ) + list ( t . defaults ) <EOL> for i , arg in enumerate ( zip ( t . argnames , defaults ) ) : <EOL> self . _write ( arg [ <NUM_LIT:0> ] ) <EOL> if arg [ <NUM_LIT:1> ] is not None : <EOL> self . _write ( '<STR_LIT:=>' ) <EOL> self . _dispatch ( arg [ <NUM_LIT:1> ] ) <EOL> if i < len ( t . argnames ) - <NUM_LIT:1> : <EOL> self . _write ( '<STR_LIT:U+002CU+0020>' ) <EOL> self . _write ( "<STR_LIT:)>" ) <EOL> if self . _single_func : <EOL> self . _do_indent = False <EOL> self . _enter ( ) <EOL> self . _dispatch ( t . code ) <EOL> self . _leave ( ) <EOL> self . _do_indent = True <EOL> def _Getattr ( self , t ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( t . expr , ( Div , Mult , Sub , Add ) ) : <EOL> self . _write ( '<STR_LIT:(>' ) <EOL> self . _dispatch ( t . expr ) <EOL> self . _write ( '<STR_LIT:)>' ) <EOL> else : <EOL> self . _dispatch ( t . expr ) <EOL> self . _write ( '<STR_LIT:.>' + t . attrname ) <EOL> def _If ( self , t ) : <EOL> self . _fill ( ) <EOL> for i , ( compare , code ) in enumerate ( t . tests ) : <EOL> if i == <NUM_LIT:0> : <EOL> self . _write ( "<STR_LIT>" ) <EOL> else : <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _dispatch ( compare ) <EOL> self . _enter ( ) <EOL> self . _fill ( ) <EOL> self . _dispatch ( code ) <EOL> self . _leave ( ) <EOL> self . _write ( "<STR_LIT:\n>" ) <EOL> if t . else_ is not None : <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _enter ( ) <EOL> self . _fill ( ) <EOL> self . _dispatch ( t . else_ ) <EOL> self . _leave ( ) <EOL> self . _write ( "<STR_LIT:\n>" ) <EOL> def _IfExp ( self , t ) : <EOL> self . _dispatch ( t . then ) <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _dispatch ( t . test ) <EOL> if t . else_ is not None : <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _dispatch ( t . else_ ) <EOL> self . _write ( "<STR_LIT:)>" ) <EOL> def _Import ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _fill ( "<STR_LIT>" ) <EOL> for i , ( name , asname ) in enumerate ( t . names ) : <EOL> if i != <NUM_LIT:0> : <EOL> self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> self . _write ( name ) <EOL> if asname is not None : <EOL> self . _write ( "<STR_LIT>" + asname ) <EOL> def _Keyword ( self , t ) : <EOL> """<STR_LIT>""" <EOL> self . _write ( t . name ) <EOL> self . _write ( "<STR_LIT:=>" ) <EOL> self . _dispatch ( t . expr ) <EOL> def _List ( self , t ) : <EOL> self . _write ( "<STR_LIT:[>" ) <EOL> for i , node in enumerate ( t . elts ) : <EOL> self . _dispatch ( node ) <EOL> if i < len ( t . elts ) - <NUM_LIT:1> : <EOL> self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> self . _write ( "<STR_LIT:]>" ) <EOL> def _Module ( self , t ) : <EOL> if t . doc is not None : <EOL> self . _dispatch ( t . doc ) <EOL> self . _dispatch ( t . node ) <EOL> def _Mult ( self , t ) : <EOL> self . __binary_op ( t , '<STR_LIT:*>' ) <EOL> def _Name ( self , t ) : <EOL> self . _write ( t . id ) <EOL> def _NoneType ( self , t ) : <EOL> self . _write ( "<STR_LIT:None>" ) <EOL> def _Not ( self , t ) : <EOL> self . _write ( '<STR_LIT>' ) <EOL> self . _dispatch ( t . expr ) <EOL> self . _write ( '<STR_LIT:)>' ) <EOL> def _Or ( self , t ) : <EOL> self . _write ( "<STR_LIT>" ) <EOL> for i , node in enumerate ( t . nodes ) : <EOL> self . _dispatch ( node ) <EOL> if i != len ( t . nodes ) - <NUM_LIT:1> : <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _write ( "<STR_LIT:)>" ) <EOL> def _Pass ( self , t ) : <EOL> self . _write ( "<STR_LIT>" ) <EOL> def _Printnl ( self , t ) : <EOL> self . _fill ( "<STR_LIT>" ) <EOL> if t . dest : <EOL> self . _write ( "<STR_LIT>" ) <EOL> self . _dispatch ( t . dest ) <EOL> self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> comma = False <EOL> for node in t . nodes : <EOL> if comma : self . _write ( '<STR_LIT:U+002CU+0020>' ) <EOL> else : comma = True <EOL> self . _dispatch ( node ) <EOL> def _Power ( self , t ) : <EOL> self . __binary_op ( t , '<STR_LIT>' ) <EOL> def _Return ( self , t ) : <EOL> self . _fill ( "<STR_LIT>" ) <EOL> if t . value : <EOL> if isinstance ( t . value , Tuple ) : <EOL> text = '<STR_LIT:U+002CU+0020>' . join ( [ name . name for name in t . value . asList ( ) ] ) <EOL> self . _write ( text ) <EOL> else : <EOL> self . _dispatch ( t . value ) <EOL> if not self . _do_indent : <EOL> self . _write ( '<STR_LIT>' ) <EOL> def _Slice ( self , t ) : <EOL> self . _dispatch ( t . expr ) <EOL> self . _write ( "<STR_LIT:[>" ) <EOL> if t . lower : <EOL> self . _dispatch ( t . lower ) <EOL> self . _write ( "<STR_LIT::>" ) <EOL> if t . upper : <EOL> self . _dispatch ( t . upper ) <EOL> self . _write ( "<STR_LIT:]>" ) <EOL> def _Sliceobj ( self , t ) : <EOL> for i , node in enumerate ( t . nodes ) : <EOL> if i != <NUM_LIT:0> : <EOL> self . _write ( "<STR_LIT::>" ) <EOL> if not ( isinstance ( node , Num ) and node . value is None ) : <EOL> self . _dispatch ( node ) <EOL> def _Stmt ( self , tree ) : <EOL> for node in tree . nodes : <EOL> self . _dispatch ( node ) <EOL> def _Sub ( self , t ) : <EOL> self . __binary_op ( t , '<STR_LIT:->' ) <EOL> def _Subscript ( self , t ) : <EOL> self . _dispatch ( t . expr ) <EOL> self . _write ( "<STR_LIT:[>" ) <EOL> for i , value in enumerate ( t . subs ) : <EOL> if i != <NUM_LIT:0> : <EOL> self . _write ( "<STR_LIT:U+002C>" ) <EOL> self . _dispatch ( value ) <EOL> self . _write ( "<STR_LIT:]>" ) <EOL> def _TryExcept ( self , t ) : <EOL> self . _fill ( "<STR_LIT>" ) <EOL> self . _enter ( ) <EOL> self . _dispatch ( t . body ) <EOL> self . _leave ( ) <EOL> for handler in t . handlers : <EOL> self . _fill ( '<STR_LIT>' ) <EOL> self . _dispatch ( handler [ <NUM_LIT:0> ] ) <EOL> if handler [ <NUM_LIT:1> ] is not None : <EOL> self . _write ( '<STR_LIT:U+002CU+0020>' ) <EOL> self . _dispatch ( handler [ <NUM_LIT:1> ] ) <EOL> self . _enter ( ) <EOL> self . _dispatch ( handler [ <NUM_LIT:2> ] ) <EOL> self . _leave ( ) <EOL> if t . else_ : <EOL> self . _fill ( "<STR_LIT>" ) <EOL> self . _enter ( ) <EOL> self . _dispatch ( t . else_ ) <EOL> self . _leave ( ) <EOL> def _Tuple ( self , t ) : <EOL> if not t . nodes : <EOL> self . _write ( "<STR_LIT>" ) <EOL> else : <EOL> self . _write ( "<STR_LIT:(>" ) <EOL> for element in t . nodes [ : - <NUM_LIT:1> ] : <EOL> self . _dispatch ( element ) <EOL> self . _write ( "<STR_LIT:U+002CU+0020>" ) <EOL> last_element = t . nodes [ - <NUM_LIT:1> ] <EOL> self . _dispatch ( last_element ) <EOL> self . _write ( "<STR_LIT:)>" ) <EOL> def _UnaryAdd ( self , t ) : <EOL> self . _write ( "<STR_LIT:+>" ) <EOL> self . _dispatch ( t . expr ) <EOL> def _UnarySub ( self , t ) : <EOL> self . _write ( "<STR_LIT:->" ) <EOL> self . _dispatch ( t . expr ) <EOL> def _With ( self , t ) : <EOL> self . _fill ( '<STR_LIT>' ) <EOL> self . _dispatch ( t . expr ) <EOL> if t . vars : <EOL> self . _write ( '<STR_LIT>' ) <EOL> self . _dispatch ( t . vars . name ) <EOL> self . _enter ( ) <EOL> self . _dispatch ( t . body ) <EOL> self . _leave ( ) <EOL> self . _write ( '<STR_LIT:\n>' ) <EOL> def _int ( self , t ) : <EOL> self . _write ( repr ( t ) ) <EOL> def __binary_op ( self , t , symbol ) : <EOL> has_paren = False <EOL> left_class = str ( t . left . __class__ ) <EOL> if ( left_class in op_precedence . keys ( ) and <EOL> op_precedence [ left_class ] < op_precedence [ str ( t . __class__ ) ] ) : <EOL> has_paren = True <EOL> if has_paren : <EOL> self . _write ( '<STR_LIT:(>' ) <EOL> self . _dispatch ( t . left ) <EOL> if has_paren : <EOL> self . _write ( '<STR_LIT:)>' ) <EOL> self . _write ( symbol ) <EOL> has_paren = False <EOL> right_class = str ( t . right . __class__ ) <EOL> if ( right_class in op_precedence . keys ( ) and <EOL> op_precedence [ right_class ] < op_precedence [ str ( t . __class__ ) ] ) : <EOL> has_paren = True <EOL> if has_paren : <EOL> self . _write ( '<STR_LIT:(>' ) <EOL> self . _dispatch ( t . right ) <EOL> if has_paren : <EOL> self . _write ( '<STR_LIT:)>' ) <EOL> def _float ( self , t ) : <EOL> self . _write ( str ( t ) ) <EOL> def _str ( self , t ) : <EOL> self . _write ( repr ( t ) ) <EOL> def _tuple ( self , t ) : <EOL> self . _write ( str ( t ) ) </s>
