Search code examples
pythoncode-analysiscode-metrics

Measuring various aspects of code


Is there a tool that is able to measure the frequency of function calls in a project and counts other aspects (for statistics purposes) of Python code?

thank you


Solution

  • I guess you want to do static code analysis. How many locations in your code call a function.

    This is very hard to do in dynamic languages like python, because there are so many ways functions may be called otherwise than by proper name, and even the python bytecode compiler won't know always which function is going to be called in a place, and it may even change during execution. And there's standard OO polymorphism too.

    Consider:

    def doublefx(f, x):
        return f(x)*2
    
    print doublefx(math.sqrt, 9)  # 6 
    
    f = stdin.readline()
    print doublefx(getattr(math, f), 9)  # ?
    

    No way any static analyses tool will find out which functions in math.* are going to be called by this code. Even the first example would be very hard to reason about, the second is impossible.

    The following tool does some static analysis regarding complexity.

    Other analysis tools like PyLint and PyChecker rather focus on style and probable errors.