Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python print all function calls to know the script flow

How can I print every function/method call ? I have tried to use : python -m trace --trace but it prints also the function inner code... I just want functions names that were called. using traceback in code prints the last function called before calling the command itself, and the code contains many classes...

like image 872
Maroun Sassine Avatar asked Mar 09 '23 16:03

Maroun Sassine


1 Answers

You may find -l useful.

main.py:

def foo():
    pass

def bar():
    pass

foo()
bar()

Doing

$ python -m trace -l main.py

outputs

functions called:
filename: C:\Python34\lib\trace.py, modulename: trace, funcname: _unsettrace
filename: main.py, modulename: main, funcname: <module>
filename: main.py, modulename: main, funcname: bar
filename: main.py, modulename: main, funcname: foo

Depending on the number of functions you have, you may find a decorator more suitable:

def print_deco(func):
    def inner(*args, **kwargs):
        print(func.__name__)
        return func(*args, **kwargs)
    return inner

@print_deco
def foo():
    pass

@print_deco
def bar():
    pass

foo()
bar()

# foo
# bar
like image 86
DeepSpace Avatar answered Mar 11 '23 04:03

DeepSpace