Python code complexity micro-tool

Tuesday 18 March 2008This is over 16 years old. Be careful.

One of the side-effects of going to PyCon is getting immersed in some side project or other. Being immersed in all things Python for a few days away from the other usual complexities of life makes it a natural environment in which to dive deep.

This PyCon, I was ignited by a comment Matt Harrison made about there not being a tool to find code paths in Python. Having wrestled with the difficulty of analyzing Python code for coverage.py, I thought I could hack something together.

A few days later, the meager result is codepaths.py. It’s a command line script. Give it a Python source file, and it will report on the McCabe complexity measure of the functions and class methods within. The -m option is a minimum complexity measure below which functions are too uninteresting to include in the output (default 2). The -d option causes the output to be a Graphviz dot file for drawing the code path graphs. Without -d, the names and complexity measures are simply listed.

This will make a PNG file, for instance:

python codepaths.py -d mycode.py | dot -Tpng -o mycode.png

Weaknesses:

  • The graphs are kind of wonky because I don’t know how to control Graphviz.
  • Some Python constructs aren’t handled yet (try/except, while/else) because I don’t know how to account for them in the complexity measure.
  • Files of any interesting size make graphs that are slow to draw and large to display.
  • The graphs could be streamlined: coalescing consecutive simple statements, removing the join points after branches, and so on.

It’s a quick hack starting point. If people are interested, it will go some place. If not, it was a fun weekend project.

Comments

[gravatar]
Ned - It was great to meet you in person. This is awesome! McCabe's 1976 paper has a few of the graph structures. We should collaborate on the missing ones..... (Me pines for more hours in the day)
[gravatar]
That's a great tool, I am using McCabe metrics and this kind of output would be nice to compute a QA page for a Python package. I am also using trace2html (see http://pypi.python.org/pypi/trace2html) for code coverage display, and I think your tool would be great if some similar kind of HTML file tree output could be generated.

A final usage in my work would be auto-generated QA pages done through svn commit hooks.
[gravatar]
It would be nice to have the output of this tool integrated with Bitten [1]. Bitten is a Trac plugin for collecting various software metrics via continuous integration. See integration into Trac's timeline [2] (entries with hammers), recent history of a branch [3], results from an individual build [4] for example outputs of Bitten.

[1] http://bitten.edgewall.org/
[2] http://bitten.edgewall.org/timeline
[3] http://bitten.edgewall.org/build/trunk
[4] http://bitten.edgewall.org/build/trunk/1237
[gravatar]
I got mccabe.py with pip. Tried it and it works about 90% of the time. It does not work when the more advance "Pascal" like scoping is used. On a copy of Dijikstra's Queens 8 I get:
c:\4>python -m mccabe "c:\MyBooks\AnExposure to Python3Programming\1Volume\source\Queens8.py"
15:0: 'Queens8' 13
111:0: 'DoDisplayBoard' 5
I modified the mccabe. The modification yields:
c:\MyBooks\AnExposure to Python3Programming\1Volume\source>python "C:\Dads\MySoftware\Sandbox\mymccabe.py" "c:\MyBooks\AnExposure to Python3Programming\1Volume\source\Queens8.py"
15:0: 'Queens8' 5
16:4: 'Queens8.Initialize' 1
31:4: 'Queens8.PlaceQueenOn' 1
45:4: 'Queens8.PlaceRestOfQueens' 5
48:8: 'Queens8.PlaceRestOfQueens.TakeQueen' 1
111:0: 'DoDisplayBoard' 5
If you are interested in the changes - email me : danbates@verizon.net

Add a comment:

Ignore this:
Leave this empty:
Name is required. Either email or web are required. Email won't be displayed and I won't spam you. Your web site won't be indexed by search engines.
Don't put anything here:
Leave this empty:
Comment text is Markdown.