有没有办法从以下模块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()
的输出并不容易:nested
是 main
内的一个模块并不明显。
最佳答案
问题并不完全清楚,但这里有一些有用的代码:
# 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/