| 1 |
Radon |
| 2 |
===== |
| 3 |
|
| 4 |
.. image:: https://img.shields.io/codacy/grade/623b84f5f6e6708c486f371e10da3610.svg?style=for-the-badge |
| 5 |
:alt: Codacy badge |
| 6 |
:target: https://www.codacy.com/app/rubik/radon/dashboard |
| 7 |
|
| 8 |
.. image:: https://img.shields.io/travis/rubik/radon/master.svg?style=for-the-badge |
| 9 |
:alt: Travis-CI badge |
| 10 |
:target: https://travis-ci.org/rubik/radon |
| 11 |
|
| 12 |
.. image:: https://img.shields.io/coveralls/rubik/radon/master.svg?style=for-the-badge |
| 13 |
:alt: Coveralls badge |
| 14 |
:target: https://coveralls.io/r/rubik/radon?branch=master |
| 15 |
|
| 16 |
.. image:: https://img.shields.io/pypi/v/radon.svg?style=for-the-badge |
| 17 |
:alt: PyPI latest version badge |
| 18 |
:target: https://pypi.python.org/pypi/radon |
| 19 |
|
| 20 |
.. image:: https://img.shields.io/pypi/l/radon.svg?style=for-the-badge |
| 21 |
:alt: Radon license |
| 22 |
:target: https://pypi.python.org/pypi/radon |
| 23 |
|
| 24 |
|
| 25 |
---- |
| 26 |
|
| 27 |
Radon is a Python tool that computes various metrics from the source code. |
| 28 |
Radon can compute: |
| 29 |
|
| 30 |
* **McCabe's complexity**, i.e. cyclomatic complexity |
| 31 |
* **raw** metrics (these include SLOC, comment lines, blank lines, &c.) |
| 32 |
* **Halstead** metrics (all of them) |
| 33 |
* **Maintainability Index** (the one used in Visual Studio) |
| 34 |
|
| 35 |
Requirements |
| 36 |
------------ |
| 37 |
|
| 38 |
Radon will run from **Python 2.7** to **Python 3.8** (except Python versions |
| 39 |
from 3.0 to 3.3) with a single code base and without the need of tools like |
| 40 |
2to3 or six. It can also run on **PyPy** without any problems (currently PyPy |
| 41 |
3.5 v7.3.1 is used in tests). |
| 42 |
|
| 43 |
Radon depends on as few packages as possible. Currently only `mando` is |
| 44 |
strictly required (for the CLI interface). `colorama` is also listed as a |
| 45 |
dependency but if Radon cannot import it, the output simply will not be |
| 46 |
colored. |
| 47 |
|
| 48 |
**Note**: |
| 49 |
**Python 2.6** was supported until version 1.5.0. Starting from version 2.0, it |
| 50 |
is not supported anymore. |
| 51 |
|
| 52 |
Installation |
| 53 |
------------ |
| 54 |
|
| 55 |
With Pip: |
| 56 |
|
| 57 |
.. code-block:: sh |
| 58 |
|
| 59 |
$ pip install radon |
| 60 |
|
| 61 |
If you want to configure Radon from `pyproject.toml` and you run Python <3.11, |
| 62 |
you'll need the extra `toml` dependency: |
| 63 |
|
| 64 |
.. code-block:: sh |
| 65 |
|
| 66 |
$ pip install radon[toml] |
| 67 |
|
| 68 |
Or download the source and run the setup file: |
| 69 |
|
| 70 |
.. code-block:: sh |
| 71 |
|
| 72 |
$ python setup.py install |
| 73 |
|
| 74 |
Usage |
| 75 |
----- |
| 76 |
|
| 77 |
Radon can be used either from the command line or programmatically. |
| 78 |
Documentation is at https://radon.readthedocs.org/. |
| 79 |
|
| 80 |
Cyclomatic Complexity Example |
| 81 |
----------------------------- |
| 82 |
|
| 83 |
Quick example: |
| 84 |
|
| 85 |
.. code-block:: sh |
| 86 |
|
| 87 |
$ radon cc sympy/solvers/solvers.py -a -nc |
| 88 |
sympy/solvers/solvers.py |
| 89 |
F 346:0 solve - F |
| 90 |
F 1093:0 _solve - F |
| 91 |
F 1434:0 _solve_system - F |
| 92 |
F 2647:0 unrad - F |
| 93 |
F 110:0 checksol - F |
| 94 |
F 2238:0 _tsolve - F |
| 95 |
F 2482:0 _invert - F |
| 96 |
F 1862:0 solve_linear_system - E |
| 97 |
F 1781:0 minsolve_linear_system - D |
| 98 |
F 1636:0 solve_linear - D |
| 99 |
F 2382:0 nsolve - C |
| 100 |
|
| 101 |
11 blocks (classes, functions, methods) analyzed. |
| 102 |
Average complexity: F (61.0) |
| 103 |
|
| 104 |
Explanation: |
| 105 |
|
| 106 |
* ``cc`` is the radon command to compute Cyclomatic Complexity |
| 107 |
* ``-a`` tells radon to calculate the average complexity at the end. Note that |
| 108 |
the average is computed among the *shown* blocks. If you want the total |
| 109 |
average, among all the blocks, regardless of what is being shown, you should |
| 110 |
use ``--total-average``. |
| 111 |
* ``-nc`` tells radon to print only results with a complexity rank of C or |
| 112 |
worse. Other examples: ``-na`` (from A to F), or ``-nd`` (from D to F). |
| 113 |
* The letter *in front of* the line numbers represents the type of the block |
| 114 |
(**F** means function, **M** method and **C** class). |
| 115 |
|
| 116 |
Actually it's even better: it's got colors! |
| 117 |
|
| 118 |
.. image:: https://cloud.githubusercontent.com/assets/238549/3707477/5793aeaa-1435-11e4-98fb-00e0bd8137f5.png |
| 119 |
:alt: A screen of Radon's cc command |
| 120 |
|
| 121 |
|
| 122 |
**Note about file encoding** |
| 123 |
|
| 124 |
On some systems, such as Windows, the default encoding is not UTF-8. If you are |
| 125 |
using Unicode characters in your Python file and want to analyze it with Radon, |
| 126 |
you'll have to set the `RADONFILESENCODING` environment variable to `UTF-8`. |
| 127 |
|
| 128 |
|
| 129 |
On a Continuous Integration server |
| 130 |
---------------------------------- |
| 131 |
|
| 132 |
If you are looking to use `radon` on a CI server you may be better off with |
| 133 |
`xenon <https://github.com/rubik/xenon>`_. Although still experimental, it will |
| 134 |
fail (that means exiting with a non-zero exit code) when various thresholds are |
| 135 |
surpassed. `radon` is more of a reporting tool, while `xenon` is a monitoring |
| 136 |
one. |
| 137 |
|
| 138 |
If you are looking for more complete solutions, read the following sections. |
| 139 |
|
| 140 |
Codacy |
| 141 |
++++++++++++ |
| 142 |
|
| 143 |
`Codacy <https://www.codacy.com/>`_ uses Radon `by default <https://support.codacy.com/hc/en-us/articles/213632009-Engines#other-tools>`_ to calculate metrics from the source code. |
| 144 |
|
| 145 |
Code Climate |
| 146 |
++++++++++++ |
| 147 |
|
| 148 |
Radon is available as a `Code Climate Engine <https://docs.codeclimate.com/docs/list-of-engines>`_. |
| 149 |
To understand how to add Radon's checks to your Code Climate Platform, head |
| 150 |
over to their documentation: |
| 151 |
https://docs.codeclimate.com/v1.0/docs/radon |
| 152 |
|
| 153 |
coala Analyzer |
| 154 |
++++++++++++++ |
| 155 |
|
| 156 |
Radon is also supported in `coala <http://coala.io/>`_. To add Radon's |
| 157 |
checks to coala, simply add the ``RadonBear`` to one of the sections in |
| 158 |
your ``.coafile``. |
| 159 |
|
| 160 |
CodeFactor |
| 161 |
++++++++++++ |
| 162 |
|
| 163 |
`CodeFactor <https://www.codefactor.io/>`_ uses Radon `out-of-the-box <https://support.codefactor.io/i24-analysis-tools-open-source>`_ to calculate Cyclomatic Complexity. |
| 164 |
|
| 165 |
Usage with Jupyter Notebooks |
| 166 |
---------------------------- |
| 167 |
|
| 168 |
Radon can be used with ``.ipynb`` files to inspect code metrics for Python cells. Any ``%`` macros will be ignored in the metrics. |
| 169 |
|
| 170 |
.. note:: |
| 171 |
|
| 172 |
Jupyter Notebook support requires the optional ``nbformat`` package. To install, run ``pip install nbformat``. |
| 173 |
|
| 174 |
To enable scanning of Jupyter notebooks, add the ``--include-ipynb`` flag. |
| 175 |
|
| 176 |
To enable reporting of individual cells, add the ``--ipynb-cells`` flag. |
| 177 |
|
| 178 |
Quick example: |
| 179 |
|
| 180 |
.. code-block:: sh |
| 181 |
|
| 182 |
$ radon raw --include-ipynb --ipynb-cells . |
| 183 |
example.ipynb |
| 184 |
LOC: 63 |
| 185 |
LLOC: 37 |
| 186 |
SLOC: 37 |
| 187 |
Comments: 3 |
| 188 |
Single comments: 2 |
| 189 |
Multi: 10 |
| 190 |
Blank: 14 |
| 191 |
- Comment Stats |
| 192 |
(C % L): 5% |
| 193 |
(C % S): 8% |
| 194 |
(C + M % L): 21% |
| 195 |
example.ipynb:[0] |
| 196 |
LOC: 0 |
| 197 |
LLOC: 0 |
| 198 |
SLOC: 0 |
| 199 |
Comments: 0 |
| 200 |
Single comments: 0 |
| 201 |
Multi: 0 |
| 202 |
Blank: 0 |
| 203 |
- Comment Stats |
| 204 |
(C % L): 0% |
| 205 |
(C % S): 0% |
| 206 |
(C + M % L): 0% |
| 207 |
example.ipynb:[1] |
| 208 |
LOC: 2 |
| 209 |
LLOC: 2 |
| 210 |
SLOC: 2 |
| 211 |
Comments: 0 |
| 212 |
Single comments: 0 |
| 213 |
Multi: 0 |
| 214 |
Blank: 0 |
| 215 |
- Comment Stats |
| 216 |
(C % L): 0% |
| 217 |
(C % S): 0% |
| 218 |
(C + M % L): 0% |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
Links |
| 223 |
----- |
| 224 |
|
| 225 |
* Documentation: https://radon.readthedocs.org |
| 226 |
* PyPI: http://pypi.python.org/pypi/radon |
| 227 |
* Issue Tracker: https://github.com/rubik/radon/issues |