Skip to main content

A package for calculating metrics and detecting code smells

Project description

Codemetrica

A Python library for calculating code metrics and detecting code smells in Python codebases using tree-sitter.

Features

  • Comprehensive code metrics calculation
  • Code smell detection
  • Support for analyzing Python files, classes, and methods
  • Easy-to-use API
  • Powered by tree-sitter for robust parsing

Prerequisites

  • Python 3.10 or higher
  • GCC compiler (for building tree-sitter grammar)
  • Git (for cloning tree-sitter repository)

Installation

# Clone the repository
git clone https://github.com/yourusername/codemetrica.git
cd codemetrica

# Install the package
pip install -e .

The installation process will:

  1. Clone the tree-sitter-python repository
  2. Build the tree-sitter grammar
  3. Install the package in development mode

Quick Start

from codemetrica import File, GodClass

# Analyze a Python file
f = File("example.py")

# Get all classes in the file
classes = f.get_classes()

# Check for code smells
for class_obj in classes:
    if GodClass.detect(class_obj):
        print(f"Found God Class: {class_obj.name}")

Available Metrics

Size Metrics

  • Lines of Code (LOC)
  • Source Lines of Code (SLOC)
  • Kilo Lines of Code (KLOC)
  • Comment Lines of Code (CLOC)
  • Number of Methods (NOM)
  • Number of Classes (NOC)
  • Number of Fields (NOF)
  • Number of Public Methods (NOPM)

Complexity Metrics

  • Cyclomatic Complexity (CC)
  • Weighted Methods per Class (WMC)
  • NPath Complexity
  • Essential Complexity
  • Halstead Metrics
  • Nested Block Depth (NBD)
  • Decision Density

Object-Oriented Metrics

  • Depth of Inheritance Tree (DIT)
  • Number of Children (NOC)
  • Response for a Class (RFC)
  • Coupling Between Objects (CBO)
  • Lack of Cohesion of Methods (LCOM)
  • And more...

Available Code Smells

General Smells

  • Broken Modularization
  • Complex Conditional
  • Deficient Encapsulation
  • Dispersed Coupling
  • Empty catch clause
  • And more...

Class Level Smells

  • Brain Class
  • Class Data Should Be Private
  • Complex Class
  • Data Class
  • God Class
  • Large Class
  • Lazy Class

Method Level Smells

  • Abstract Function Call From Constructor
  • Brain Method
  • Complex Method
  • Long Method
  • Long Parameter List

Development

To build the tree-sitter grammar manually:

# Clone tree-sitter-python if not already done
git clone https://github.com/tree-sitter/tree-sitter-python.git

# Build the grammar
gcc -shared -o build/my-languages.so -I./tree-sitter-python/src tree-sitter-python/src/parser.c -fPIC

License

MIT License

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

codemetrica-0.1.0.tar.gz (14.7 kB view details)

Uploaded Source

Built Distribution

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

codemetrica-0.1.0-py3-none-any.whl (14.7 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: codemetrica-0.1.0.tar.gz
  • Upload date:
  • Size: 14.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for codemetrica-0.1.0.tar.gz
Algorithm Hash digest
SHA256 129a76064d0b7dcfd9fbad4d3041b499459779127ee80b136f60e017ac01b31a
MD5 82b9ca8d2bd997d70d706058e6eb9408
BLAKE2b-256 cc720ebd0ea79262b1d435b09d7aeb710d1e52c8ea6ed1f0c938bf7e22a8b5c2

See more details on using hashes here.

File details

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

File metadata

  • Download URL: codemetrica-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 14.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.11.9

File hashes

Hashes for codemetrica-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4d794b8e5498854dc7f214656eacd8bb5bd02934031ebd77c0ff51a1c8b7d154
MD5 c49a6473cb59bdd94f396513b91f911f
BLAKE2b-256 430600e79f4428a17715bf1d82230407559aafb6c396eaf0a89e07a86422eb89

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