简单的方法来转换python源代码到AST与评论完好无损 [英] Easy way to convert python source code to an AST with comments intact

查看:184
本文介绍了简单的方法来转换python源代码到AST与评论完好无损的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经做了公平的搜索,如何捕获python ASTs与保留的评论。建议的方法包括使用 ast tokenize 库来完成工作。



我根据我的要求在使用这些库方面取得了一定的成功,但我觉得必须有一个更好的方法。



lib2to3 将python2代码转换为python3代码并保留注释的事实。此外,该过程被声明为源代码在Python2 - > AST - >源代码在Python3(以简化的方式提出)。



我的问题是如何捕获之间的 AST?我看过 python-docs ,但没有命令行标志来获取AST。 / p>

只是为了提供上下文:我试图将python源代码转换为XML文件(保留注释)以进行进一步处理 / p>

解决方案

只是为了提供上下文:我试图将python源代码转换为XML文件)进行一些进一步处理



一个简单的方法是使用一个已经做到这一点的工具,而不是重新发明它,时间。



我们的DMS软件重构工具包可以解析Python(和许多其他语言),构建AST,捕获注释,并将结果树以XML格式输出。请参见下面的示例。



注释:XML最初似乎不错,但是是一种笨拙的方式来表示/分析/转换代码。存在DMS这样的工具的原因是提供了以比XML变换更有效的方式操纵解析的AST所需的所有机制,并且可以更好地扩展(例如,达到数百万行代码):最终,节省工程时间和运行时间。



即使你决定使用XML,你要在哪里获得好的工具来处理它? (XSLT不是正确的答案)。最后,如果您打算修改程序,并更改XML,您打算如何获取源代码? DMS可以修改AST并重新生成有效的源程序文本(包括注释)。



因此,DMS将导出AST中的XML(因为像你这样的人似乎坚持) ,此功能在实际中很少使用。典型的用例是解析,分析,修改AST,然后打印修改的AST,所有使用DMS以集成的方式。



对于这个python程序:

 #标题中的注释

import sys

TOKENBLANKS = 1

class MyClassNameTranslator:

#get_name查找名称
def get_name(self,name):
获取真实姓名的翻译
return self.realnames [name]

DMS生成其AST的以下XML版本,完成捕获的注释:

  C:\ [snip] Python\Tools \Parser> domainparser ++ XML C:\ [snip] tiny.py 

Python〜v3_0域解析器版本2.5.15
版权所有(C)1996-2013 Semantic Designs,Inc;版权所有; SD机密
由DMS(R)软件重构工具包提供
树中的165个树节点。
<?xml version =1.1encoding =ISO-8859-1?>
<! - 使用DMS PrintASTasXML(v.1.03) - >
<! - 在2014/03/01 12:14:49生成的XML - >
< DMSForest>
< tree node =Pythontype =1domain =1id =yk0xparents =0line =2column =1file =1>
< tree node =file_inputtype =2domain =1id =yk0wline =2column =1file =1>
< tree node =file_input_element_listtype =4domain =1id =yk0vline =2column =1file =1
< tree node =file_input_element_listtype =4domain =1id =yjwwline =2column =1file =1>
< tree node =file_input_element_listtype =4domain =1id =yjvcline =2column =1file =1>
< tree node =file_input_element_listtype =4domain =1id =yjusline =2column =1file =1
< tree node =file_input_element_listtype =3domain =1id =ydbyline =2column =1file =1/>
< tree node =file_input_elementtype =5domain =1id =yjuqline =2column =1file =1>
< tree node =NEWLINEtype =282domain =1id =ydbnliteral =0line =2column =1file =1>
< precomment child =0index =1>#标题中的注释< / precomment>
< / tree>
< / tree>
< / tree>
< tree node =file_input_elementtype =6domain =1id =yjvbline =3column =1file =1>
< tree node =stmttype =7domain =1id =yjvaline =3column =1file =1>
< tree node =simple_stmttype =9domain =1id =yjv9line =3column =1file =1>
< tree node =small_stmt_listtype =11domain =1id =yjv3line =3column =1file =1>
< tree node =small_stmttype =45domain =1id =yjv1line =3column =1file =1>
< tree node ='import'type =305domain =1id =yjupliteral =0line =3column =1file =1 >
< tree node =dotted_as_name_listtype =53domain =1id =yjuzline =3column =8file =1>
< tree node =dotted_as_nametype =60domain =1id =yjuyline =3column =8file =1>
< tree node =dotted_nametype =61domain =1id =yjuxline =3column =8file =1>
< tree node =NAMEtype =310domain =1id =yjuvline =3column =8file =1>
< literal> sys< / literal>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node =NEWLINEtype =282domain =1id =yjuwliteral =0line =3column =11file =1/>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node =file_input_elementtype =6domain =1id =yjwvline =5column =1file =1>
< tree node =stmttype =7domain =1id =yjwuline =5column =1file =1>
< tree node =simple_stmttype =9domain =1id =yjwtline =5column =1file =1>
< tree node =small_stmt_listtype =11domain =1id =yjwoline =5column =1file =1>
< tree node =small_stmttype =14domain =1id =yjwlline =5column =1file =1>
< tree node =assign_stmttype =15domain =1id =yjwjline =5column =1file =1>
< tree node =target_listtype =215domain =1id =yjvgline =5column =1file =1>
< tree node =targetstype =217domain =1id =yjvfline =5column =1file =1>
< tree node =targettype =221domain =1id =yjveline =5column =1file =1>
< tree node =NAMEtype =310domain =1id =yjv8line =5column =1file =1>
< literal> TOKENBLANKS< / literal>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node ='='type =284domain =1id =yjvdliteral =0line =5column =12file =1 >
< tree node =assign_rhstype =29domain =1id =yjwhline =5column =13file =1>
< tree node =test_listtype =30domain =1id =yjwfline =5column =13file =1>
< tree node =teststype =32domain =1id =yjwcline =5column =13file =1>
< tree node =testtype =151domain =1id =yjwaline =5column =13file =1>
< tree node =or_testtype =152domain =1id =yjw8line =5column =13file =1>
< tree node =and_testtype =154domain =1id =yjw4line =5column =13file =1>
< tree node =not_testtype =157domain =1id =yjw1line =5column =13file =1>
< tree node =comparisontype =158domain =1id =yjvzline =5column =13file =1>
< tree node =exprtype =170domain =1id =yjvxline =5column =13file =1>
< tree node =xor_exprtype =172domain =1id =yjvvline =5column =13file =1>
< tree node =and_exprtype =174domain =1id =yjvsline =5column =13file =1>
< tree node =shift_exprtype =176domain =1id =yjvqline =5column =13file =1>
< tree node =arith_exprtype =179domain =1id =yjvoline =5column =13file =1>
< tree node =termtype =182domain =1id =yjvnline =5column =13file =1>
< tree node =factortype =187domain =1id =yjvmline =5column =13file =1>
< tree node =powertype =191domain =1id =yjvlline =5column =13file =1>
< tree node =valuetype =194domain =1id =yjvkline =5column =13file =1>
< tree node =constanttype =197domain =1id =yjvjline =5column =13file =1>
< tree node =INTEGERtype =355domain =1id =yjvhliteral =1line =5column =13file =1/>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node =NEWLINEtype =282domain =1id =yjviliteral =0line =5column =14file =1/>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node =file_input_elementtype =6domain =1id =yk0uline =7column =1file =1
< tree node =stmttype =8domain =1id =yk0pline =7column =1file =1>
< tree node =compound_stmttype =143domain =1id =yk0sline =7column =1file =1>
< tree node =decoratorstype =144domain =1id =yjwxline =7column =1file =1/>
< tree node ='class'type =330domain =1id =yjwsliteral =0line =7column =1file =1 >
< tree node =NAMEtype =310domain =1id =yjwyline =7column =7file =1>
< literal> MyClassNameTranslator< / literal>
< / tree>
< tree node =':'type =314domain =1id =yjwzliteral =0line =7column =28file =1 >
< tree node =blocktype =115domain =1id =yk0qline =7column =29file =1>
< tree node =NEWLINEtype =282domain =1id =yjx0literal =0line =7column =29file =1/>
< tree node =INDENTtype =324domain =1id =yjx1literal =0line =10column =1file =1>
< precomment child =0index =1>#get_name查找名称< / precomment>
< / tree>
< tree node =stmt_listtype =116domain =1id =yk0oline =10column =5file =1>
< tree node =stmttype =8domain =1id =yk0jline =10column =5file =1>
< tree node =compound_stmttype =119domain =1id =yk0mline =10column =5file =1>
< tree node =decoratorstype =144domain =1id =yjx5line =10column =5file =1/>
< tree node ='def'type =326domain =1id =yjx4literal =0line =10column =5file =1 >
< tree node =NAMEtype =310domain =1id =yjx6line =10column =9file =1>
< literal> get_name< / literal>
< / tree>
< tree node =parameterstype =121domain =1id =yjxeline =10column =17file =1>
< tree node ='('type =327domain =1id =yjx7literal =0line =10column =17file =1 >
< tree node =optional_varargslisttype =123domain =1id =yjxdline =10column =18file =1>
< tree node =varargslisttype =126domain =1id =yjw6line =10column =18file =1>
< tree node = fpdef_test_list_prefixtype =131domain =1id =yjxkline =10column =18file =1>
< tree node =fpdef_test_list_prefixtype =130 domain =1id =yjx9line =10column =18file =1/>
< tree node =fpdef_test_commatype =132domain = id =yjxhline =10column =18file =1>
< tree node =fpdef_testtype =133domain =1id =yjxc line =10column =18file =1>
< tree node =fpdeftype =135domain =1id =yjxbline = =18file =1>
< tree node =NAMEtype =310domain =1id =yjx8line =10column =18file = 1>
< literal> self< / literal>
< / tree>
< / tree>
< / tree>
< tree node =','type =297domain =1id =yjxaliteral =0line =10column =22file =1 >
< / tree>
< / tree>
< tree node =fpdef_testtype =133domain =1id =yjw3line =10column =24file =1>
< tree node =fpdeftype =135domain =1id =yjvwline =10column =24file =1>
< tree node =NAMEtype =310domain =1id =yjxgline =10column =24file =1>
< literal> name< / literal>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node =')'type =328domain =1id =yjvtliteral =0line =10column =28file =1 >
< / tree>
< tree node =':'type =314domain =1id =yjxlliteral =0line =10column =29file =1 >
< tree node =blocktype =115domain =1id =yk0kline =10column =30file =1
< tree node =NEWLINEtype =282domain =1id =yjxfliteral =0line =10column =30file =1/>
< tree node =INDENTtype =324domain =1id =yjxmliteral =0line =11column =1file =1/>
< tree node =stmt_listtype =117domain =1id =yk0hline =11column =9file =1>
< tree node =stmt_listtype =116domain =1id =yjyqline =11column =9file =1>
< tree node =stmttype =7domain =1id =yjypline =11column =9file =1>
< tree node =simple_stmttype =9domain =1id =yjyoline =11column =9file =1>
< tree node =small_stmt_listtype =11domain =1id =yjykline =11column =9file =1>
< tree node =small_stmttype =13domain =1id =yjyhline =11column =9file =1>
< tree node =testlisttype =255domain =1id =yjyfline =11column =9file =1>
< tree node =test_plustype =256domain =1id =yjydline =11column =9file =1>
< tree node =testtype =151domain =1id =yjyaline =11column =9file =1>
< tree node =or_testtype =152domain =1id =yjy7line =11column =9file =1>
< tree node =and_testtype =154domain =1id =yjy5line =11column =9file =1>
< tree node =not_testtype =157domain =1id =yjy3line =11column =9file =1>
< tree node =comparisontype =158domain =1id =yjy1line =11column =9file =1>
< tree node =exprtype =170domain =1id =yjxyline =11column =9file =1>
< tree node =xor_exprtype =172domain =1id =yjxwline =11column =9file =1>
< tree node =and_exprtype =174domain =1id =yjxvline =11column =9file =1>
< tree node =shift_exprtype =176domain =1id =yjxuline =11column =9file =1>
< tree node =arith_exprtype =179domain =1id =yjxtline =11column =9file =1>
< tree node =termtype =182domain =1id =yjxsline =11column =9file =1>
< tree node =factortype =187domain =1id =yjxrline =11column =9file =1>
< tree node =powertype =191domain =1id =yjxqline =11column =9file =1>
< tree node =valuetype =194domain =1id =yjxpline =11column =9file =1>
< tree node =constanttype =200domain =1id =yjxoline =11column =9file =1>
< tree node =string_sequencetype =208domain =1id =yjxjline =11column =9file =1>
< tree node =STRINGtype =362domain =1id =yjxnline =11column =9file =1>
< literal>获取实名的翻译< / literal>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node =NEWLINEtype =282domain =1id =yjxiliteral =0line =11column =48file =1/>
< / tree>
< / tree>
< / tree>
< tree node =stmttype =7domain =1id =yk0gline =12column =9file =1>
< tree node =simple_stmttype =9domain =1id =yk0fline =12column =9file =1>
< tree node =small_stmt_listtype =11domain =1id =yk09line =12column =9file =1>
< tree node =small_stmttype =39domain =1id =yk06line =12column =9file =1>
< tree node ='return'type =302domain =1id =yjynliteral =0line =12column =9file =1 >
< tree node =testlisttype =255domain =1id =yk03line =12column =16file =1>
< tree node =test_plustype =256domain =1id =yk02line =12column =16file =1>
< tree node =testtype =151domain =1id =yk01line =12column =16file =1>
< tree node =or_testtype =152domain =1id =yk00line =12column =16file =1>
< tree node =and_testtype =154domain =1id =yjzzline =12column =16file =1>
< tree node =not_testtype =157domain =1id =yjzyline =12column =16file =1>
< tree node =comparisontype =158domain =1id =yjzxline =12column =16file =1>
< tree node =exprtype =170domain =1id =yjzwline =12column =16file =1>
< tree node =xor_exprtype =172domain =1id =yjzvline =12column =16file =1>
< tree node =and_exprtype =174domain =1id =yjzuline =12column =16file =1>
< tree node =shift_exprtype =176domain =1id =yjztline =12column =16file =1>
< tree node =arith_exprtype =179domain =1id =yjzsline =12column =16file =1>
< tree node =termtype =182domain =1id =yjzrline =12column =16file =1>
< tree node =factortype =187domain =1id =yjzqline =12column =16file =1
< tree node =powertype =191domain =1id =yjzpline =12column =16file =1>
< tree node =valuetype =195domain =1id =yjzoline =12column =16file =1>
< tree node =valuetype =195domain =1id =yjz0line =12column =16file =1>
< tree node =valuetype =193domain =1id =yjyuline =12column =16file =1>
< tree node =atomtype =207domain =1id =yjytline =12column =16file =1>
< tree node =NAMEtype =310domain =1id =yjyrline =12column =16file =1>
< literal> self< / literal>
< / tree>
< / tree>
< / tree>
< tree node =trailertype =228domain =1id =yjyzline =12column =20file =1>
< tree node ='。'type =312domain =1id =yjysliteral =0line =12column =20file =1 >
< tree node =NAMEtype =310domain =1id =yjyvline =12column =21file =1>
< literal> realnames< / literal>
< / tree>
< / tree>
< / tree>
< tree node =trailertype =227domain =1id =yjznline =12column =30file =1>
< tree node =indextype =230domain =1id =yjzmline =12column =30file =1>
< tree node ='['type =358domain =1id =yjyyliteral =0line =12column =30file =1 >
< tree node =subscript_listtype =234domain =1id =yjzjline =12column =31file =1>
< tree node =subscripttype =249domain =1id =yjziline =12column =31file =1>
< tree node =testtype =151domain =1id =yjzhline =12column =31file =1>
< tree node =or_testtype =152domain =1id =yjzgline =12column =31file =1
< tree node =and_testtype =154domain =1id =yjzfline =12column =31file =1>
< tree node =not_testtype =157domain =1id =yjzeline =12column =31file =1>
< tree node =comparisontype =158domain =1id =yjzdline =12column =31file =1
< tree node =exprtype =170domain =1id =yjzcline =12column =31file =1>
< tree node =xor_exprtype =172domain =1id =yjzbline =12column =31file =1>
< tree node =and_exprtype =174domain =1id =yjzaline =12column =31file =1>
< tree node =shift_exprtype =176domain =1id =yjz9line =12column =31file =1>
< tree node =arith_exprtype =179domain =1id =yjz8line =12column =31file =1>
< tree node =termtype =182domain =1id =yjz7line =12column =31file =1>
< tree node =factortype =187domain =1id =yjz6line =12column =31file =1>
< tree node =powertype =191domain =1id =yjz5line =12column =31file =1>
< tree node =valuetype =193domain =1id =yjz4line =12column =31file =1>
< tree node =atomtype =207domain =1id =yjz3line =12column =31file =1>
< tree node =NAMEtype =310domain =1id =yjz1line =12column =31file =1>
< literal> name< / literal>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node =']'type =359domain =1id =yjz2literal =0line =12column =35file =1 >
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node =NEWLINEtype =282domain =1id =yjzlliteral =0line =12column =36file =1/>
< / tree>
< / tree>
< / tree>
< tree node =DEDENTtype =325domain =1id =yk0eliteral =0line =14column =1file =1/>
< / tree>
< / tree>
< / tree>
< / tree>
< tree node =DEDENTtype =325domain =1id =yk0iliteral =0line =14column =1file =1/>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< / tree>
< FileIndex>
< File index =1> C:/DMS/Domains/Python/v2_6/Examples/tiny.py< / File>
< / FileIndex>
< DomainIndex>
< Domain index =1> Python〜v3_0< / Domain>
< / DomainIndex>
< / DMSForest>


I have done fair bit of searching around how to capture python ASTs with comments preserved. The suggested way includes using ast and tokenize libraries to get the job done.

I have had fair bit of success in utilizing these libraries as per my requirement but I feel there has to be a better way.

This thought stems from the fact that lib2to3 converts python2 code to python3 code with comments preserved. Also the process is stated to be Source-Code-in-Python2 -> AST -> Source-Code-in-Python3 (to be put forth in a simplified manner).

My question is how do I capture the in-between AST? I have looked at python-docs but there is no command line flag to get hold of the AST.

Just to provide you the context: I am trying to convert python source code to an XML file (with comments preserved) for some further processing

解决方案

Just to provide you the context: I am trying to convert python source code to an XML file (with comments preserved) for some further processing

An "easy" way is to use a tool that already does this, rather than reinventing it, especially if you are short on time.

Our DMS Software Reengineering Toolkit can parse Python (and many other languages), build ASTs, and capture comments, and spit out the resulting tree as XML. See example below.

A remark: XML initially seems nice, but is a clumsy way to represent/analyze/transform code. The reason that tools like DMS exist, is to provide all the machinery necessary to manipulate the parsed ASTs in ways that are more effective than XML transformation, and scale much better (e.g., to millions of lines of code): ultimately, to save engineering time and runtime.

Even if you decide on XML, where are you going to get good tools to process it? (XSLT isn't the right answer). Finally, if you intend to modify the program, and you change the XML, how do you intend to get the source code back? DMS can modify ASTs and regenerate valid source program text (including the comments).

So while DMS will export ASTs in XML (because people like you seem to insist on it), this feature is rarely used in practice. The typical use case is parse, analyze, modify the AST, then prettyprint the modified AST, all using DMS in an integrated way.

For this python program:

# A comment in the header

import sys

TOKENBLANKS=1

class MyClassNameTranslator:

    # get_name looks up name
    def get_name(self, name):
        """Get a translation for a real name"""
        return self.realnames[name]

DMS generates the follow XML version of its AST, complete with captured comments:

C:\[snip]Python\Tools\Parser>run ..\domainparser ++XML C:\[snip]tiny.py

Python~v3_0 Domain Parser Version 2.5.15
Copyright (C) 1996-2013 Semantic Designs, Inc; All Rights Reserved; SD Confidential
Powered by DMS (R) Software Reengineering Toolkit
165 tree nodes in tree.
<?xml version="1.1" encoding="ISO-8859-1"?>
<!-- Using DMS PrintASTasXML (v.1.03) -->
<!-- XML generated on 2014/03/01 12:14:49 -->
<DMSForest>
  <tree node="Python" type="1" domain="1" id="yk0x" parents="0" line="2" column="1" file="1">
<tree node="file_input" type="2" domain="1" id="yk0w" line="2" column="1" file="1">
  <tree node="file_input_element_list" type="4" domain="1" id="yk0v" line="2" column="1" file="1">
    <tree node="file_input_element_list" type="4" domain="1" id="yjww" line="2" column="1" file="1">
      <tree node="file_input_element_list" type="4" domain="1" id="yjvc" line="2" column="1" file="1">
    <tree node="file_input_element_list" type="4" domain="1" id="yjus" line="2" column="1" file="1">
      <tree node="file_input_element_list" type="3" domain="1" id="ydby" line="2" column="1" file="1"/>
      <tree node="file_input_element" type="5" domain="1" id="yjuq" line="2" column="1" file="1">
        <tree node="NEWLINE" type="282" domain="1" id="ydbn" literal="0" line="2" column="1" file="1">
          <precomment child="0" index="1"># A comment in the header</precomment>
        </tree>
      </tree>
    </tree>
    <tree node="file_input_element" type="6" domain="1" id="yjvb" line="3" column="1" file="1">
      <tree node="stmt" type="7" domain="1" id="yjva" line="3" column="1" file="1">
        <tree node="simple_stmt" type="9" domain="1" id="yjv9" line="3" column="1" file="1">
          <tree node="small_stmt_list" type="11" domain="1" id="yjv3" line="3" column="1" file="1">
        <tree node="small_stmt" type="45" domain="1" id="yjv1" line="3" column="1" file="1">
          <tree node="'import'" type="305" domain="1" id="yjup" literal="0" line="3" column="1" file="1"/>
          <tree node="dotted_as_name_list" type="53" domain="1" id="yjuz" line="3" column="8" file="1">
            <tree node="dotted_as_name" type="60" domain="1" id="yjuy" line="3" column="8" file="1">
              <tree node="dotted_name" type="61" domain="1" id="yjux" line="3" column="8" file="1">
            <tree node="NAME" type="310" domain="1" id="yjuv" line="3" column="8" file="1">
              <literal>sys</literal>
            </tree>
              </tree>
            </tree>
          </tree>
        </tree>
          </tree>
          <tree node="NEWLINE" type="282" domain="1" id="yjuw" literal="0" line="3" column="11" file="1"/>
        </tree>
      </tree>
    </tree>
      </tree>
      <tree node="file_input_element" type="6" domain="1" id="yjwv" line="5" column="1" file="1">
    <tree node="stmt" type="7" domain="1" id="yjwu" line="5" column="1" file="1">
      <tree node="simple_stmt" type="9" domain="1" id="yjwt" line="5" column="1" file="1">
        <tree node="small_stmt_list" type="11" domain="1" id="yjwo" line="5" column="1" file="1">
          <tree node="small_stmt" type="14" domain="1" id="yjwl" line="5" column="1" file="1">
        <tree node="assign_stmt" type="15" domain="1" id="yjwj" line="5" column="1" file="1">
          <tree node="target_list" type="215" domain="1" id="yjvg" line="5" column="1" file="1">
            <tree node="targets" type="217" domain="1" id="yjvf" line="5" column="1" file="1">
              <tree node="target" type="221" domain="1" id="yjve" line="5" column="1" file="1">
            <tree node="NAME" type="310" domain="1" id="yjv8" line="5" column="1" file="1">
              <literal>TOKENBLANKS</literal>
            </tree>
              </tree>
            </tree>
          </tree>
          <tree node="'='" type="284" domain="1" id="yjvd" literal="0" line="5" column="12" file="1"/>
          <tree node="assign_rhs" type="29" domain="1" id="yjwh" line="5" column="13" file="1">
            <tree node="test_list" type="30" domain="1" id="yjwf" line="5" column="13" file="1">
              <tree node="tests" type="32" domain="1" id="yjwc" line="5" column="13" file="1">
            <tree node="test" type="151" domain="1" id="yjwa" line="5" column="13" file="1">
              <tree node="or_test" type="152" domain="1" id="yjw8" line="5" column="13" file="1">
                <tree node="and_test" type="154" domain="1" id="yjw4" line="5" column="13" file="1">
                  <tree node="not_test" type="157" domain="1" id="yjw1" line="5" column="13" file="1">
                <tree node="comparison" type="158" domain="1" id="yjvz" line="5" column="13" file="1">
                  <tree node="expr" type="170" domain="1" id="yjvx" line="5" column="13" file="1">
                    <tree node="xor_expr" type="172" domain="1" id="yjvv" line="5" column="13" file="1">
                      <tree node="and_expr" type="174" domain="1" id="yjvs" line="5" column="13" file="1">
                    <tree node="shift_expr" type="176" domain="1" id="yjvq" line="5" column="13" file="1">
                      <tree node="arith_expr" type="179" domain="1" id="yjvo" line="5" column="13" file="1">
                        <tree node="term" type="182" domain="1" id="yjvn" line="5" column="13" file="1">
                          <tree node="factor" type="187" domain="1" id="yjvm" line="5" column="13" file="1">
                        <tree node="power" type="191" domain="1" id="yjvl" line="5" column="13" file="1">
                          <tree node="value" type="194" domain="1" id="yjvk" line="5" column="13" file="1">
                            <tree node="constant" type="197" domain="1" id="yjvj" line="5" column="13" file="1">
                              <tree node="INTEGER" type="355" domain="1" id="yjvh" literal="1" line="5" column="13" file="1"/>
                            </tree>
                          </tree>
                        </tree>
                          </tree>
                        </tree>
                      </tree>
                    </tree>
                      </tree>
                    </tree>
                  </tree>
                </tree>
                  </tree>
                </tree>
              </tree>
            </tree>
              </tree>
            </tree>
          </tree>
        </tree>
          </tree>
        </tree>
        <tree node="NEWLINE" type="282" domain="1" id="yjvi" literal="0" line="5" column="14" file="1"/>
      </tree>
    </tree>
      </tree>
    </tree>
    <tree node="file_input_element" type="6" domain="1" id="yk0u" line="7" column="1" file="1">
      <tree node="stmt" type="8" domain="1" id="yk0p" line="7" column="1" file="1">
    <tree node="compound_stmt" type="143" domain="1" id="yk0s" line="7" column="1" file="1">
      <tree node="decorators" type="144" domain="1" id="yjwx" line="7" column="1" file="1"/>
      <tree node="'class'" type="330" domain="1" id="yjws" literal="0" line="7" column="1" file="1"/>
      <tree node="NAME" type="310" domain="1" id="yjwy" line="7" column="7" file="1">
        <literal>MyClassNameTranslator</literal>
      </tree>
      <tree node="':'" type="314" domain="1" id="yjwz" literal="0" line="7" column="28" file="1"/>
      <tree node="block" type="115" domain="1" id="yk0q" line="7" column="29" file="1">
        <tree node="NEWLINE" type="282" domain="1" id="yjx0" literal="0" line="7" column="29" file="1"/>
        <tree node="INDENT" type="324" domain="1" id="yjx1" literal="0" line="10" column="1" file="1">
          <precomment child="0" index="1"># get_name looks up name</precomment>
        </tree>
        <tree node="stmt_list" type="116" domain="1" id="yk0o" line="10" column="5" file="1">
          <tree node="stmt" type="8" domain="1" id="yk0j" line="10" column="5" file="1">
        <tree node="compound_stmt" type="119" domain="1" id="yk0m" line="10" column="5" file="1">
          <tree node="decorators" type="144" domain="1" id="yjx5" line="10" column="5" file="1"/>
          <tree node="'def'" type="326" domain="1" id="yjx4" literal="0" line="10" column="5" file="1"/>
          <tree node="NAME" type="310" domain="1" id="yjx6" line="10" column="9" file="1">
            <literal>get_name</literal>
          </tree>
          <tree node="parameters" type="121" domain="1" id="yjxe" line="10" column="17" file="1">
            <tree node="'('" type="327" domain="1" id="yjx7" literal="0" line="10" column="17" file="1"/>
            <tree node="optional_varargslist" type="123" domain="1" id="yjxd" line="10" column="18" file="1">
              <tree node="varargslist" type="126" domain="1" id="yjw6" line="10" column="18" file="1">
            <tree node="fpdef_test_list_prefix" type="131" domain="1" id="yjxk" line="10" column="18" file="1">
              <tree node="fpdef_test_list_prefix" type="130" domain="1" id="yjx9" line="10" column="18" file="1"/>
              <tree node="fpdef_test_comma" type="132" domain="1" id="yjxh" line="10" column="18" file="1">
                <tree node="fpdef_test" type="133" domain="1" id="yjxc" line="10" column="18" file="1">
                  <tree node="fpdef" type="135" domain="1" id="yjxb" line="10" column="18" file="1">
                <tree node="NAME" type="310" domain="1" id="yjx8" line="10" column="18" file="1">
                  <literal>self</literal>
                </tree>
                  </tree>
                </tree>
                <tree node="','" type="297" domain="1" id="yjxa" literal="0" line="10" column="22" file="1"/>
              </tree>
            </tree>
            <tree node="fpdef_test" type="133" domain="1" id="yjw3" line="10" column="24" file="1">
              <tree node="fpdef" type="135" domain="1" id="yjvw" line="10" column="24" file="1">
                <tree node="NAME" type="310" domain="1" id="yjxg" line="10" column="24" file="1">
                  <literal>name</literal>
                </tree>
              </tree>
            </tree>
              </tree>
            </tree>
            <tree node="')'" type="328" domain="1" id="yjvt" literal="0" line="10" column="28" file="1"/>
          </tree>
          <tree node="':'" type="314" domain="1" id="yjxl" literal="0" line="10" column="29" file="1"/>
          <tree node="block" type="115" domain="1" id="yk0k" line="10" column="30" file="1">
            <tree node="NEWLINE" type="282" domain="1" id="yjxf" literal="0" line="10" column="30" file="1"/>
            <tree node="INDENT" type="324" domain="1" id="yjxm" literal="0" line="11" column="1" file="1"/>
            <tree node="stmt_list" type="117" domain="1" id="yk0h" line="11" column="9" file="1">
              <tree node="stmt_list" type="116" domain="1" id="yjyq" line="11" column="9" file="1">
            <tree node="stmt" type="7" domain="1" id="yjyp" line="11" column="9" file="1">
              <tree node="simple_stmt" type="9" domain="1" id="yjyo" line="11" column="9" file="1">
                <tree node="small_stmt_list" type="11" domain="1" id="yjyk" line="11" column="9" file="1">
                  <tree node="small_stmt" type="13" domain="1" id="yjyh" line="11" column="9" file="1">
                <tree node="testlist" type="255" domain="1" id="yjyf" line="11" column="9" file="1">
                  <tree node="test_plus" type="256" domain="1" id="yjyd" line="11" column="9" file="1">
                    <tree node="test" type="151" domain="1" id="yjya" line="11" column="9" file="1">
                      <tree node="or_test" type="152" domain="1" id="yjy7" line="11" column="9" file="1">
                    <tree node="and_test" type="154" domain="1" id="yjy5" line="11" column="9" file="1">
                      <tree node="not_test" type="157" domain="1" id="yjy3" line="11" column="9" file="1">
                        <tree node="comparison" type="158" domain="1" id="yjy1" line="11" column="9" file="1">
                          <tree node="expr" type="170" domain="1" id="yjxy" line="11" column="9" file="1">
                        <tree node="xor_expr" type="172" domain="1" id="yjxw" line="11" column="9" file="1">
                          <tree node="and_expr" type="174" domain="1" id="yjxv" line="11" column="9" file="1">
                            <tree node="shift_expr" type="176" domain="1" id="yjxu" line="11" column="9" file="1">
                              <tree node="arith_expr" type="179" domain="1" id="yjxt" line="11" column="9" file="1">
                            <tree node="term" type="182" domain="1" id="yjxs" line="11" column="9" file="1">
                              <tree node="factor" type="187" domain="1" id="yjxr" line="11" column="9" file="1">
                                <tree node="power" type="191" domain="1" id="yjxq" line="11" column="9" file="1">
                                  <tree node="value" type="194" domain="1" id="yjxp" line="11" column="9" file="1">
                                <tree node="constant" type="200" domain="1" id="yjxo" line="11" column="9" file="1">
                                  <tree node="string_sequence" type="208" domain="1" id="yjxj" line="11" column="9" file="1">
                                    <tree node="STRING" type="362" domain="1" id="yjxn" line="11" column="9" file="1">
                                      <literal>Get a translation for a real name</literal>
                                    </tree>
                                  </tree>
                                </tree>
                                  </tree>
                                </tree>
                              </tree>
                            </tree>
                              </tree>
                            </tree>
                          </tree>
                        </tree>
                          </tree>
                        </tree>
                      </tree>
                    </tree>
                      </tree>
                    </tree>
                  </tree>
                </tree>
                  </tree>
                </tree>
                <tree node="NEWLINE" type="282" domain="1" id="yjxi" literal="0" line="11" column="48" file="1"/>
              </tree>
            </tree>
              </tree>
              <tree node="stmt" type="7" domain="1" id="yk0g" line="12" column="9" file="1">
            <tree node="simple_stmt" type="9" domain="1" id="yk0f" line="12" column="9" file="1">
              <tree node="small_stmt_list" type="11" domain="1" id="yk09" line="12" column="9" file="1">
                <tree node="small_stmt" type="39" domain="1" id="yk06" line="12" column="9" file="1">
                  <tree node="'return'" type="302" domain="1" id="yjyn" literal="0" line="12" column="9" file="1"/>
                  <tree node="testlist" type="255" domain="1" id="yk03" line="12" column="16" file="1">
                <tree node="test_plus" type="256" domain="1" id="yk02" line="12" column="16" file="1">
                  <tree node="test" type="151" domain="1" id="yk01" line="12" column="16" file="1">
                    <tree node="or_test" type="152" domain="1" id="yk00" line="12" column="16" file="1">
                      <tree node="and_test" type="154" domain="1" id="yjzz" line="12" column="16" file="1">
                    <tree node="not_test" type="157" domain="1" id="yjzy" line="12" column="16" file="1">
                      <tree node="comparison" type="158" domain="1" id="yjzx" line="12" column="16" file="1">
                        <tree node="expr" type="170" domain="1" id="yjzw" line="12" column="16" file="1">
                          <tree node="xor_expr" type="172" domain="1" id="yjzv" line="12" column="16" file="1">
                        <tree node="and_expr" type="174" domain="1" id="yjzu" line="12" column="16" file="1">
                          <tree node="shift_expr" type="176" domain="1" id="yjzt" line="12" column="16" file="1">
                            <tree node="arith_expr" type="179" domain="1" id="yjzs" line="12" column="16" file="1">
                              <tree node="term" type="182" domain="1" id="yjzr" line="12" column="16" file="1">
                            <tree node="factor" type="187" domain="1" id="yjzq" line="12" column="16" file="1">
                              <tree node="power" type="191" domain="1" id="yjzp" line="12" column="16" file="1">
                                <tree node="value" type="195" domain="1" id="yjzo" line="12" column="16" file="1">
                                  <tree node="value" type="195" domain="1" id="yjz0" line="12" column="16" file="1">
                                <tree node="value" type="193" domain="1" id="yjyu" line="12" column="16" file="1">
                                  <tree node="atom" type="207" domain="1" id="yjyt" line="12" column="16" file="1">
                                    <tree node="NAME" type="310" domain="1" id="yjyr" line="12" column="16" file="1">
                                      <literal>self</literal>
                                    </tree>
                                  </tree>
                                </tree>
                                <tree node="trailer" type="228" domain="1" id="yjyz" line="12" column="20" file="1">
                                  <tree node="'.'" type="312" domain="1" id="yjys" literal="0" line="12" column="20" file="1"/>
                                  <tree node="NAME" type="310" domain="1" id="yjyv" line="12" column="21" file="1">
                                    <literal>realnames</literal>
                                  </tree>
                                </tree>
                                  </tree>
                                  <tree node="trailer" type="227" domain="1" id="yjzn" line="12" column="30" file="1">
                                <tree node="index" type="230" domain="1" id="yjzm" line="12" column="30" file="1">
                                  <tree node="'['" type="358" domain="1" id="yjyy" literal="0" line="12" column="30" file="1"/>
                                  <tree node="subscript_list" type="234" domain="1" id="yjzj" line="12" column="31" file="1">
                                    <tree node="subscript" type="249" domain="1" id="yjzi" line="12" column="31" file="1">
                                      <tree node="test" type="151" domain="1" id="yjzh" line="12" column="31" file="1">
                                    <tree node="or_test" type="152" domain="1" id="yjzg" line="12" column="31" file="1">
                                      <tree node="and_test" type="154" domain="1" id="yjzf" line="12" column="31" file="1">
                                        <tree node="not_test" type="157" domain="1" id="yjze" line="12" column="31" file="1">
                                          <tree node="comparison" type="158" domain="1" id="yjzd" line="12" column="31" file="1">
                                        <tree node="expr" type="170" domain="1" id="yjzc" line="12" column="31" file="1">
                                          <tree node="xor_expr" type="172" domain="1" id="yjzb" line="12" column="31" file="1">
                                            <tree node="and_expr" type="174" domain="1" id="yjza" line="12" column="31" file="1">
                                              <tree node="shift_expr" type="176" domain="1" id="yjz9" line="12" column="31" file="1">
                                            <tree node="arith_expr" type="179" domain="1" id="yjz8" line="12" column="31" file="1">
                                              <tree node="term" type="182" domain="1" id="yjz7" line="12" column="31" file="1">
                                                <tree node="factor" type="187" domain="1" id="yjz6" line="12" column="31" file="1">
                                                  <tree node="power" type="191" domain="1" id="yjz5" line="12" column="31" file="1">
                                                <tree node="value" type="193" domain="1" id="yjz4" line="12" column="31" file="1">
                                                  <tree node="atom" type="207" domain="1" id="yjz3" line="12" column="31" file="1">
                                                    <tree node="NAME" type="310" domain="1" id="yjz1" line="12" column="31" file="1">
                                                      <literal>name</literal>
                                                    </tree>
                                                  </tree>
                                                </tree>
                                                  </tree>
                                                </tree>
                                              </tree>
                                            </tree>
                                              </tree>
                                            </tree>
                                          </tree>
                                        </tree>
                                          </tree>
                                        </tree>
                                      </tree>
                                    </tree>
                                      </tree>
                                    </tree>
                                  </tree>
                                  <tree node="']'" type="359" domain="1" id="yjz2" literal="0" line="12" column="35" file="1"/>
                                </tree>
                                  </tree>
                                </tree>
                              </tree>
                            </tree>
                              </tree>
                            </tree>
                          </tree>
                        </tree>
                          </tree>
                        </tree>
                      </tree>
                    </tree>
                      </tree>
                    </tree>
                  </tree>
                </tree>
                  </tree>
                </tree>
              </tree>
              <tree node="NEWLINE" type="282" domain="1" id="yjzl" literal="0" line="12" column="36" file="1"/>
            </tree>
              </tree>
            </tree>
            <tree node="DEDENT" type="325" domain="1" id="yk0e" literal="0" line="14" column="1" file="1"/>
          </tree>
        </tree>
          </tree>
        </tree>
        <tree node="DEDENT" type="325" domain="1" id="yk0i" literal="0" line="14" column="1" file="1"/>
      </tree>
    </tree>
      </tree>
    </tree>
  </tree>
</tree>
  </tree>
  <FileIndex>
<File index="1">C:/DMS/Domains/Python/v2_6/Examples/tiny.py</File>
  </FileIndex>
  <DomainIndex>
<Domain index="1">Python~v3_0</Domain>
  </DomainIndex>
</DMSForest>

这篇关于简单的方法来转换python源代码到AST与评论完好无损的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆