[英]How to access a docstring from a separate script?
Building a GUI for users to select Python scripts they want to run.为用户构建他们想要运行的 select Python 脚本的 GUI。 Each script has its own docstring explaining inputs and outputs for the script.
每个脚本都有自己的文档字符串来解释脚本的输入和输出。 I want to display that information in the UI once they've highlighted the script, but not selected to run it, and I can't seem to get access to the docstrings from the base program.
一旦他们突出显示了脚本,但没有选择运行它,我想在 UI 中显示该信息,而且我似乎无法从基本程序访问文档字符串。
ex.前任。
test.py
"""this is a docstring"""
print('hello world')
program.py
index
is test.py
for this example, but is normally not known because it's whatever the user has selected in the GUI.在本例中
index
是test.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))
Let's the docstring you want to access belongs to the file, file.py
.让我们要访问的文档字符串属于文件
file.py
。
You can get the docstring by doing the following:您可以通过执行以下操作获取文档字符串:
import file
print(file.__doc__)
If you want to get the docstring before you import it then the you could read the file and extract the docstring.如果您想在导入之前获取文档字符串,那么您可以读取文件并提取文档字符串。 Here is an example:
这是一个例子:
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"))
Note: For this regex to work the docstring will need to be at the very top.注意:要使此正则表达式起作用,文档字符串需要位于最顶部。
Here's how to get it via importlib
.这是通过
importlib
获取它的方法。 Most of the logic has been put in a function.大部分逻辑都放在了 function 中。 Note that using
importlib
does import the script (which causes all its top-level statements to be executed), but the module itself is discarded when the function returns.请注意,使用
importlib
确实会导入脚本(这会导致执行其所有顶级语句),但是当 function 返回时,模块本身会被丢弃。
If this was the script docstring_test.py
in the current directory that I wanted to get the docstring from:如果这是我想从中获取文档字符串的当前目录中的脚本
docstring_test.py
:
""" this is a multiline
docstring.
"""
print('hello world')
Here's how to do it:这是如何做到的:
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: Output:
hello world
this is a multiline
docstring.
Update:更新:
Here's how to do it by letting the ast
module in the standard library do the parsing which avoids both importing/executing the script as well as trying to parse it yourself with a regex.这是通过让标准库中的
ast
模块进行解析来避免导入/执行脚本以及尝试使用正则表达式自己解析它的方法。
This looks more-or-less equivalent to what's in your question, so it's unclear why what you have isn't working for you.这看起来或多或少等同于您的问题,因此尚不清楚为什么您所拥有的对您不起作用。
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: Output:
' this is a multiline\n docstring.\n'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.