繁体   English   中英

如何从单独的脚本访问文档字符串?

[英]How to access a docstring from a separate script?

为用户构建他们想要运行的 select Python 脚本的 GUI。 每个脚本都有自己的文档字符串来解释脚本的输入和输出。 一旦他们突出显示了脚本,但没有选择运行它,我想在 UI 中显示该信息,而且我似乎无法从基本程序访问文档字符串。

前任。

test.py

"""this is a docstring"""
print('hello world')

program.py

在本例中indextest.py ,但通常不知道,因为它是用户在 GUI 中选择的任何内容。

# index is test.py
def on_selected(self, index):
    script_path = self.tree_view_model.filePath(index)
    fparse = ast.parse(''.join(open(script_path)))
    self.textBrowser_description.setPlainText(ast.get_docstring(fparse))

让我们要访问的文档字符串属于文件file.py

您可以通过执行以下操作获取文档字符串:

import file
print(file.__doc__)

如果您想在导入之前获取文档字符串,那么您可以读取文件并提取文档字符串。 这是一个例子:

import re


def get_docstring(file)
    with open(file, "r") as f:
        content = f.read()  # read file
        quote = content[0]  # get type of quote
        pattern = re.compile(rf"^{quote}{quote}{quote}[^{quote}]*{quote}{quote}{quote}")  # create docstring pattern
    return re.findall(pattern, content)[0][3:-3]  # return docstring without quotes


print(get_docstring("file.py"))

注意:要使此正则表达式起作用,文档字符串需要位于最顶部。

这是通过importlib获取它的方法。 大部分逻辑都放在了 function 中。 请注意,使用importlib确实会导入脚本(这会导致执行其所有顶级语句),但是当 function 返回时,模块本身会被丢弃。

如果这是我想从中获取文档字符串的当前目录中的脚本docstring_test.py

""" this is a multiline
    docstring.
"""
print('hello world')

这是如何做到的:

import importlib.util

def get_docstring(script_name, script_path):
    spec = importlib.util.spec_from_file_location(script_name, script_path)
    foo = importlib.util.module_from_spec(spec)
    spec.loader.exec_module(foo)
    return foo.__doc__

if __name__ == '__main__':

    print(get_docstring('docstring_test', "./docstring_test.py"))

Output:

hello world
 this is a multiline
    docstring.

更新:

这是通过让标准库中的ast模块进行解析来避免导入/执行脚本以及尝试使用正则表达式自己解析它的方法。

这看起来或多或少等同于您的问题,因此尚不清楚为什么您所拥有的对您不起作用。

import ast

def get_docstring(script_path):
    with open(script_path, 'r') as file:
        tree = ast.parse(file.read())
        return ast.get_docstring(tree, clean=False)

if __name__ == '__main__':

    print(repr(get_docstring('./docstring_test.py')))

Output:

' this is a multiline\n    docstring.\n'

暂无
暂无

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

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