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:
- Clone the tree-sitter-python repository
- Build the tree-sitter grammar
- 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)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| codemetrica-0.1.0.tar.gz | 14.7 kB | Without provenance | Details for codemetrica-0.1.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| codemetrica-0.1.0-py3-none-any.whl | Python 3 | none | any | Details for codemetrica-0.1.0-py3-none-any.whl |
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
|
Security for codemetrica 0.1.0
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.