【问题标题】:How to output all unassigned strings in a python file如何在python文件中输出所有未分配的字符串
【发布时间】:2016-08-30 03:19:17
【问题描述】:

我有一个如下所示的 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 类有关的文本

【问题讨论】:

    标签: python string unassigned-variable


    【解决方案1】:

    假设您在问题中指定的格式,应该这样做:

    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)
    

    【讨论】:

    • 谢谢!我对您的代码进行了一些修改以自行打开,这可以解决问题。
    【解决方案2】:

    使用正则表达式:

    $ 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 Demo

    理想的方法是使用 ast 模块,解析整个文档,然后在 ast.FunctionDef、ast.ClassDef 或 ast.Module 类型的所有节点上打印调用 ast.get_docstring。但是,您的 cmets 不是文档字符串。如果文件是这样的:

    $ 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
    

    【讨论】:

      猜你喜欢
      • 2014-07-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多