這是不完美的,但應該是一個開始,我會在個位相加更好的實施方案:
from ast import parse, Call, walk
import importlib
import inspect
mod = "test"
mod = importlib.import_module(mod)
p = parse(inspect.getsource(mod))
from ast import literal_eval
vals = []
for node in p.body:
if isinstance(node, FunctionDef) and node.name == "my_method":
for node in walk(node):
if isinstance(node,Call) and node.func.id == "foobar":
vals.append([literal_eval(val) for val in node.args])
print(vals)
[['apple', 'pear', 6], [1, 2, 3], ['a', 'b']]
test.py的樣子:
def foobar(a=0, b=0, c=None):
return a, b, c
def other_method(x,y,z):
return x,y,z
def my_method():
print('hello')
foobar('apple', 'pear', 6)
print('world')
foobar(1, 2, 3)
for i in range(10):
if i > 9:
s = foobar(4, 5, 6)
print(s)
return foobar('a', 'b')
def my_method2():
foobar('orange', 'tomatoe', 6)
foobar(10, 20, 30)
for i in range(10):
if i > 9:
foobar(40, 50, 60)
other_method("foo","bar","foobar")
return foobar('c', 'd')
如果你有一個您需要將print('world')
改爲foobar(a=1, b=2, c=3)
vals = []
for node in p.body:
if isinstance(node, FunctionDef) and node.name == "my_method":
for node in walk(node):
if isinstance(node, Call) and node.func.id == "foobar":
kws = node.keywords
if kws:
print("Found keywords",[(kw.arg, literal_eval(kw.value)) for kw in kws])
else:
print([literal_eval(val) for val in node.args])
輸出:
[['apple', 'pear', 6], [], ['a', 'b'], [4, 5, 6]]
['apple', 'pear', 6]
('Found keywords', [('a', 1), ('b', 2), ('c', 3)])
['a', 'b']
[4, 5, 6]
使用ast.Nodevisitor找到所有通話對象將返回所有調用所有功能 「FOOBAR」:
from ast import parse, NodeVisitor, literal_eval
import importlib
import inspect
mod = "test"
mod = importlib.import_module(mod)
p = parse(inspect.getsource(mod))
class FindCall(NodeVisitor):
def __init__(self, *args):
if len(args) < 1:
raise ValueError("Must supply at least ine target function")
self.result = {arg: []for arg in args}
def visit_Call(self, node):
if node.func.id in self.result:
self.result[node.func.id].append(map(literal_eval, node.args))
# visit the children
self.generic_visit(node)
fc = FindCall("foobar")
fc.visit(p)
print(fc.result)
輸出:
from pprint import pprint as pp
pp(fc.result)
{'foobar': [['apple', 'pear', 6],
[1, 2, 3],
[4, 5, 6],
['a', 'b'],
['orange', 'tomatoe', 6],
[10, 20, 30],
[40, 50, 60],
['c', 'd']],
'other_method': [['foo', 'bar', 'foobar']]}
您可以再次添加對kwargs的搜索並僅搜索特定功能。
我認爲foo是在什麼地方定義的? –
'foobar()'是的。 – thodic
是的,意思是foobar,我改變了代碼,所以你應該得到foobar的任何電話。 –