def h_visit_ast(ast_node): ''' Visit the AST node using the :class:`~radon.visitors.HalsteadVisitor` visitor. The results are `HalsteadReport` namedtuples with the following fields: * h1: the number of distinct operators * h2: the number of distinct operands * N1: the total number of operators * N2: the total number of operands * h: the vocabulary, i.e. h1 + h2 * N: the length, i.e. N1 + N2 * calculated_length: h1 * log2(h1) + h2 * log2(h2) * volume: V = N * log2(h) * difficulty: D = h1 / 2 * N2 / h2 * effort: E = D * V * time: T = E / 18 seconds * bugs: B = V / 3000 - an estimate of the errors in the implementation The actual return of this function is a namedtuple with the following fields: * total: a `HalsteadReport` namedtuple for the entire scanned file * functions: a list of `HalsteadReport`s for each toplevel function Nested functions are not tracked. ''' visitor = HalsteadVisitor.from_ast(ast_node) total = halstead_visitor_report(visitor) functions = [(v.context, halstead_visitor_report(v)) for v in visitor.function_visitors] return Halstead(total, functions)
def h_visit_ast(ast_node): '''Visit the AST node using the :class:`~radon.visitors.HalsteadVisitor` visitor. A namedtuple with the following fields is returned: * h1: the number of distinct operators * h2: the number of distinct operands * N1: the total number of operators * N2: the total number of operands * h: the vocabulary, i.e. h1 + h2 * N: the length, i.e. N1 + N2 * calculated_length: h1 * log2(h1) + h2 * log2(h2) * volume: V = N * log2(h) * difficulty: D = h1 / 2 * N2 / h2 * effort: E = D * V * time: T = E / 18 seconds * bugs: B = V / 3000 - an estimate of the errors in the implementation ''' visitor = HalsteadVisitor.from_ast(ast_node) h1, h2 = visitor.distinct_operators, visitor.distinct_operands N1, N2 = visitor.operators, visitor.operands h = h1 + h2 N = N1 + N2 if all((h1, h2)): length = h1 * math.log(h1, 2) + h2 * math.log(h2, 2) else: length = 0 volume = N * math.log(h, 2) if h != 0 else 0 difficulty = (h1 * N2) / float(2 * h2) if h2 != 0 else 0 effort = difficulty * volume return Halstead( h1, h2, N1, N2, h, N, length, volume, difficulty, effort, effort / 18., volume / 3000. )
def h_visit_ast(ast_node): '''Visit the AST node using the :class:`~radon.visitors.HalsteadVisitor` visitor. A namedtuple with the following fields is returned: * h1: the number of distinct operators * h2: the number of distinct operands * N1: the total number of operators * N2: the total number of operands * h: the vocabulary, i.e. h1 + h2 * N: the length, i.e. N1 + N2 * calculated_length: h1 * log2(h1) + h2 * log2(h2) * volume: V = N * log2(h) * difficulty: D = h1 / 2 * N2 / h2 * effort: E = D * V * time: T = E / 18 seconds * bugs: B = V / 3000 - an estimate of the errors in the implementation ''' visitor = HalsteadVisitor.from_ast(ast_node) h1, h2 = visitor.distinct_operators, visitor.distinct_operands N1, N2 = visitor.operators, visitor.operands h = h1 + h2 N = N1 + N2 if all((h1, h2)): length = h1 * math.log(h1, 2) + h2 * math.log(h2, 2) else: length = 0 volume = N * math.log(h, 2) if h != 0 else 0 difficulty = (h1 * N2) / float(2 * h2) if h2 != 0 else 0 effort = difficulty * volume return Halstead(h1, h2, N1, N2, h, N, length, volume, difficulty, effort, effort / 18., volume / 3000.)
def calculate_halstead_volume(ast): visitor = HalsteadVisitor.from_ast(ast) h1, h2 = visitor.distinct_operators, visitor.distinct_operands N1, N2 = visitor.operators, visitor.operands h = h1 + h2 N = N1 + N2 return N * math.log(h, 2) if h != 0 else 0