def from_code(cls, code, **kwargs): """Instantiate the class from source code (string object). The `**kwargs` are directly passed to the `ast.NodeVisitor` constructor. """ cls.code = code node = code2ast(code) return cls.from_ast(node, **kwargs)
def analyze_complexity(code, code_metrics, is_multi): ast = visitors.code2ast(code) # Cyclomatic complexity complexity_visitor = ComplexityVisitor.from_ast(ast) res = get_wavg_complexity(complexity_visitor.blocks) # Maintainability index and rank if code_metrics['sloc'] != 0: comments_lines = (code_metrics['comments'] + (code_metrics['multi'] if is_multi else 0)) comments = utils.divide_or_zero(comments_lines, code_metrics['sloc']) * 100 else: comments = 0 res['mi'] = metrics.mi_compute(calculate_halstead_volume(ast), complexity_visitor.total_complexity, code_metrics['lloc'], comments) return res
def cc_visit(code, **kwargs): '''Visit the given code with :class:`~radon.visitors.ComplexityVisitor`. All the keyword arguments are directly passed to the visitor. ''' return cc_visit_ast(code2ast(code), **kwargs)
def is_hardcoded_data(code): ast_nodes = visitors.code2ast(code).body avg_code_block_len = sum(n.end_lineno - n.lineno + 1 for n in ast_nodes) / len(ast_nodes) return avg_code_block_len > cfg.CODE_BLOCK_LEN_HARDCODED_DATA
def cc_visit(code, **kwargs): '''Visit the given code with :class:`~radon.visitors.ComplexityVisitor` and then pass the result to the :func:`~radon.complexity.sorted_results` function. ''' return cc_visit_ast(code2ast(code), **kwargs)