Skip to main content

A green code smell detection package

Project description

PyGreenSense

PyGreenSense is a Python code analysis tool that detects code smells and can optionally track carbon/emission metrics during execution.

Features

  • God Class Detection - Identifies classes that are too large or complex
  • Duplicated Code Detection - Finds similar code blocks across your project
  • Long Method Detection - Detects methods that exceed size/complexity thresholds
  • Dead Code Detection - Identifies unreachable/unused definitions
  • Mutable Default Arguments - Warns about mutable default arguments in functions
  • Carbon Emissions Tracking - Tracks the carbon footprint of execution (optional)

Setup (venv)

python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
pip install -e .

If codecarbon is missing, install it:

pip install codecarbon

Run the Library

Installed command

pygreensense .
pygreensense ./src
pygreensense ./some_file.py

Local source command (without install)

python -m src.green_code_smell.cli .

Common options

pygreensense . --no-god-class
pygreensense . --no-dup-check
pygreensense . --no-long-method
pygreensense . --no-dead-code
pygreensense . --no-mutable-default
pygreensense . --no-carbon

Rule tuning examples

pygreensense . --max-methods 8 --max-cc 30 --max-loc 150
pygreensense . --dup-similarity 0.80 --dup-min-statements 5
pygreensense . --method-max-loc 30 --max-cyclomatic 3

Run Unit Tests

source .venv/bin/activate
pytest tests/unit

Run tests with coverage

pytest tests/unit --cov=src/green_code_smell --cov-report=term-missing

Carbon Emission Comparison Validation

This repository includes a comparison note in carbon_emission_validate to cross-check carbon readings between approaches.

Snapshot from carbon_emission_validate

  • pygreensense
    • Average carbon emission: 1.7463339e-07 kg CO2
    • Min carbon emission: 1.516343e-07 kg CO2
    • Max carbon emission: 2.863806e-07 kg CO2
    • Average diff between consecutive runs (signed): -4.5652333e-09 kg CO2
    • Average absolute diff between consecutive runs: 3.8392633e-08 kg CO2
  • codeCarbon
    • Average carbon emission: 2.488713665990e-09 kgCO2eq
    • Min carbon emission: 1.404247309062e-09 kgCO2eq
    • Max carbon emission: 4.240361297175e-09 kgCO2eq
    • Average diff (signed): -3.151237764570e-10 kgCO2eq
    • Average absolute diff: 5.768756903969e-10 kgCO2eq

Using tests/validate_parity.py to validate parity between both methods:

  • Direct avg: 9.085950555049e-06 kgCO2eq
  • Lib avg: 9.032015317825e-06 kgCO2eq
  • Mean absolute percentage difference: 0.59%
  • Ratio (lib/direct): 0.9941x

This parity check isolates carbon-emission workflows and helps verify that library-level metrics are close to direct measurements.

Output

The tool provides:

  • Issue location (file and line number)
  • Rule/category
  • Human-readable issue description
  • Summary statistics grouped by rule

License

MIT License - see LICENSE.

Support

For bugs, feature requests, or questions: https://github.com/chocody/green_code_smell/issues

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

pygreensense-0.0.5.tar.gz (24.8 kB view details)

Uploaded Source

Built Distribution

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

pygreensense-0.0.5-py3-none-any.whl (26.7 kB view details)

Uploaded Python 3

File details

Details for the file pygreensense-0.0.5.tar.gz.

File metadata

  • Download URL: pygreensense-0.0.5.tar.gz
  • Upload date:
  • Size: 24.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for pygreensense-0.0.5.tar.gz
Algorithm Hash digest
SHA256 52b00759a30d3a7bf277c66cc9d1a2a64e28d35b4f968dc475533a31a716207b
MD5 aa27acdda086eea5704180d4ca665225
BLAKE2b-256 95539f88897d211731a750bb6021c6a32d4ab7996b4f76c23e42eff553022cf6

See more details on using hashes here.

File details

Details for the file pygreensense-0.0.5-py3-none-any.whl.

File metadata

  • Download URL: pygreensense-0.0.5-py3-none-any.whl
  • Upload date:
  • Size: 26.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.9.6

File hashes

Hashes for pygreensense-0.0.5-py3-none-any.whl
Algorithm Hash digest
SHA256 aa0d44f66c50c5fb446578e634eaf4041673166d60ba3c86059efc24a1f6373d
MD5 55bfe6ac77811f6d69e724fbd0b459f4
BLAKE2b-256 01bd7d562ae3cb25ce2a14142f00a186f3de6c60bc14683125aefb4457190e11

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