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.1.tar.gz (6.3 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.1-py3-none-any.whl (7.2 kB view details)

Uploaded Python 3

File details

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

File metadata

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

Hashes for codecomplexity-0.1.1.tar.gz
Algorithm Hash digest
SHA256 97e4698f3f878b0edc6c7094f38698e19f0245281786fe9a4f478e21a94706b9
MD5 79d34de67c2ec5914036865812ce30d9
BLAKE2b-256 b7e7ce86543a179112d5b2d652fd632c488dc7a050ac5d64f99e4eb566bdfcf2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codecomplexity-0.1.1-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.1-py3-none-any.whl
Algorithm Hash digest
SHA256 c62c336829bd8a53468373ee4c9a01793adb7aff0d12bf3a3b54083930d749d3
MD5 b5864ffa1be4321b6629a1e7d5a6946d
BLAKE2b-256 d57d234b6cc497185b2a97952bdcc34297f3fca07deb49fadcd6bd9af066e9b0

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