0

次のような python ファイル (スクリプト) があります。

script.py

"""
Multiline comment with unique
text pertaining to the Foo class
"""
class Foo():
    pass


"""
Multiline comment with unique
text pertaining to the Bar class
"""
class Bar():
    pass


"""
Multiline comment with unique
text pertaining to the FooBar class
"""
class FooBar():
    pass


def print_comments():
    # NotImplementedError 

print_comments割り当てられていないすべての文字列を検出して出力する方法はありますか?

Foo クラスに関する固有のテキストを含む複数行のコメント

Bar クラスに関連する一意のテキストを含む複数行のコメント

FooBar クラスに関する一意のテキストを含む複数行のコメント

4

2 に答える 2

2

あなたの質問で示したフォーマットを仮定すると、次のようなものがそれを行うはずです:

class Show_Script():
    def construct(self):
        with open(os.path.abspath(__file__)) as f:
            my_lines = f.readlines()

        comments = []
        in_comment = 0

        for line in my_lines:
            # detected the start of a comment
            if line.strip().startswith('"""') and in_comment == 0:
                in_comment = 1
                comments.append('')
            # detected the end of a comment
            elif line.strip().endswith('"""') and in_comment == 1:
                in_comment = 0
            # the contents of a comment
            elif in_comment == 1:
                comments[-1] += line

        print '\n'.join(comments)
于 2016-08-30T03:40:35.673 に答える
1

正規表現の使用:

$ cat script.py
from __future__ import print_function
import sys, re

"""
Multiline comment with unique
text pertaining to the Foo class
"""
class Foo():
    pass


"""
Multiline comment with unique
text pertaining to the Bar class
"""
class Bar():
    pass


"""
Multiline comment with unique
text pertaining to the FooBar class
"""
class FooBar():
    pass

def print_comments():
    with open(sys.argv[0]) as f:
        file_contents = f.read()

    map(print, re.findall(r'"""\n([^"""]*)"""', file_contents, re.S))

print_comments()
$ python script.py
Multiline comment with unique
text pertaining to the Foo class

Multiline comment with unique
text pertaining to the Bar class

Multiline comment with unique
text pertaining to the FooBar class

正規表現の説明:

"""\n([^"""]*)"""

正規表現の視覚化

Debuggex デモ

これを行う理想的な方法は、ast モジュールを使用し、ドキュメント全体を解析してから、タイプが ast.FunctionDef、ast.ClassDef、または ast.Module のすべてのノードで ast.get_docstring を出力することです。ただし、コメントはドキュメントストリングではありません。ファイルが次のようなものだったとします。

$ cat script.py

import sys, re, ast

class Foo():
    """
    Multiline comment with unique
    text pertaining to the Foo class
    """
    pass


class Bar():
    """
    Multiline comment with unique
    text pertaining to the Bar class
    """
    pass


class FooBar():
    """
    Multiline comment with unique
    text pertaining to the FooBar class
    """
    pass

def print_docstrings():
    with open(sys.argv[0]) as f:
        file_contents = f.read()

    tree = ast.parse(file_contents)
    class_nodes = filter((lambda x: type(x) in [ast.ClassDef, ast.FunctionDef, ast.Module]), ast.walk(tree))
    for node in class_nodes:
        doc_str = ast.get_docstring(node)
        if doc_str:
            print doc_str

print_docstrings()

$ python script.py
Multiline comment with unique
text pertaining to the Foo class
Multiline comment with unique
text pertaining to the Bar class
Multiline comment with unique
text pertaining to the FooBar class
于 2016-08-30T04:01:48.800 に答える