Skip to main content

Code Metrics in Python

Project description

Codacy badge Travis-CI badge Coveralls badge PyPI latest version badge Download format Radon license

Radon is a Python tool that computes various metrics from the source code. Radon can compute:

  • McCabe’s complexity, i.e. cyclomatic complexity

  • raw metrics (these include SLOC, comment lines, blank lines, &c.)

  • Halstead metrics (all of them)

  • Maintainability Index (the one used in Visual Studio)

Requirements

Radon will run from Python 2.7 to Python 3.6 with a single code base and without the need of tools like 2to3 or six. It can also run on PyPy without any problems (currently PyPy 5.8.0 is used in tests).

Radon depends on as few packages as possible. Currently only mando is strictly required (for the CLI interface). colorama is also listed as a dependency but if Radon cannot import it, the output simply will not be colored.

Note: Python 2.6 was supported until version 1.5.0. Starting from version 2.0, it is not supported anymore.

Installation

With Pip:

$ pip install radon

Or download the source and run the setup file:

$ python setup.py install

Usage

Radon can be used either from the command line or programmatically. Documentation is at https://radon.readthedocs.org/.

Cyclomatic Complexity Example

Quick example:

$ radon cc sympy/solvers/solvers.py -a -nc
sympy/solvers/solvers.py
    F 346:0 solve - F
    F 1093:0 _solve - F
    F 1434:0 _solve_system - F
    F 2647:0 unrad - F
    F 110:0 checksol - F
    F 2238:0 _tsolve - F
    F 2482:0 _invert - F
    F 1862:0 solve_linear_system - E
    F 1781:0 minsolve_linear_system - D
    F 1636:0 solve_linear - D
    F 2382:0 nsolve - C

11 blocks (classes, functions, methods) analyzed.
Average complexity: F (61.0)

Explanation:

  • cc is the radon command to compute Cyclomatic Complexity

  • -a tells radon to calculate the average complexity at the end. Note that the average is computed among the shown blocks. If you want the total average, among all the blocks, regardless of what is being shown, you should use --total-average.

  • -nc tells radon to print only results with a complexity rank of C or worse. Other examples: -na (from A to F), or -nd (from D to F).

  • The letter in front of the line numbers represents the type of the block (F means function, M method and C class).

Actually it’s even better: it’s got colors!

A screen of Radon's cc command

Note about file encoding

On some systems, such as Windows, the default encoding is not UTF-8. If you are using Unicode characters in your Python file and want to analyze it with Radon, you’ll have to set the RADONFILESENCODING environment variable to UTF-8.

On a Continuous Integration server

If you are looking to use radon on a CI server you may be better off with xenon. Although still experimental, it will fail (that means exiting with a non-zero exit code) when various thresholds are surpassed. radon is more of a reporting tool, while xenon is a monitoring one.

If you are looking for more complete solutions, read the following sections.

Codacy

Codacy uses Radon by default to calculate metrics from the source code.

Code Climate

Radon is available as a Code Climate Engine. To understand how to add Radon’s checks to your Code Climate Platform, head over to their documentation: https://docs.codeclimate.com/v1.0/docs/radon

Coala Analyzer

Radon is also supported in coala. To add Radon’s checks to coala, simply add the RadonBear to one of the sections in your .coafile.

CodeFactor

CodeFactor uses Radon out-of-the-box to calculate Cyclomatic Complexity.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

radon-2.3.0.tar.gz (1.6 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

radon-2.3.0-py2.py3-none-any.whl (45.4 kB view details)

Uploaded Python 2Python 3

File details

Details for the file radon-2.3.0.tar.gz.

File metadata

  • Download URL: radon-2.3.0.tar.gz
  • Upload date:
  • Size: 1.6 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for radon-2.3.0.tar.gz
Algorithm Hash digest
SHA256 df761b5b81ec5f4aae8d6ecae13fa9ae995113758dc321b321e760f97dacec5c
MD5 95942f4a113baa4e5b1345d3cd150c67
BLAKE2b-256 e5ad3aadd62bb509f2bbbe02730acc0c8e865ba38d619e67cff3c6eeb1ad57b6

See more details on using hashes here.

File details

Details for the file radon-2.3.0-py2.py3-none-any.whl.

File metadata

  • Download URL: radon-2.3.0-py2.py3-none-any.whl
  • Upload date:
  • Size: 45.4 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/1.12.1 pkginfo/1.4.2 requests/2.19.1 setuptools/40.4.3 requests-toolbelt/0.8.0 tqdm/4.26.0 CPython/3.7.0

File hashes

Hashes for radon-2.3.0-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 9a1a5f7ace673c21e1e91ff3ab9b30069bd02836d13181543fb5e1c04474dde2
MD5 39539584bdb6e03666cca154568b1a26
BLAKE2b-256 ccb701f75cead3e6d63b74d216ee0a8e1dd88001a685c94360a21082638ab01a

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page