小编baa*_*ezx的帖子

令牌化模块中的Python 2换行标记

tokenize在Python中使用该模块,并想知道为什么有2个不同的换行符号:

NEWLINE = 4
NL = 54
Run Code Online (Sandbox Code Playgroud)

任何产生两个令牌的代码示例都将受到赞赏.

python tokenize

7
推荐指数
1
解决办法
809
查看次数

我怎么会打印python文档字符串?

我有一个python文件,其原始字符串作为docstrings.

def a():
    '\n\tthis\n\tis\n\tthe docstring.\n\t'
    print 'hello world'
Run Code Online (Sandbox Code Playgroud)

我如何重写docstring看起来像

def a():
    """
    this
    is
    the docstring.
    """
    print 'hello world'
Run Code Online (Sandbox Code Playgroud)

python docstring

6
推荐指数
1
解决办法
1005
查看次数

Python ast 和分词器

tokenize在Python中,如何将模块与模块结合使用ast?两者都使用不同的节点类型,您如何将两者关联在一起?有办法吗?可能是parser模块?

python parsing abstract-syntax-tree tokenize

5
推荐指数
0
解决办法
1127
查看次数

ast 模块使用什么类型的树遍历?

使用什么类型的树遍历ast(特别是ast.NodeVisitor())?当我创建一个堆栈并将遍历的每个节点推入堆栈时,结果似乎是“广度优先”树遍历。这意味着顺序取决于树中的级别。

前任。树看起来像

Module
  Assign
    Name
      Store
    Call
      Attribute
        Str
        Load
Run Code Online (Sandbox Code Playgroud)

堆栈看起来像

[Module,Assign,Name,Call,Store,Attribute,Str,Load]
Run Code Online (Sandbox Code Playgroud)

前任。代码

stack = []
class a(ast.NodeTransformer):
    def visit_Num(self,node):
        stack.append(node)
        ...
        return node

    ...                      #this is all the other visit_*() functions

    def visit_Str(self,node):
        stack.append(node)
        ...
        return node

if __name__ == "__main__":
    with open('some_file.py','r') as pt:
        tree = ast.parse(pt)
    new_tree = a()
    new_tree_edit = ast.fix_missing_locations(new_tree.visit(tree)) # I have tried with and without calling fix_missing_locations and got the same results.
    print stack
Run Code Online (Sandbox Code Playgroud)

python stack abstract-syntax-tree tree-traversal

4
推荐指数
1
解决办法
3321
查看次数

如何查看特定类型的对象是否在Python列表中?

如何检查Python列表中的特定对象类型?例如,我想ast在列表l中检查模块对象Subscript或_ast.Subscript:

if Subscript in l:
    return True
Run Code Online (Sandbox Code Playgroud)

但这还没有奏效.任何有关此问题的帮助将不胜感激.

对象也在0x7ff7f7f7f7ff>处打印为<_ast.Subscript对象

python list object

0
推荐指数
1
解决办法
95
查看次数