Skip to main content

An extremely fast Python library to calculate the cognitive complexity of python files, written in Rust.

Project description

complexipy

complexipy

An extremely fast Python library to calculate the cognitive complexity of python files, written in Rust.

Quality Gate Package version


Documentation: https://rohaquinlop.github.io/complexipy/

Source Code: https://github.com/rohaquinlop/complexipy

PyPI: https://pypi.org/project/complexipy/


Requirements

  • Python >= 3.11
  • You also need to install git in your computer if you want to analyze a git repository.

Installation

pip install complexipy

Usage

To run complexipy you can use the following command:

complexipy .                         # Use complexipy to analyze the current directory and any subdirectories
complexipy path/to/directory         # Use complexipy to analyze a specific directory and any subdirectories
complexipy git_repository_url        # Use complexipy to analyze a git repository
complexipy path/to/file.py           # Use complexipy to analyze a specific file
complexipy path/to/file.py -m 20     # Use the -m option to set the maximum congnitive complexity, default is 15
complexipy path/to/directory -m 0    # Set the maximum cognitive complexity to 0 to disable the exit with error
complexipy path/to/directory -o      # Use the -o option to output the results to a CSV file, default is False

If the cognitive complexity of a file is greater than the maximum cognitive, then the return code will be 1 and exit with error, otherwise it will be 0.

Example

Analyzing a file

For example, given the following file:

def a_decorator(a, b):
    def inner(func):
        return func
    return inner

def b_decorator(a, b):
    def inner(func):
        if func:
            return None
        return func
    return inner

The cognitive complexity of the file is 1, and the output of the command complexipy path/to/file.py will be:

─────────────────────── complexipy 0.2.0 🐙 ───────────────────────
- Finished analysis in test_decorator.py
──────────────────── 🎉 Analysis completed!🎉 ─────────────────────
                       Summary
┏━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━━━━━━━━┳━━━━━━━━━━━━┓
┃ Path              ┃ File              ┃ Complexity ┃
┡━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━━━━━━━━╇━━━━━━━━━━━━┩
│ test_decorator.py │ test_decorator.py │ 1          │
└───────────────────┴───────────────────┴────────────┘
🧠 Total Cognitive Complexity in ./tests/test_decorator.py: 1
1 files analyzed in 0.0005 seconds

Output to a CSV file

If you want to output the results to a CSV file, you can use the -o option, this is really useful if you want to integrate complexipy with other tools, for example, a CI/CD pipeline. You will get the output in the console and will create a CSV file with the results of the analysis.

The filename will be complexipy.csv and will be saved in the current directory.

$ complexipy path/to/file.py -o

The output will be:

Path,File Name,Cognitive Complexity
test_decorator.py,test_decorator.py,1

Analyzing a directory

You can also analyze a directory, for example:

$ complexipy .

And complexipy will analyze all the files in the current directory and any subdirectories.

Analyzing a git repository

You can also analyze a git repository, for example:

$ complexipy https://github.com/rohaquinlop/complexipy

And to generate the output to a CSV file:

$ complexipy https://github.com/rohaquinlop/complexipy -o

License

This project is licensed under the MIT License - see the LICENSE file for details.

Acknowledgments

  • This project is inspired by the sonar way to calculate the cognitive complexity.

References

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

complexipy-0.2.0.tar.gz (17.5 kB view hashes)

Uploaded Source

Built Distributions

complexipy-0.2.0-cp313-cp313-manylinux_2_17_s390x.manylinux2014_s390x.whl (3.3 MB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ s390x

complexipy-0.2.0-cp313-cp313-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (3.3 MB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ ppc64le

complexipy-0.2.0-cp313-cp313-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (2.9 MB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ ARMv7l

complexipy-0.2.0-cp313-cp313-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.9 MB view hashes)

Uploaded CPython 3.13 manylinux: glibc 2.17+ ARM64

complexipy-0.2.0-cp312-none-win_amd64.whl (2.0 MB view hashes)

Uploaded CPython 3.12 Windows x86-64

complexipy-0.2.0-cp312-none-win32.whl (1.8 MB view hashes)

Uploaded CPython 3.12 Windows x86

complexipy-0.2.0-cp312-cp312-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ x86-64

complexipy-0.2.0-cp312-cp312-manylinux_2_17_s390x.manylinux2014_s390x.whl (3.3 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ s390x

complexipy-0.2.0-cp312-cp312-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (3.3 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ppc64le

complexipy-0.2.0-cp312-cp312-manylinux_2_17_i686.manylinux2014_i686.whl (3.1 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ i686

complexipy-0.2.0-cp312-cp312-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (2.9 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARMv7l

complexipy-0.2.0-cp312-cp312-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.9 MB view hashes)

Uploaded CPython 3.12 manylinux: glibc 2.17+ ARM64

complexipy-0.2.0-cp312-cp312-macosx_11_0_arm64.whl (2.0 MB view hashes)

Uploaded CPython 3.12 macOS 11.0+ ARM64

complexipy-0.2.0-cp312-cp312-macosx_10_12_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.12 macOS 10.12+ x86-64

complexipy-0.2.0-cp311-none-win_amd64.whl (2.0 MB view hashes)

Uploaded CPython 3.11 Windows x86-64

complexipy-0.2.0-cp311-none-win32.whl (1.8 MB view hashes)

Uploaded CPython 3.11 Windows x86

complexipy-0.2.0-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (3.0 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ x86-64

complexipy-0.2.0-cp311-cp311-manylinux_2_17_s390x.manylinux2014_s390x.whl (3.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ s390x

complexipy-0.2.0-cp311-cp311-manylinux_2_17_ppc64le.manylinux2014_ppc64le.whl (3.3 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ppc64le

complexipy-0.2.0-cp311-cp311-manylinux_2_17_i686.manylinux2014_i686.whl (3.1 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ i686

complexipy-0.2.0-cp311-cp311-manylinux_2_17_armv7l.manylinux2014_armv7l.whl (2.9 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARMv7l

complexipy-0.2.0-cp311-cp311-manylinux_2_17_aarch64.manylinux2014_aarch64.whl (2.9 MB view hashes)

Uploaded CPython 3.11 manylinux: glibc 2.17+ ARM64

complexipy-0.2.0-cp311-cp311-macosx_11_0_arm64.whl (2.0 MB view hashes)

Uploaded CPython 3.11 macOS 11.0+ ARM64

complexipy-0.2.0-cp311-cp311-macosx_10_12_x86_64.whl (2.1 MB view hashes)

Uploaded CPython 3.11 macOS 10.12+ x86-64

Supported by

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