1

I'm trying to use Ned Batchelder's script for McCabe Complexity to get the complexity of codes written in Python 3, but I want to run it from a script instead of running it via command line.

I've already installed the McCabe from pypi and the version is 0.6.1. I'm using Python 3.8 in VSCode via Jupyter Notebook.

After reading mccabe.py from the GitHub page, I'm trying this so far:

from mccabe import *
code = open("testFile.py").read()
res = get_code_complexity(code, threshold=7, filename="testFile.py")
print(res)

However, the result I'm getting from the code above is this:

testFile.py:5:1: C901 'If 5' is too complex (63)
1

I don't know how to interpret this, because when I run the application from the command line, I get this:

$ python -m mccabe testFile.py 
If 5 63

What does this mean? I thought that the complexity would be this 63, but in the res variable I'm getting 1 returned. testFile.py is in the same directory I'm running the .ipynb. I uploaded it as I think it may help.

P.S.: I know I could use Radon for this, but it seems that it only calculates the McCabe Complexity if the code is within a function, not "loose". The vast majority of the programs I'm trying to analyze don't use main(). The testFile.py I'm using as an example results in a blank list when using Radon, that's why I'm trying with this McCabe from Ned Batchelder.

BloodySinner
  • 95
  • 1
  • 9

0 Answers0