如何分析 Python 代码来识别有问题的区域?

发布于 2024-07-06 17:15:12 字数 1728 浏览 8 评论 0原文

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(8

梦屿孤独相伴 2024-07-13 17:15:12

为了测量圈复杂度,有一个很好的工具,位于 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.

楠木可依 2024-07-13 17:15:12

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

(使用 pip 安装它:pip install radon

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

  • 原始指标(包括 SLOC、注释行、空白行和c.)
  • 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)
飘落散花 2024-07-13 17:15:12

对于静态分析,有 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

少钕鈤記 2024-07-13 17:15:12

当您需要了解一个新项目时,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:

alt text

无人问我粥可暖 2024-07-13 17:15:12

感谢Pydev,您可以Eclipse IDE 中集成 pylint 非常容易,并分别获得一个代码报告保存修改后的文件的时间。

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.

恬淡成诗 2024-07-13 17:15:12

使用 flake8,它在一个工具中提供了 pep8、pyflakes 和圈复杂度分析

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

澜川若宁 2024-07-13 17:15:12

有一个工具叫
CloneDigger 可帮助您查找类似的代码片段。

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

旧竹 2024-07-13 17:15:12

为了检查圈复杂度,当然有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文