module - Julia:列出模块内的文档字符串

标签 module julia docstring

有没有办法从以下模块main中提取所有文档字符串,包括驻留在模块main内的模块nested的文档字符串>,还不知道模块main中有哪些模块?

"This is Module main"
module main

"This is fA()"
fA(x) = x

"This is module nested"
module nested

"This is fB()"
fB(x) = x

end

end

假设我已经知道模块 nested 存在于模块 main 中,我可以获得模块 main 和模块 nested 的文档字符串

? main
This is Module main
? main.nested
This is Module nested

我熟悉 names(),它有帮助,但在其最简单的咒语中没有那么多:

names(main, all = true)
7-element Array{Symbol,1}:
 Symbol("##meta#252")
 Symbol("#eval")     
 Symbol("#fA")       
 :eval               
 :fA                 
 :main               
 :nested             

解释 names() 的输出并不容易:nestedmain 内的一个模块并不明显。

最佳答案

问题并不完全清楚,但这里有一些有用的代码:

# check if a symbol is a nested module
issubmodule(m::Module, s::Symbol) = isa(eval(m,s),Module) && module_name(m) != s

# get a list of all submodules of module m
submodules(m) = filter(x->issubmodule(m,x),names(m,true))

# get a list of all docstring bindings in a module
allbindings(m) = [ [y[2].data[:binding] for y in x[2].docs]
  for x in eval(m,Base.Docs.META)]

# recursively get all docstrings from a module and submodules
function alldocs(m)
    thedocs = map(x->Base.Docs.doc(x[1]),allbindings(m))
    return vcat(thedocs,map(x->alldocs(eval(m,x)),submodules(m))...)
end

现在你有:

julia> alldocs(main)
  This is fA()
  This is Module main
  This is module nested
  This is fB()

希望这有帮助。

关于module - Julia:列出模块内的文档字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44166177/

相关文章:

global-variables - 为什么我可以更改另一个模块中的数组的值,但不能更改变量的值

python - python (2.7) unittest 的测试描述如何修改

julia - 错误: "/tmp/jl_F8q8cm/Registry.toml": No such file in Julia REPL

node.js - nodejs 从 import 转换为 require

python - 执行 `from abc import xyz` 模块 `abc`去哪了?

javascript - 尽管导出,Angular 仍无法识别导入模块中的组件

julia - unicodeplots() 的奇怪输出

python - 文档字符串 - 一行与多行

python - 将文档字符串中的预期结果指定为十六进制?

模块未定义且进程未在 Visual Studio 代码中的 eslint 中定义