Sig*_*mun 14 python fortran docstring f2py
我想使用docstring或类似的东西记录我的fortran例程,这些例程可以与python help命令一起使用.由f2py生成的自动生成的文档字符串是不够的,我需要添加更多细节,就像我们使用python函数docstring一样.
在我的想法中,它应该看起来像:
mymod.f:
subroutine foo()
! This is my function
end subroutine
Run Code Online (Sandbox Code Playgroud)
并在python会话中:
>>> import mymod
>>> help(mymod.foo)
Run Code Online (Sandbox Code Playgroud)
一个有点肮脏的解决方案是将文档保存在 ascii 文件中并在运行时加载它们。f2py 文档在编译时进行硬编码,并且到目前为止,我认为在包装器中修改它的选项不可用(这会很好!)。
例如,您可以编写一个 __init__.py 文件来加载 f2py 编译模块 _mymodule.so 并覆盖或附加到 f2py __doc__ 字符串。“>> mymodule.function?” 在 ipython 中可以工作,但令人惊讶的是“>> help(mymodule.function)”却不能!(不知道为什么......)
以下 __init__.py 片段采用存储在文件夹 doc/ 中的文档以及与每个函数关联的文件 doc/"function name".doc。在这种情况下,文档始终会加载,但您也可以手动加载。
def load_documentation():
"""
Fills the modules __doc__ strings
"""
import os
from . import _mymodule
print('loading documentation')
docfolder = os.path.abspath(os.path.join(os.path.dirname(__file__), 'doc'))
for name,func in _mymodule.__dict__.items():
if callable(func):
try:
path = os.path.join(docfolder,name.lower()+'.doc')
docfile = open(path)
doc = docfile.read()
docfile.close()
func.__doc__ = doc
except IOError as msg:
print(msg)
load_documentation()
from _mymodule import *
Run Code Online (Sandbox Code Playgroud)