def get_complexity_number(snippet, strio, max=0): """Get the complexity number from the printed string.""" # Report from the lowest complexity number. get_code_complexity(snippet, max) strio_val = strio.getvalue() if strio_val: return int(strio_val.split()[-1].strip("()")) else: return None
def get_complexity_number(snippet, strio): """Get the complexity number from the printed string.""" # Report from the lowest complexity number. get_code_complexity(snippet, 0) strio_val = strio.getvalue() if strio_val: return int(strio_val.split()[-1].strip("()")) else: return None
def test_idempotent_any_syntatically_valid_python(src_contents: str, max_complexity: int) -> None: """Property-based tests for mccabe. This test case is based on a similar test for Black, the code formatter. Black's test was written by Zac Hatfield-Dodds, the author of Hypothesis and the Hypothesmith tool for source code generation. You can run this file with `python`, `pytest`, or (soon) a coverage-guided fuzzer Zac is working on. """ # Before starting, let's confirm that the input string is valid Python: compile(src_contents, "<string>", "exec") # else bug is in hypothesmith # Then try to apply get_complexity_number to the code... get_code_complexity(src_contents, max_complexity)
def test_sample(self): self.assertEqual(get_code_complexity(_GLOBAL, 1), 2) self.out.seek(0) res = self.out.read().strip().split('\n') wanted = ["stdin:5:1: C901 'a' is too complex (4)", "stdin:2:1: C901 'Loop 2' is too complex (2)"] self.assertEqual(res, wanted)
def test_print_message(self): get_code_complexity(sequential, 0) printed_message = self.strio.getvalue() self.assertEqual(printed_message, "stdin:1:1: C901 'f' is too complex (1)\n")
def cal_metric(text): mccabe.get_code_complexity(text, 1)
import mccabe file_path = ".\\demo_combine.py" f = open(file_path, encoding='utf-8') text = f.read() mccabe.get_code_complexity(text,1) f.close() print('----------------------------------------') file_path = ".\\demo_separate.py" f = open(file_path, encoding='utf-8') text = f.read() mccabe.get_code_complexity(text,1) f.close()