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
def test_visitor(code, expected): visitor = HalsteadVisitor.from_code(dedent(code)) assert expected == ( visitor.operators, visitor.operands, visitor.distinct_operators, visitor.distinct_operands, )
def test_HalsteadVisitor(self): visitor = HalsteadVisitor.from_code(self.code) result = visitor.operators, visitor.operands, \ visitor.distinct_operators, visitor.distinct_operands self.assertEqual(result, self.expected_result)
input_list = [int(input()) for el in range(n)] print() if n <= 7 or n > 10000: print("Ошибка ввода числа измерений. Ожидается 7 < N < 10000.") sys.exit() max_s = -sys.maxsize for i in range(n - 7): for j in range(n - 7 - i): current_s = input_list[i] + input_list[i + j + 7] if current_s > max_s: max_s = current_s print(max_s) ''' visitor = HalsteadVisitor.from_code(my_code) print("Уникальные операторы: " + str(visitor.operators_seen)) print("Уникальные операнды: " + str(visitor.operands_seen)) n1, n2 = visitor.distinct_operators, visitor.distinct_operands N1, N2 = visitor.operators, visitor.operands n = n1 + n2 N = N1 + N2 if n1 and n2: length = n1 * math.log(n1, 2) + n2 * math.log(n2, 2) else: length = 0 volume = N * math.log(n, 2) if n != 0 else 0 difficulty = (n1 * N2) / float(2 * n2) if n2 != 0 else 0 effort = difficulty * volume
def test_visitor(code, expected): visitor = HalsteadVisitor.from_code(dedent(code)) assert expected == (visitor.operators, visitor.operands, visitor.distinct_operators, visitor.distinct_operands)
def testHalsteadVisitor(self): visitor = HalsteadVisitor.from_code(self.code) result = visitor.operators, visitor.operands, \ visitor.distinct_operators, visitor.distinct_operands self.assertEqual(result, self.expected_result)