# 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
```bash
pip install codecomplexity
```
## Usage
### As a Library
```python
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
```bash
# 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 name
- `type`: Type of code unit
- `lines_of_code`: Line count
- `cyclomatic_complexity`: Cyclomatic complexity score
- `cognitive_complexity`: Cognitive complexity score
- `nesting_depth`: Maximum nesting level
- `parameters`: Parameter count
- `returns`: Return statement count
## Development
```bash
# 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.
Raw data
{
"_id": null,
"home_page": "https://github.com/yourusername/codecomplexity",
"name": "codecomplexity",
"maintainer": null,
"docs_url": null,
"requires_python": ">=3.8",
"maintainer_email": null,
"keywords": null,
"author": "Your Name",
"author_email": "Ayaan Bandey <ayaanniaz@gmail.com>",
"download_url": "https://files.pythonhosted.org/packages/ed/00/b219f96bfe3dd3e1c705140541c720c581fc71ff3bfc37798ece0863b15e/codecomplexity-0.1.2.tar.gz",
"platform": null,
"description": "# CodeComplexity\r\n\r\nA simple and powerful Python library for analyzing code complexity metrics.\r\n\r\n## Features\r\n\r\n- **Cyclomatic Complexity**: Measures the number of linearly independent paths through code\r\n- **Cognitive Complexity**: Measures how difficult code is to understand\r\n- **Nesting Depth**: Tracks maximum nesting level\r\n- **Lines of Code**: Counts actual code lines per function\r\n- **Parameter Count**: Tracks function parameters\r\n- **Return Count**: Counts return statements\r\n\r\n## Installation\r\n\r\n```bash\r\npip install codecomplexity\r\n```\r\n\r\n## Usage\r\n\r\n### As a Library\r\n\r\n```python\r\nfrom codecomplexity import analyze_code, analyze_file, format_report\r\n\r\n# Analyze code string\r\ncode = \"\"\"\r\ndef example_function(x, y):\r\n if x > 0:\r\n for i in range(y):\r\n if i % 2 == 0:\r\n return i\r\n return -1\r\n\"\"\"\r\n\r\nmetrics = analyze_code(code)\r\nprint(format_report(metrics))\r\n```\r\n\r\n### Command Line Interface\r\n\r\n```bash\r\n# Analyze a single file\r\ncodecomplexity script.py\r\n\r\n# Analyze all Python files in a directory recursively\r\ncodecomplexity src/ --recursive\r\n\r\n# Only show functions above complexity threshold\r\ncodecomplexity script.py --threshold 10\r\n\r\n# Output as JSON\r\ncodecomplexity script.py --format json\r\n\r\n# Save results to file\r\ncodecomplexity script.py --output report.txt\r\n```\r\n\r\n## Understanding the Metrics\r\n\r\n### Cyclomatic Complexity\r\n- **1-5**: Simple function, low risk\r\n- **6-10**: Moderate complexity, medium risk\r\n- **11-20**: Complex function, high risk\r\n- **21+**: Very complex, very high risk\r\n\r\n### Cognitive Complexity\r\nMeasures how difficult code is to understand by weighting nested control structures.\r\n\r\n### Nesting Depth\r\nMaximum level of nested blocks (if, for, while, etc.). Aim for depth \u2264 4.\r\n\r\n## API Reference\r\n\r\n### `analyze_code(code: str) -> List[ComplexityMetrics]`\r\nAnalyze a Python code string.\r\n\r\n### `analyze_file(filepath: str) -> List[ComplexityMetrics]`\r\nAnalyze a Python file.\r\n\r\n### `format_report(metrics: List[ComplexityMetrics], threshold: Optional[int] = None) -> str`\r\nFormat metrics into a readable report.\r\n\r\n### `ComplexityMetrics`\r\nDataclass containing:\r\n- `name`: Function name\r\n- `type`: Type of code unit\r\n- `lines_of_code`: Line count\r\n- `cyclomatic_complexity`: Cyclomatic complexity score\r\n- `cognitive_complexity`: Cognitive complexity score\r\n- `nesting_depth`: Maximum nesting level\r\n- `parameters`: Parameter count\r\n- `returns`: Return statement count\r\n\r\n## Development\r\n\r\n```bash\r\n# Clone repository\r\ngit clone https://github.com/yourusername/codecomplexity.git\r\ncd codecomplexity\r\n\r\n# Install in development mode\r\npip install -e \".[dev]\"\r\n\r\n# Run tests\r\npytest\r\n\r\n# Format code\r\nblack .\r\n\r\n# Lint\r\nflake8\r\n```\r\n\r\n## Contributing\r\n\r\nContributions are welcome! Please feel free to submit a Pull Request.\r\n\r\n## License\r\n\r\nMIT License - see LICENSE file for details.\r\n\r\n## Acknowledgments\r\n\r\nInspired by various code complexity tools including McCabe, Radon, and SonarQube.\r\n",
"bugtrack_url": null,
"license": "MIT",
"summary": "A simple code complexity analyzer for Python",
"version": "0.1.2",
"project_urls": {
"Bug Tracker": "https://github.com/ayaanniaz/codecomplexity/issues",
"Homepage": "https://github.com/ayaanniaz/codecomplexity"
},
"split_keywords": [],
"urls": [
{
"comment_text": null,
"digests": {
"blake2b_256": "e5b1362a0c600234d5be63c9ab65b2ac3a4fae9afc7e36936d34186fc5eecef6",
"md5": "9af5cb5c1b333964c6dac1c36c4a4590",
"sha256": "10f9c751d5c8971b3a285e87a5d9662680ad1ac5e60cc62894176ebe1a467db2"
},
"downloads": -1,
"filename": "codecomplexity-0.1.2-py3-none-any.whl",
"has_sig": false,
"md5_digest": "9af5cb5c1b333964c6dac1c36c4a4590",
"packagetype": "bdist_wheel",
"python_version": "py3",
"requires_python": ">=3.8",
"size": 7214,
"upload_time": "2025-10-19T12:06:36",
"upload_time_iso_8601": "2025-10-19T12:06:36.338421Z",
"url": "https://files.pythonhosted.org/packages/e5/b1/362a0c600234d5be63c9ab65b2ac3a4fae9afc7e36936d34186fc5eecef6/codecomplexity-0.1.2-py3-none-any.whl",
"yanked": false,
"yanked_reason": null
},
{
"comment_text": null,
"digests": {
"blake2b_256": "ed00b219f96bfe3dd3e1c705140541c720c581fc71ff3bfc37798ece0863b15e",
"md5": "0ced795d53aafcb022659169c1c02061",
"sha256": "6acdfa2cb91aa88a310c38cace9860ef93190a0aa7d0a67bccab624b1bec66e0"
},
"downloads": -1,
"filename": "codecomplexity-0.1.2.tar.gz",
"has_sig": false,
"md5_digest": "0ced795d53aafcb022659169c1c02061",
"packagetype": "sdist",
"python_version": "source",
"requires_python": ">=3.8",
"size": 6285,
"upload_time": "2025-10-19T12:06:37",
"upload_time_iso_8601": "2025-10-19T12:06:37.550336Z",
"url": "https://files.pythonhosted.org/packages/ed/00/b219f96bfe3dd3e1c705140541c720c581fc71ff3bfc37798ece0863b15e/codecomplexity-0.1.2.tar.gz",
"yanked": false,
"yanked_reason": null
}
],
"upload_time": "2025-10-19 12:06:37",
"github": true,
"gitlab": false,
"bitbucket": false,
"codeberg": false,
"github_user": "yourusername",
"github_project": "codecomplexity",
"github_not_found": true,
"lcname": "codecomplexity"
}