Getting Python source code metrics using radon

Overview

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.

Installation method

Execute the following in python2.x or Python3.x.

easy_install easy_install radon

How to use

Measurement of Cyclomatic Complexity

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"

無題.png

__ 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

Maintainability Index measurement

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

Raw data measurement

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

Use from the program

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

Getting Python source code metrics using radon
Check python code styles using pep8
[Python] Read the Flask source code
[VS Code] ~ Tips when using python ~
Jinja2 source code formatting using atom-beautify
Execute Python code on C ++ (using Boost.Python)
Notes on using code formatter in Python
python setup.py test the code using multiprocess
[Python] Read the source code of Bottle Part 2
Try using Sourcetrail, a source code visualization tool
Debug with VS Code using boost python numpy
Start using Python
python character code
[Python] Read the source code of Bottle Part 1
[Python] Algorithm-aware code
Scraping using Python
[Python] Django Source Code Reading View Starting from Zero ①
[Unity (C #), Python] Try running Python code in Unity using IronPython
[Python] I immediately tried using Pylance's VS Code extension.
How to make a Python package using VS Code
Operate Redmine using Python Redmine
Fibonacci sequence using Python
1.1 Getting Started with Python
Python code acceleration approach
Data analysis using Python 0
Rewrite Python2 code to Python3 (2to3)
infomap python draw code
Before writing Python code
Install python from source
Getting Started with Python
Data cleaning using Python
How to parse Java source code with AST (Abstract Syntax Tree) using ANTLR and Python
About Python3 character code
Using Python #external packages
WiringPi-SPI communication using Python
Age calculation using python
Search Twitter using Python
Getting Started with Python
Name identification using python
Python Requests status code
Notes using Python subprocesses
OpenCV basic code (python)
Try using Tweepy [Python2.7]
Getting Started with python3 # 3 Try Advanced Computations Using Import Statements
Remove one-line comments containing Japanese from source code in Python
Always check PEP8 while editing Python source code in Emacs
PyArmor ~ Easy way to encrypt and deliver python source code ~