Julia:列出模块内的文档字符串
Julia: List docstrings inside a module
有没有办法从以下模块 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
我可以获得模块 main
和模块 nested
的文档字符串,假设我已经知道模块 nested
存在于模块 main
中。
? 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()
希望对您有所帮助。
有没有办法从以下模块 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
我可以获得模块 main
和模块 nested
的文档字符串,假设我已经知道模块 nested
存在于模块 main
中。
? 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()
希望对您有所帮助。