Tuesday, June 22, 2010

Python code metrics with pymetrics

The python tool pymetrics can be used to measure your source code complexity. You can install it with easy_install pymetrics. Here is an example (running in virtual environment devenv; source code is located in src):
user1@deby:~/devenv/trunk$ ../bin/pymetrics src/greatings\

=== File: src/greatings/helloworld.py ===
Module src/greatings/helloworld.py is missing a module doc string.
Detected at line 1

Basic Metrics for module src/greatings/helloworld.py
          1    maxBlockDepth
          4    numBlocks
        189    numCharacters
          1    numDocStrings
          1    numFcnDocStrings
          2    numFunctions
          7    numKeywords
         16    numLines
         73    numTokens

         50.00 %FunctionsHavingDocStrings

Functions DocString present(+) or missing(-)
- main
+ say

McCabe Complexity Metric for file src/greatings/helloworld.py
          2    __main__
          1    main
          1    say

COCOMO 2's SLOC Metric for src/greatings/helloworld.py
          8    src/greatings/helloworld.py

*** Processed 1 module in run ***
Here is a script that let you generate report for all your files in src directory:

mkdir -p $working_dir

find src/ -name \*.py > $working_dir/files.txt
../bin/pymetrics --nosql --nocsv -f $working_dir/files.txt
Here is a guideline for understanding cyclomatic complexity number:
  • 1 - 15: simple code, minimum risk, can be easily covered by tests
  • 15 - 30: complicated code, consider refactoring before writing tests
  • 30 - 50+: complex code, refactor now, almost impossible to write a good tests
Read more about software complexity here and here.


  1. Score 1135 in complexity.
    Do I have to do something when score is upper 1000 ?

  2. This is extremely high... I would suggest:
    1. try to identify actors in your code; visualize the code in some sort of drawing so you can have an overall picture (no need for special software, just paper and pencil); develop ten items plan of what can be refactored first.
    2. split functions that are longer than your screen can fit in several smaller functions, introduce modules
    3. apply some sort of object oriented design to abstract certain things
    4. while refactoring ensure the new code has at least 95% test coverage, start with doctest, plan unit tests (mockers will help)
    5. after each stage provide some sort of regression testing to ensure nothing has been broken

  3. ...
    I missread the correct result.

    McCabe Complexity Metric for file src/greatings/helloworld.py
    2 __main__
    1 main
    1 say

    COCOMO 2's SLOC Metric for src/greatings/helloworld.py
    8 src/greatings/helloworld.py

    How to understand the cocomo 2's SLOC Metric ? ( the "8" score in your code).

    I have 1135, but it's cocomo 2's score.

  4. COCOMO (Constructive Cost Model) is estimated software cost. You should take is as something relative.

    You are working on project A that has a score S1 and a support for a project B is coming to you/team next week, it would be nice to have a quick idea how project B source code is complex, what is estimated cost so you can properly plan resource available. This is where metrics help. There is something you are well aware of (project A) and you got filling of project B by simply running code metrics tool.

  5. 1-15 for a single method is probably rather high. In the 2nd edition of Code Complete by Steve McConnell it mentions 0-5 as being "probably fine", and 6-10 to be a point at which you should start thinking about simplifying. At 10+ he recommends breaking parts of the routines into subroutines.

  6. Hi Andriy,
    Is there a way to get the output of PyMetrics to a format like .txt, .xml or anyother format instead of the terminal?

    1. 1. How about redirecting shell output to file?
      pymetrics hello.py > results.txt
      2. There are a number of options available, just
      try issue the following command:
      pymetrics --help

    2. Thank you very much Andriy for your quick reply. Yeah, your first option is working. But, it seems like i should try for an xml output. Can i do that?
      I could not get any solution from your second option.

    3. Eric, unfortunately there is no way to output result into xml file, however there is CSV output format that you might consider translate to XML as appropriate.

    4. I think that is the solution Andriy. Your blog is great and appricate your quick respoces. Thank you very much.

  7. Hi, csv output of PyMetrics gives line by line details of the code. Is there anyway to get summery metrics output in csv format?

    1. There is no summary metrics report, you need calculate appropriate metrics out from csv file.