How can I analyze Python code to identify problema

2019-01-12 14:27发布

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?

8条回答
Explosion°爆炸
2楼-- · 2019-01-12 14:39

Pycana works like charm when you need to understand a new project!

PyCAna (Python Code Analyzer) is a fancy name for a simple code analyzer for python that creates a class diagram after executing your code.

See how it works: http://pycana.sourceforge.net/

output:

alt text http://pycana.sourceforge.net/relations.png

查看更多
Anthone
3楼-- · 2019-01-12 14:42

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.

查看更多
在下西门庆
4楼-- · 2019-01-12 14:48

Use flake8, which provides pep8, pyflakes, and cyclomatic complexity analysis in one tool

查看更多
beautiful°
5楼-- · 2019-01-12 14:53

For static analysis there is pylint and pychecker. Personally I use pylint as it seems to be more comprehensive than pychecker.

For cyclomatic complexity you can try this perl program, or this article which introduces a python program to do the same

查看更多
手持菜刀,她持情操
6楼-- · 2019-01-12 14:59

There is a tool called CloneDigger that helps you find similar code snippets.

查看更多
smile是对你的礼貌
7楼-- · 2019-01-12 15:00

Thanks to Pydev, you can integrate pylint in the Eclipse IDE really easily and get a code report each time you save a modified file.

查看更多
登录 后发表回答