Aviral Srivastava
Aviral Srivastava

Reputation: 4582

How to store the result of trace calls in trace.Trace module in Python3

I want to store the results of the trace functional calls in a variable(a data structure). I am able to get the results out in the shell but unable to store in a variable.

I tried reading the official documentation but it does not address it, nor I was able to figure this out using the source code.

This is my 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')



Upvotes: 1

Views: 217

Answers (1)

Aviral Srivastava
Aviral Srivastava

Reputation: 4582

After looking at the source code, it turns out that calledfuncs is the required api to be used.

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()

Upvotes: 2

Related Questions