标签归档:cyclomatic-complexity

如何分析Python代码以找出问题区域?

问题:如何分析Python代码以找出问题区域?

我有一个跨多个项目的大型资源库。我想生成一份有关源代码运行状况的报告,以确定需要解决的问题区域。

具体来说,我想找出循环复杂度高的例程,确定重复性,并可能进行一些类似于皮棉的静态分析,以发现可疑的(因而可能是错误的)构造。

我将如何构建这样的报告?

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?


回答 0

为了测量圈复杂度,traceback.org提供了一个不错的工具。该页面还很好地概述了如何解释结果。

+1为pylint。它非常适合验证是否遵守编码标准(无论是PEP8还是您自己组织的变体),最终可以帮助降低循环复杂性。

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.


回答 1

对于圈复杂度,可以使用radonhttps : //github.com/rubik/radon

(使用pip安装它:pip install radon

此外,它还具有以下功能:

  • 原始指标(包括SLOC,注释行,空白行等)
  • Halstead指标(所有指标)
  • 可维护性指数(在Visual Studio中使用的指数)

For cyclomatic complexity you can use radon: https://github.com/rubik/radon

(Use pip to install it: pip install radon)

Additionally it also has these features:

  • raw metrics (these include SLOC, comment lines, blank lines, &c.)
  • Halstead metrics (all of them)
  • Maintainability Index (the one used in Visual Studio)

回答 2

对于静态分析,有pylintpychecker。我个人使用pylint,因为它似乎比pychecker更全面。

对于圈复杂度,您可以尝试使用此perl程序,或者本文介绍了python程序来执行相同的操作

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


回答 3

当您需要了解新项目时,Pycana就像魅力一样工作!

PyCAna(Python代码分析器)是一个简单的python代码分析器的名字,它在执行代码后创建类图。

看看它是如何工作的:http : //pycana.sourceforge.net/

输出:

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:


回答 4

感谢Pydev,您可以真正轻松地将pylint集成Eclipse IDE中,并在每次保存修改后的文件时获得代码报告。

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.


回答 5

使用flake8,在一个工具中提供pep8,pyflakes和循环复杂性分析

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


回答 6

有一个名为CloneDigger的工具 ,可以帮助您找到类似的代码片段。

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


回答 7

为了检查圈复杂度,当然有mccabe包装。

安装:

$ pip install --upgrade mccabe

用法:

$ python -m mccabe --min=6 path/to/myfile.py

请注意上面的阈值6。根据这个答案,分数> 5可能应该简化。

输出示例--min=3

68:1: 'Fetcher.fetch' 3
48:1: 'Fetcher._read_dom_tag' 3
103:1: 'main' 3

也可以通过pylint-mccabepytest-mccabe等使用它。

For checking cyclomatic complexity, there is of course the mccabe package.

Installation:

$ pip install --upgrade mccabe

Usage:

$ python -m mccabe --min=6 path/to/myfile.py

Note the threshold of 6 above. Per this answer, scores >5 probably should be simplified.

Sample output with --min=3:

68:1: 'Fetcher.fetch' 3
48:1: 'Fetcher._read_dom_tag' 3
103:1: 'main' 3

It can optionally also be used via pylint-mccabe or pytest-mccabe, etc.