- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_a_code_complexity_analyzer_in_python.py
More file actions
Latest commit
27 lines (22 loc) · 864 Bytes
/
Copy pathcreate_a_code_complexity_analyzer_in_python.py
File metadata and controls
27 lines (22 loc) · 864 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# Use radon library to analyze code complexity
# Install radon using
# >> pip install radon
fromradon.complexityimportcc_rank, cc_visit
fromradon.cli.toolsimportiter_filenames
# Define a function to calculate complexity
defcalculate_complexity(file_path):
withopen(file_path, 'r') asfile:
source_code=file.read()
# Calculate complexity
results=cc_visit(source_code)
returnresults
# Get list of Python files in a directory
file_paths=iter_filenames(['path/to/directory'], exclude='*test*.py')
# Loop through files and calculate complexity
forfile_pathinfile_paths:
complexity_results=calculate_complexity(file_path)
print(f"File: {file_path}, Complexity: {complexity_results.total_complexity}")
# Output
# >> File: example.py, Complexity: 10
# >> File: another.py, Complexity: 15
# Please Like and Subscribe