Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I analyze Python code to identify problematic areas?

I have a large source repository split across multiple projects. I would like to produce a report about the health of the source code, identifying problem areas that need to be addressed.

Specifically, I'd like to call out routines with a high cyclomatic complexity, identify repetition, and perhaps run some lint-like static analysis to spot suspicious (and thus likely erroneous) constructs.

How might I go about constructing such a report?

like image 294
Jerub Avatar asked Sep 19 '08 07:09

Jerub


People also ask

How do you analyze code in Python?

Python code analysis can be a heavy subject, but it can be very helpful in making your programs better. There are several Python code analyzers that you can use to check your code and see if they conform to standards. pylint is probably the most popular. It's very configurable, customizable and pluggable too.

What is static analysis in Python?

Why use static analysis? The main work of static code analysis tools is to analyze source code or compiled code so that you could easily detect vulnerabilities without executing a program. 👍 You are already using it (if you use any IDE that already has static analyzers, Pycharm uses pep8 for example).

How do you check complexity in Python?

To understand Python code complexity we can take a look at Cyclomatic Complexity (proposed by Tomas McCabe in 1976), a metric used to calculate it. This is a measure of the linearly independent paths computed using the control-flow graph of your code.

Which tool checks for errors and ensures that Python code is well formatted?

pycodestyle (formerly pep8)


1 Answers

For measuring cyclomatic complexity, there's a nice tool available at traceback.org. The page also gives a good overview of how to interpret the results.

+1 for pylint. It is great at verifying adherence to coding standards (be it PEP8 or your own organization's variant), which can in the end help to reduce cyclomatic complexity.

like image 136
Mike Griffith Avatar answered Sep 19 '22 20:09

Mike Griffith