【发布时间】:2017-05-07 15:02:31
【问题描述】:
我正在尝试在函数级别绘制 python 包中函数和变量的用途/原因。有几个模块在其他函数中使用了函数/变量,我想创建一个看起来像这样的字典:
{'function_name':{'uses': [...functions used in this function...],
'causes': [...functions that use this function...]},
...
}
我所指的功能需要在包的模块中定义。
我该如何开始呢?我知道我可以遍历包 __dict__ 并通过以下方式测试包中定义的函数:
import package
import inspect
import types
for name, obj in vars(package).items():
if isinstance(obj, types.FunctionType):
module, *_ = inspect.getmodule(obj).__name__.split('.')
if module == package.__name__:
# Now that function is obtained need to find usages or functions used within it
但在那之后我需要找到当前函数中使用的函数。如何才能做到这一点?是否已经为这类工作开发了一些东西?我认为分析库可能必须做类似的事情。
【问题讨论】:
-
到目前为止看起来很有希望。显然这些是“失踪”
ast文档:greentreesnakes.readthedocs.io/en/latest/index.html -
@Ni。感谢您推荐这个模块。我最终实现了一些真正有效的东西。
标签: python function python-3.x mapping python-packaging