简体   繁体   English

使用grep或awk从python文件中提取文档字符串

[英]Extract docstring form a python file using grep or awk

I want to extract all docstrings from my python file using grep or awk. 我想使用grep或awk从我的python文件中提取所有文档字符串。 I tried 我试过了

cat test.py | grep """[\w\W]*?"""

But I see no output. 但是我看不到输出。 Say the the test test.py looks like this. 说测试test.py看起来像这样。

import libraries

class MyClass(object):
    """Docstring to this class. 
       second line of docstring."""

    def myClassMethod(a,b):
        """Docstring of the method. 
           another line in docstring of the method."""
        return a + b

Then the output should be all that is enclosed in triple quotes. 然后,输出应全部包含在三引号中。

"""Docstring to this class. 
second line of docstring."""
"""Docstring of the method. 
another line in docstring of the method."""

The proper way to extract docstrings from Python code is via actual Python parser (the ast module): 从Python代码中提取文档字符串的正确方法是通过实际的Python解析器( ast模块):

#!/usr/bin/env python
import ast

with open('/path/to/file') as f:
    code = ast.parse(f.read())

for node in ast.walk(code):
    if isinstance(node, (ast.FunctionDef, ast.ClassDef, ast.Module)):
        docstring = ast.get_docstring(node)
        if docstring:
            print(repr(docstring))

Run on your sample will output: 对您的示例运行将输出:

'Docstring to this class. \nsecond line of docstring.'
'Docstring of the method. \nanother line in docstring of the method.'

Just for fun, we can do also do it with GNU awk : 只是为了好玩,我们也可以使用GNU awk做到这一点:

$ awk -v RS= -v FPAT="'''.*'''|"'""".*"""' '{print $1}' file
"""Docstring to this class. 
       second line of docstring."""
"""Docstring of the method. 
           another line in docstring of the method."""

With P(perl) grep you can do the following: 使用P(perl)grep,您可以执行以下操作:

grep -Poz '"""[^"]+"""' test.py

Output: 输出:

"""Docstring to this class. 
       second line of docstring.""""""Docstring of the method. 
           another line in docstring of the method."""

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM