Skip to main content

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 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

# 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


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

codecomplexity-0.1.0.tar.gz (6.2 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

codecomplexity-0.1.0-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

Details for the file codecomplexity-0.1.0.tar.gz.

File metadata

  • Download URL: codecomplexity-0.1.0.tar.gz
  • Upload date:
  • Size: 6.2 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.8

File hashes

Hashes for codecomplexity-0.1.0.tar.gz
Algorithm Hash digest
SHA256 35ea233de256d503c4215160f63a164bccbe88f4a1209638ff315abf868fa35a
MD5 0488782a6ada751abe46be09f93b342c
BLAKE2b-256 a40fa925db52f25c25e6b3ee317229198f70a49272f79b6ae8b55ef3836ffb9f

See more details on using hashes here.

File details

Details for the file codecomplexity-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: codecomplexity-0.1.0-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

Hashes for codecomplexity-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 6b4393f312f9814a4c64f40988d06d8870add646e06aa2eb7f2ad8514868cf7f
MD5 4e7f31f3b854f9d10aeef980a4c34f00
BLAKE2b-256 a7bb6b838d5498eaca4ba933a0b10004cd53e1bee272cacda8bce7b7473c7067

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page