Skip to main content

Codebase metrics and analysis tool for Python projects

Project description

Pymetrica

Tests Status PyPI version Python

pre-commit Ruff Pylint Type Checked

GitHub stars Downloads License

Pymetrica is a static analysis tool that computes software engineering metrics for Python codebases.

It parses Python source code using the AST (Abstract Syntax Tree) and evaluates classical metrics used to assess complexity, maintainability, and architectural stability.

The tool provides a modular architecture, a CLI interface, and extensible reporting to help developers understand the structural quality of their Python projects.

Repository: https://github.com/JuanJFarina/pymetrica


Example

Analyze a Python project:

pymetrica run-all path/to/project

Example output:

Metric: Abstract Lines Of Code
aloc_number: 67
aloc_percentage: 14.89

Metric: Cyclomatic Complexity
cc_number: 156
lloc_per_cc: 2.89

Metric: Halstead Volume
hv_number: 5423.67

Metric: Maintainability Cost
maintainability_cost: 24.67

Pymetrica can also analyze architecture layers and dependencies within the codebase.


Contents

  • Features
  • Why Pymetrica
  • Metrics
  • Installation
  • Quick Start
  • CLI Commands
  • Architecture Overview
  • Architecture Diagram Generation
  • Testing
  • Contributing
  • License

Features

  • Static analysis of Python projects using the AST
  • Logical Lines of Code (LLOC) analysis
  • Comment density statistics
  • Layered architecture detection based on directories
  • Multiple classical software engineering metrics
  • CLI interface for fast inspection of codebases
  • Optional Mermaid architecture diagrams
  • Extensible metric and reporting system

Why Pymetrica?

Several tools compute Python complexity metrics (such as radon, lizard, or SonarQube integrations). Pymetrica focuses on a different goal: architecture-aware metric analysis.

Unlike many static analysis tools, Pymetrica:

  • groups metrics by codebase layers derived from directory structure
  • computes cross-layer coupling and instability metrics
  • produces architecture diagrams alongside metric results
  • provides a modular framework for implementing new metrics

This makes it useful not only for measuring complexity, but also for analyzing architectural quality in Python projects.


Metrics

Pymetrica implements several classical software engineering metrics.

Abstract Lines of Code (ALOC)

Measures the amount of abstraction and indirection in the codebase by counting abstract constructs such as definitions and structural components.

High ALOC ratios may indicate excessive abstraction or over-engineering.


Cyclomatic Complexity (CC)

Measures the number of independent execution paths in a program.

Calculated by analyzing control flow structures including:

  • conditionals
  • loops
  • exception handling
  • boolean logic

Higher values correspond to more complex and harder-to-maintain code.


Halstead Volume (HV)

Measures implementation complexity based on operators and operands used in the program.

Derived from:

  • program vocabulary
  • program length
  • token frequency

Maintainability Cost (MC)

A composite metric derived from:

  • Cyclomatic Complexity
  • Halstead Volume
  • Logical Lines of Code

It estimates the expected maintenance effort required for the codebase.

Lower scores indicate better maintainability.


Instability (LI)

Measures package coupling and architectural stability based on import dependencies.

Instability is defined as:

Instability = Efferent Coupling / (Afferent Coupling + Efferent Coupling)

Values range from:

  • 0 → Stable
  • 1 → Unstable

Installation

Requires Python 3.10 or newer.

Install from source:

git clone https://github.com/JuanJFarina/pymetrica
cd pymetrica
pip install -e .

After installation the CLI command becomes available:

pymetrica

Quick Start

Analyze a Python project:

pymetrica run-all path/to/project

For an initial overview of a codebase:

pymetrica base-stats path/to/project

CLI Commands

pymetrica status
pymetrica base-stats
pymetrica aloc
pymetrica cc
pymetrica hv
pymetrica mc
pymetrica li
pymetrica run-all

Typical usage pattern:

pymetrica <command> DIR_PATH

Architecture Overview

Pymetrica is built around a modular analysis pipeline.

Codebase Parsing
        ↓
Code Representation
        ↓
Metric Calculators
        ↓
Results
        ↓
Report Generators

Core components include:

Parser

Recursively scans .py files and builds a structured representation of the codebase.

Extracted information includes:

  • logical lines of code
  • comment lines
  • classes and functions
  • directory structure

Files containing syntax errors are automatically skipped.


Data Models

Core data structures are implemented using Pydantic models.

Main models include:

  • Code – representation of a Python file
  • Codebase – full project structure
  • Metric – container for metric metadata and results
  • Results – structured metric outputs

Metric Calculators

Each metric is implemented as a subclass of an abstract MetricCalculator.

This design makes it easy to extend the system with additional metrics.


Reporting

Metrics are rendered through pluggable report generators.

Currently supported:

  • terminal summaries
  • detailed reports

Future formats may include JSON, Markdown, or CI-friendly outputs.


Architecture Diagram Generation

Pymetrica can generate Mermaid diagrams representing the layered architecture of a codebase.

pymetrica base-stats --diagram path/to/project

This creates a .mmd file that can be rendered using:

  • Mermaid Live Editor
  • VSCode Mermaid extensions
  • documentation pipelines

Testing

Tests are implemented using pytest and mirror the project structure.

Run tests with:

pytest

Contributing

Contributions are welcome.

If you want to:

  • implement a new metric
  • improve the parser
  • extend reporting capabilities

feel free to open an issue or submit a pull request.


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

pymetrica-1.0.1.tar.gz (44.5 kB view details)

Uploaded Source

Built Distribution

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

pymetrica-1.0.1-py3-none-any.whl (34.6 kB view details)

Uploaded Python 3

File details

Details for the file pymetrica-1.0.1.tar.gz.

File metadata

  • Download URL: pymetrica-1.0.1.tar.gz
  • Upload date:
  • Size: 44.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.15

File hashes

Hashes for pymetrica-1.0.1.tar.gz
Algorithm Hash digest
SHA256 ee483b5a27f3468f7b79c45aa534bb46fb83ba232c9305874470bce3ae585a9e
MD5 369a50820a5c66380eaaae71a6ce4240
BLAKE2b-256 9205c76d11739730417474d0f8d3323b45dc1adc23957688303e29994d539b3a

See more details on using hashes here.

File details

Details for the file pymetrica-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: pymetrica-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 34.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.15

File hashes

Hashes for pymetrica-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 42eaf8b06835c184e437ee373dc6b599fb0aba9b0027077f9c2777d714800321
MD5 b9278f935e4d8aac7611158beb117c08
BLAKE2b-256 62d645b8f61ed4ca8ee103f8e62a60e0dc4b9bd4f8fed1e90e63e9ad3214b41d

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