Python PyCodeMetrics

analyzes size and complexity of Rust, C++, C#, and Python

A lightweight Python command-line tool that reports size and complexity metrics for source code files in a project tree.

Metrics

MetricDefinition
LinesTotal line count (code + comments + blank lines)
ScopesNumber of scope-opening tokens — { for brace languages, lines ending with : for Python

Supported Languages

Extension(s)Strategy
.pyPython (: line endings)
.csC# (brace count)
.cpp, .c, .h, .hpp, .hxx, .cxx, .ixxC/C++ (brace count)
.rsRust (brace count)
.js, .ts, .jsx, .tsxJavaScript / TypeScript (brace count)
.java, .goJava, Go (brace count)

Usage

python code_metrics.py [path] [--html] [--html-only] [--no-recurse]
ArgumentDescription
pathProject root directory or source file (default: current directory)
--htmlPrint text table followed by HTML table
--html-onlyPrint only the HTML table
--no-recurseScan only the top-level directory, not subdirectories

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

Text output sample

  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    |
+-----------------------------+-------+--------+

Skipped Directories

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

Requirements

  • Python 3.9+
  • No third-party packages required

License

MIT © Jim Fawcett