Radon is a program that measures Python metrics. https://github.com/rubik/radon
This allows you to get the complexity and number of lines of Python code. By measuring complexity, you can identify potential risks in your source code and identify what to refactor or test.
Execute the following in python2.x or Python3.x.
easy_install easy_install radon
Cyclomatic Complexity can be measured by using the cc command. Cyclomatic Complexity increases as the number of control statements increases. See below for details. https://radon.readthedocs.org/en/latest/intro.html#cyclomatic-complexity
Example of use:
radon cc -s ".\*.py"
__ Output content: __ The first symbol represents the block type
symbol | Description |
---|---|
F | function |
M | Method |
C | class |
The following numbers represent the start matrix of the block to be measured
line:Column
The following represents block names such as function names.
After "-", it becomes the value of rank and Cyclomatic Complexity.
Rank is determined by the value of Cyclomatic Complexity.
CC value | Rank | risk |
---|---|---|
1 - 5 | A | low -Simple block |
6 - 10 | B | low -Well-structured and stable block |
11 - 20 | C | moderate -A little complicated block |
21 - 30 | D | more than moderate -More complex blocks |
31 - 40 | E | high -Complex blocks, alarming |
41+ | F | very high -Unstable blocks that are error prone |
option: The options available for the cc command are:
Option | Description |
---|---|
-s, --show | Display Cyclomatic Complexity. |
-n, --min | Set the minimum rank to display. Specify A to F after this argument |
-x, --max | Set the maximum rank to be displayed. Specify A to F after this argument |
-a, --average | Display the average of Cyclomatic Complexity at the end. This average is-n or-Affected by x filter |
--total-average | Receive the average of all data-Unlike a, it is not affected by the filter |
-e, --exclude | Specify the path of the file to be excluded from analysis separated by commas |
-i, --ignore | Specify directories to ignore, separated by commas. Do not check under ignored directories |
-o, --order | Specify the sort order. SCORE complexity order LINES by number of lines ALPHA block name order |
-j, --json | Output the result in JSON |
--no-assert | Asser when calculating complexity()Instructions do not count |
Measure the Maintainability Index by using the mi command. Maintainability is calculated from Cyclomatic Complexity and the number of rows, and the highest 100 is, the easier it is to maintain.
See below for details https://radon.readthedocs.org/en/latest/intro.html#maintainability-index
Example of use:
radon mi -s ".\*.py"
__ Output content: __
C:\dev\py33>radon mi -s "test.py"
test.py - A (58.76)
Display file name, rank, MI score
The rank is as follows
MI score | Rank | Maintainability |
---|---|---|
100 - 20 | A | Very expensive |
19 - 10 | B | Medium |
9 - 0 | C | Very low |
option: The options available for the mi command are:
Option | Description |
---|---|
-s, --show | Display Maintainability Index. |
-e, --exclude | Specify the path of the file to be excluded from analysis separated by commas |
-i, --ignore | Specify directories to ignore, separated by commas. Do not check under ignored directories |
-m, --multi | Do not count multi-line strings as comments |
The raw command outputs the following numbers.
・ LOC: Total number of lines -LLOC_: Logical LOC. Excluding blank lines and comment lines -SLOC_: Source LOC. Excluding blank lines. ・ Comments: Comment line · Multi: A multi-line string. May be considered a comment ・ Blank: Blank line
option: The options available in the raw command are:
Option | Description |
---|---|
-s, --summary | Display the measured aggregation result |
-e, --exclude | Specify the path of the file to be excluded from analysis separated by commas |
-i, --ignore | Specify directories to ignore, separated by commas. Do not check under ignored directories |
-j, --json | Output the result in JSON |
You can use radon from python by importing the radon module into python code.
Example:
import radon
from radon import raw
ret = radon.raw.analyze("""
if a==1:
print (a)
if a==2:
print (a)
""")
print(ret)
See help or see below for more information https://radon.readthedocs.org/en/latest/api.html
Recommended Posts