Skip to content

Instantly share code, notes, and snippets.

@Tritlo
Tritlo / McCabeCounter.py
Last active December 14, 2015 15:28
A program that calculates the (McCabe complexity)[https://en.wikipedia.org/wiki/McCabe_complexity] of python programs in a directory and subdirectories. Usage: python McCabeCounter.py directory
#!/usr/bin/python
#encoding utf-8
import sys, os
from itertools import takewhile
def getFuncs(filename, funcDef = "def "):
lines = open(filename,'r').readlines()
funcDefIn = lambda line: True if line.strip().startswith(funcDef) else False
funcs = map(lambda i: [lines[i]] + list(takewhile(lambda x: not funcDefIn(x),lines[i+1:])),filter(lambda i: funcDefIn(lines[i]),range(len(lines))))
# grabs lines until start of next func/end of file lines where functions start