How can I analyze Python code to identify problematic areas?

前端 未结 8 1876
鱼传尺愫
鱼传尺愫 2020-12-07 06:58

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

相关标签:
8条回答
  • 2020-12-07 07:34

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

    0 讨论(0)
  • 2020-12-07 07:34

    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.

    0 讨论(0)
  • 2020-12-07 07:37

    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

    0 讨论(0)
  • 2020-12-07 07:42

    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)
    0 讨论(0)
  • 2020-12-07 07:44

    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:

    0 讨论(0)
  • 2020-12-07 07:52

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

    0 讨论(0)
提交回复
热议问题