A simple code complexity analyzer for Python
Project description
CodeComplexity
A simple and powerful Python library for analyzing code complexity metrics.
Features
- Cyclomatic Complexity: Measures the number of linearly independent paths through code
- Cognitive Complexity: Measures how difficult code is to understand
- Nesting Depth: Tracks maximum nesting level
- Lines of Code: Counts actual code lines per function
- Parameter Count: Tracks function parameters
- Return Count: Counts return statements
Installation
pip install codecomplexity
Usage
As a Library
from codecomplexity import analyze_code, analyze_file, format_report
# Analyze code string
code = """
def example_function(x, y):
if x > 0:
for i in range(y):
if i % 2 == 0:
return i
return -1
"""
metrics = analyze_code(code)
print(format_report(metrics))
Command Line Interface
# Analyze a single file
codecomplexity script.py
# Analyze all Python files in a directory recursively
codecomplexity src/ --recursive
# Only show functions above complexity threshold
codecomplexity script.py --threshold 10
# Output as JSON
codecomplexity script.py --format json
# Save results to file
codecomplexity script.py --output report.txt
Understanding the Metrics
Cyclomatic Complexity
- 1-5: Simple function, low risk
- 6-10: Moderate complexity, medium risk
- 11-20: Complex function, high risk
- 21+: Very complex, very high risk
Cognitive Complexity
Measures how difficult code is to understand by weighting nested control structures.
Nesting Depth
Maximum level of nested blocks (if, for, while, etc.). Aim for depth ≤ 4.
API Reference
analyze_code(code: str) -> List[ComplexityMetrics]
Analyze a Python code string.
analyze_file(filepath: str) -> List[ComplexityMetrics]
Analyze a Python file.
format_report(metrics: List[ComplexityMetrics], threshold: Optional[int] = None) -> str
Format metrics into a readable report.
ComplexityMetrics
Dataclass containing:
name: Function nametype: Type of code unitlines_of_code: Line countcyclomatic_complexity: Cyclomatic complexity scorecognitive_complexity: Cognitive complexity scorenesting_depth: Maximum nesting levelparameters: Parameter countreturns: Return statement count
Development
# Clone repository
git clone https://github.com/yourusername/codecomplexity.git
cd codecomplexity
# Install in development mode
pip install -e ".[dev]"
# Run tests
pytest
# Format code
black .
# Lint
flake8
Contributing
Contributions are welcome! Please feel free to submit a Pull Request.
License
MIT License - see LICENSE file for details.
Acknowledgments
Inspired by various code complexity tools including McCabe, Radon, and SonarQube.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file codecomplexity-0.1.2.tar.gz.
File metadata
- Download URL: codecomplexity-0.1.2.tar.gz
- Upload date:
- Size: 6.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6acdfa2cb91aa88a310c38cace9860ef93190a0aa7d0a67bccab624b1bec66e0
|
|
| MD5 |
0ced795d53aafcb022659169c1c02061
|
|
| BLAKE2b-256 |
ed00b219f96bfe3dd3e1c705140541c720c581fc71ff3bfc37798ece0863b15e
|
File details
Details for the file codecomplexity-0.1.2-py3-none-any.whl.
File metadata
- Download URL: codecomplexity-0.1.2-py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.2.0 CPython/3.12.8
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
10f9c751d5c8971b3a285e87a5d9662680ad1ac5e60cc62894176ebe1a467db2
|
|
| MD5 |
9af5cb5c1b333964c6dac1c36c4a4590
|
|
| BLAKE2b-256 |
e5b1362a0c600234d5be63c9ab65b2ac3a4fae9afc7e36936d34186fc5eecef6
|