Code Repos

PyCodeMetrics

lightweight Python tool reporting size and complexity metrics for source trees

1.  PyCodeMetrics

PyCodeMetrics is a lightweight Python command-line tool that reports size and complexity metrics for source code files in a project tree. It requires no third-party packages and works with Python 3.9 or later.

1.1  Metrics

Metric Definition
Lines Total line count (code + comments + blank lines)
Scopes Number of scope-opening tokens - { for brace languages, lines ending with : for Python

1.2  Supported Languages

Extension(s) Strategy
.py Python (: line endings)
.cs C# (brace count)
.cpp, .c, .h, .hpp, .hxx, .cxx, .ixx C/C++ (brace count)
.rs Rust (brace count)
.js, .ts, .jsx, .tsx JavaScript / TypeScript (brace count)
.java, .go Java, Go (brace count)

1.3  Usage

python code_metrics.py [path] [--html] [--html-only] [--no-recurse]
Argument Description
path Project root directory or source file (default: current directory)
--html Print text table followed by HTML table
--html-only Print only the HTML table
--no-recurse Scan only the top-level directory, not subdirectories

1.4  Examples

# Metrics for the current directory tree
python code_metrics.py

# Metrics for a specific project
python code_metrics.py C:/github/JimFawcett/NewSite/Code/Projects/TextFinder/CppTextFinder

# Output only the HTML table (for embedding in a web page)
python code_metrics.py C:/github/JimFawcett/NewSite/Code/Projects/TextFinder/CppTextFinder --html-only

# Scan top-level files only
python code_metrics.py . --no-recurse
Sample text output:
  C:\github\JimFawcett\NewSite\Code\Projects\TextFinder\CppTextFinder
+-----------------------------+-------+--------+
| File                        | Lines | Scopes |
+-----------------------------+-------+--------+
| CommandLine\src\CmdLine.ixx | 126   | 29     |
| CommandLine\src\test.cpp    | 225   | 79     |
| DirNav\src\DirNav.ixx       | 126   | 19     |
| DirNav\src\test.cpp         | 491   | 82     |
| EntryPoint\src\main.cpp     | 67    | 8      |
| EntryPoint\src\test.cpp     | 435   | 59     |
| generate_part.py            | 339   | 28     |
| Output\src\Output.ixx       | 110   | 17     |
| Output\src\test.cpp         | 469   | 70     |
+-----------------------------+-------+--------+
| TOTAL                       | 2388  | 391    |
+-----------------------------+-------+--------+

1.5  Skipped Directories

The tool automatically skips build output and toolchain directories: bin, obj, target, build, out, __pycache__, .venv, venv, dist, .git, .vs, .idea, archive

1.6  Requirements

Requirement Details
Python 3.9 or later
Packages None - no third-party packages required