如何从函数中提取所有python对象依赖项?

时间:2019-06-26 16:23:11

标签: python abstract-syntax-tree

我希望在这里必须使用ast,但目前我还不熟悉。

一个例子:

src/
    file1.py
    file2.py
    file3.py
    file4.py

file1.py

import numpy as np
from file2 import second_function, CONSTANT

def first_function(a):
    b = np.sum(a) * CONSTANT
    return second_function(b)

file2.py

import numpy as np
from file3 import third_function

CONSTANT = 3

def second_function(a):
    c = a*2
    return third_function(c)

file3.py

from file4 import unnecessary_thing

def third_function(a):
    return a

每个函数在下一个之间调用某些位。我希望能够运行它:

>>> from file1 import first_function
>>> deps = get_dependencies(first_function)
>>> print(deps)
[file2.CONSTANT, file2.second_function, file3.third_function]

我不在乎numpy或其他外部安装的模块。

有没有办法做到这一点?我在想ast.NodeVisitor

谢谢!

0 个答案:

没有答案