Friday, May 18, 2012

Useful Python Tools

I often stumble across and use a number of useful tools for creating Python code.  Thought I'd barf out a blog post documenting a few of them so that my future self will be able to find this info again if need be. :) 

( is a Python code coverage tool and is useful for finding out how well your unit tests cover your code.  I've often had it find big deficiencies in my unit test coverage.  Common usage:

$ coverage run
$ coverage report -m

Will spit out a coverage report for the tests in  Used in this way, isn't particularly handy, but combined with a good unit test runner (see below) it becomes very handy.



Is nicer testing for Python.  Nose is an extremely handy unittest runner that has some perks over the standard Python unittest module.  Continuing from the last tool, nose also integrates very nicely with  I commonly use it to produce some nice HTML pages summarzing test coverage for my project:

$ nosetests --with-coverage --cover-inclusive --cover-html --cover-erase

produces a "cover" directory containing an index.html with some nice pretty HTML reports telling me how well my unit tests cover my codebase.



pymetrics is a handy tool for spitting out some well, metrics, about your code.  Ex:

$ pymetrics

Spits out a bunch of numbers about including trivial things like how many methods have docstrings, to more interesting things like the McCabe cyclomatic complexity of each method/function within the module.  Handy.



Is a simple "lines of code" counter that happens to support Python.  In the top directory of a project a:

$ cloc .

will give you summary output for your project like:

Language  files          blank       comment           code
Python       31           3454          9215          14775
SUM:         31           3454          9215          14775

While LOC is generally a meaningless statistic, it can be handy for getting a "ballpark" idea of how big a project is.