Skip to main content

Python Cyclomatic Complexity Caculator

Installation | Usage | Examples

English | 简体中文

Features

Cyclomatic complexity is a code quality metric

  • multi charset support.(this may lead slow)
  • pretty output

Installation

pip install pcc-calc==0.0.2

you need download or build library first.

pcc ./tests/data/python3 
Do you want to creat [/your/path/pcc/.pcc] ? [y/N]: y
Create Dir [/your/path/pcc/.pcc] success!
Do you want to dowload one from [/your/path/pcc/.pcc/libpcc_ts_all.so] ? [y/N]: y
Download [/your/path/pcc/.pcc] success!
                    Code Cyclomatic Complexity Caculator                                       

| Complexity | Function          |                                                    File |
|------------|-------------------|---------------------------------------------------------|
|          6 | class             | /your/path/pcc/tests/data/python3/directory/class.py:15 |
|          6 | myfun             | /your/path/pcc/tests/data/python3/directory/class.py:32 |
|          6 | myfun2            | /your/path/pcc/tests/data/python3/directory/class.py:52 |
|          4 | function_in_file2 |  /your/path/pcc/tests/data/python3/directory/file2.py:1 |
|          2 | if_statment_0     |              /your/path/pcc/tests/data/python3/ifs.py:2 |
|          2 | if_statment_1     |              /your/path/pcc/tests/data/python3/ifs.py:8 |
|          2 | function_in_file1 |  /your/path/pcc/tests/data/python3/directory/file1.py:1 |
|          1 | get_url           |  /your/path/pcc/tests/data/python3/directory/class.py:7 |
|          1 | class             | /your/path/pcc/tests/data/python3/directory/class.py:12 |

Procee [4] files use Time: 0.022909164428710938 s

Usage

pcc --help
Usage: pcc [OPTIONS] CODE_PATH

  Cyclomatic Complexity Caculator

Options:
  -j, --jobs INTEGER            Parallel jobs count  [default: 10]
  -c, --count INTEGER           Output item count  [default: 10]
  -s, --sort BOOLEAN            sort by Complexity  [default: True]
  -e, --exclude_path DIRECTORY  exclude path
  --lib_path DIRECTORY          tree-sitter library path  [default: .pcc/]
  --version
  --help                        Show this message and exit.

Examples

caculate one file

pcc ./pcc/pcc.py
                               Code Cyclomatic Complexity Caculator

| Complexity | Function                       |                           File |
|------------|--------------------------------|--------------------------------|
|          2 | get_vistor                     |/your/project/pcc/pcc/pcc.py:19 |
|          2 | get_parser                     |/your/project/pcc/pcc/pcc.py:26 |
|          1 | detect_file_lanuage            |/your/project/pcc/pcc/pcc.py:14 |
|          1 | parse_code                     |/your/project/pcc/pcc/pcc.py:33 |
|          1 | calc_code_complexity           |/your/project/pcc/pcc/pcc.py:37 |
|          1 | calc_code_complexity_from_file |/your/project/pcc/pcc/pcc.py:49 |

caculate directory

pcc . -j 8 -e ./venv -e ./docs -e ./languages
                                        Code Cyclomatic Complexity Caculator

| Complexity | Function              |                                                      File |
|------------|-----------------------|-----------------------------------------------------------|
|          7 | class                 |            /your/project/pcc/pcc/python/vistor_impl.py:94 |
|          7 | class                 |           /your/project/pcc/pcc/python/vistor_impl.py:117 |
|          7 | class                 |  /your/project/pcc/build/lib/pcc/python/vistor_impl.py:94 |
|          7 | class                 | /your/project/pcc/build/lib/pcc/python/vistor_impl.py:117 |
|          6 | travel_dir_and_filter |                         /your/project/pcc/pcc/utils.py:33 |
|          6 | check_or_download     |                           /your/project/pcc/pcc/cli.py:38 |
|          6 | class                 |/your/project/pcc/tests/data/python3/directory/class.py:15 |
|          6 | myfun                 |/your/project/pcc/tests/data/python3/directory/class.py:32 |
|          6 | myfun2                |/your/project/pcc/tests/data/python3/directory/class.py:52 |
|          6 | travel_dir_and_filter |               /your/project/pcc/build/lib/pcc/utils.py:33 |

Procee [37] files use Time: 0.04288077354431152 s

as u can see, -e can exclude directory you don't want, and can use multi times.

multilang support

$ pcc ./tests/data/       
                  Code Cyclomatic Complexity Caculator                                       

| Complexity | Function          |                                                                         File |
|------------|-------------------|------------------------------------------------------------------------------|
|          6 | class             | /home/sss/cyclomatic_complexity/pcc/tests/data/python3/directory/class.py:15 |
|          6 | myfun             | /home/sss/cyclomatic_complexity/pcc/tests/data/python3/directory/class.py:32 |
|          6 | myfun2            | /home/sss/cyclomatic_complexity/pcc/tests/data/python3/directory/class.py:52 |
|          4 | function_in_file2 |  /home/sss/cyclomatic_complexity/pcc/tests/data/python3/directory/file2.py:1 |
|          4 | ifs_c_func        |                     /home/sss/cyclomatic_complexity/pcc/tests/data/c/ifs.c:1 |
|          4 | if_c              |            /home/sss/cyclomatic_complexity/pcc/tests/data/c/directory/if.c:1 |
|          3 | for_c             |           /home/sss/cyclomatic_complexity/pcc/tests/data/c/directory/for.c:1 |
|          3 | if_while_c        |         /home/sss/cyclomatic_complexity/pcc/tests/data/c/directory/while.c:1 |
|          2 | if_statment_0     |              /home/sss/cyclomatic_complexity/pcc/tests/data/python3/ifs.py:2 |
|          2 | if_statment_1     |              /home/sss/cyclomatic_complexity/pcc/tests/data/python3/ifs.py:8 |

Development

download repo and initialize environment

python3 -m venv venv
source ./venv/bin/activate
pip3 install -r requirements_dev.txt
# or use aliyun
pip3 install -r requirements_dev.txt -i https://mirrors.aliyun.com/pypi/simple/
git submodule update --init

compile dynamic library

make lib

run test

make test

How to add new language support

  1. add tree-sitter-parser project to languages dir
  2. build new language so
  3. add new dir in pcc dir like python.
  4. implement node, parser, vistor for new language
  5. add unit test

TodoList

  • add json output for future analyze
  • add macos, windows support
  • do more test
  • comment support,for ignore function
  • select language and sort group by language

Reference

Cookiecutter

audreyr/cookiecutter-pypackage

cyclomatic complexities Wiki

gocyclo

tree-sitter

py-tree-sitter

mccabe

cccalculator

History

0.0.1 (2022-09-04)

  • First release on PyPI.

0.0.2 (2022-09-05)

  • Add C language support.

Metadata

Release files for pcc-calc 0.0.2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pcc-calc 0.0.2
File Size Uploaded Provenance
pcc_calc-0.0.2.tar.gz 152.6 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for pcc-calc 0.0.2
File Interpreter ABI Platform
pcc_calc-0.0.2-py2.py3-none-any.whl Python 2, Python 3 none any Details

Total release size: 168.9 kB

Release files / pcc_calc-0.0.2.tar.gz

Download URL pcc_calc-0.0.2.tar.gz
Size 152.6 kB
Tags Source
SHA-256 checksum
How to use checksums
8c0728d631c3de50024fd0a27627b5242fc4516803c9ab33dcb41d5f52b79b39
BLAKE2b-256 checksum
How to use checksums
36c2eee901e1c9d1ac48eae78b875c0fbddf5ae90f31a5f7340d4f1d2231bfe2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.8.3 requests/2.28.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.10

Release files / pcc_calc-0.0.2-py2.py3-none-any.whl

Download URL pcc_calc-0.0.2-py2.py3-none-any.whl
Size 16.4 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f80ce5ca598ab144e68ebfaaef367ec19592c0f310c292842a3ac790660a5343
BLAKE2b-256 checksum
How to use checksums
72792f15546d08b6a5d0bfae0dcb147103e5e1cbb68607fce67c04a360418a57
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/1.14.0 pkginfo/1.8.3 requests/2.28.1 setuptools/44.0.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.8.10

Security for pcc-calc 0.0.2

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.0.2 This release

2 release files No provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page