Skip to main content

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

Metadata

Release files for codemetrica 0.1.0

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

Source distribution (sdist)

Source distribution for codemetrica 0.1.0
File Size Uploaded
codemetrica-0.1.0.tar.gz 14.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for codemetrica 0.1.0
File Interpreter ABI Platform
codemetrica-0.1.0-py3-none-any.whl Python 3 none any Details

Total release size: 29.3 kB

Release files / codemetrica-0.1.0.tar.gz

Download URL codemetrica-0.1.0.tar.gz
Size 14.7 kB
Tags Source
SHA-256 checksum
How to use checksums
129a76064d0b7dcfd9fbad4d3041b499459779127ee80b136f60e017ac01b31a
BLAKE2b-256 checksum
How to use checksums
cc720ebd0ea79262b1d435b09d7aeb710d1e52c8ea6ed1f0c938bf7e22a8b5c2
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release files / codemetrica-0.1.0-py3-none-any.whl

Download URL codemetrica-0.1.0-py3-none-any.whl
Size 14.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
4d794b8e5498854dc7f214656eacd8bb5bd02934031ebd77c0ff51a1c8b7d154
BLAKE2b-256 checksum
How to use checksums
430600e79f4428a17715bf1d82230407559aafb6c396eaf0a89e07a86422eb89
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.11.9

Release history Release notifications | RSS feed

This release

0.1.0 This release

2 release files

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