def average_complexity(blocks): '''Compute the average Cyclomatic complexity from the given blocks. Blocks must be either :class:`~radon.visitors.Function` or :class:`~radon.visitors.Class`. If the block list is empty, then 0 is returned. ''' size = len(blocks) if size == 0: return 0 return sum((GET_COMPLEXITY(block) for block in blocks), .0) / len(blocks)
'''This module contains all high-level helpers function that allow to work with Cyclomatic Complexity ''' import math from flake8_polyfill import options from radon.visitors import GET_COMPLEXITY, ComplexityVisitor, code2ast # sorted_block ordering functions SCORE = lambda block: -GET_COMPLEXITY(block) LINES = lambda block: block.lineno ALPHA = lambda block: block.name def cc_rank(cc): r'''Rank the complexity score from A to F, where A stands for the simplest and best score and F the most complex and worst one: ============= ===================================================== 1 - 5 A (low risk - simple block) 6 - 10 B (low risk - well structured and stable block) 11 - 20 C (moderate risk - slightly complex block) 21 - 30 D (more than moderate risk - more complex block) 31 - 40 E (high risk - complex block, alarming) 41+ F (very high risk - error-prone, unstable block) ============= ===================================================== Here *block* is used in place of function, method or class. The formula used to convert the score into an index is the following: