我想将跟踪功能调用的结果存储在变量(数据结构)中。我能够在外壳中获取结果,但无法存储在变量中。
我尝试阅读official documentation,但没有解决,也无法使用source code来解决。
这是我的代码:
from driver import main
from trace import Trace
import importlib
class GenerateSequenceDiagram:
def __init__(self, driver_module):
self.driver_module = __import__(driver_module)
def get_functions_called(self, driver_function):
self.driver_function = getattr(self.driver_module, driver_function)
print(dir(self.driver_function))
print(self.driver_function.__name__)
tracer = Trace(countfuncs=1)
try:
tracer.run('{}()'.format(self.driver_function.__name__))
except Exception as e:
print(e)
results = tracer.results()
results.write_results()
# results.write_results_file('one.txt',)
ob = GenerateSequenceDiagram('driver')
ob.get_functions_called('main')
答案 0 :(得分:0)
查看源代码后,发现calledfuncs
是要使用的必需api。
class GenerateSequenceDiagram:
def __init__(self, driver_module):
self.driver_module = __import__(driver_module)
def get_functions_called(self, driver_function):
self.driver_function = getattr(self.driver_module, driver_function)
self.driver_function()
# print(dir(self.driver_function))
# print(self.driver_function.__name__)
tracer = Trace(countfuncs=1)
tracer.run('{}()'.format(self.driver_function.__name__))
results = tracer.results()
called_functions = results.calledfuncs
for filename, modulename, funcname in sorted(called_functions):
print('filename: {}, modulename: {}, funcname: {}'.format(
filename, modulename, funcname))
results.write_results()