Measure your code
I found a cool tool today to measure code: metrics. It measures SLOC, comments and cyclomatic complexity. It’s easy to install: pip install pygments metrics
Run this in your project’s root:
metrics -v `find . -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print`
I have a django project so I added \! -path "*/migrations/*.py" to skip any files that are in a migrations dir (I’d skip the automatically generated south migrations).
You probably bundle other libraries or apps in your source tree (eg: jquery or that nice django app the author didn’t bother to make a setup.py script for) so you want to measure only some specific paths. Eg, to collect stats only for files in src/foobar, lib/tools and src/otherbar:
metrics -v `find src/foobar lib/tools src/otherbar -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print`
If you work on multiple projects you can make a script or alias for this:
metrics -v `find \`cat METRICS\` -type f \( -iname "*.css" -or -iname "*.py" -or -iname "*.js" -or -iname "*.html" -or -iname "*.txt" \) \! -path "*/migrations/*.py" -print`
And in each project just save a
METRICS file with the list of paths.
I get something like this for one random project:
Metrics Summary:
Files Language SLOC Comment McCabe
----- ------------------------------ ----------- ------- ------
129 Python 4831 289 261
2 Text only 0 0 0
49 HTML+Django/Jinja 1381 19 166
7 JavaScript 2204 231 352
21 CSS 1839 111 0
----- ------------------------------ ----------- ------- ------
208 Total 10255 650 779
Do McCabe (aka cyclomatic complexity) ratios (McCabe/(SLOC-Comment)) look odd ? (0.17 for javascript and and 0.05 for python).
Do you know other measurement tools adequate for Django projects ? Do tell me and, if possible, how to run it for specific files like above (I’m lazy :)
uhm sloccount?
Cool little utility! :) I’ll add this to my bag
of tools!
Metrics Summary:
Files Language SLOC Comment McCabe
—– —————————— ———– ——- ——
70 Python 5975 2080 1128
—– —————————— ———– ——- ——
70 Total 5975 2080 1128
^^^ circuits :)
@netblockalin: SLOCCOUNT is Source Lines Of Code Count
–JamesMills / prologic
@JamesMills and `sloccount` is an existing tool to count SLOC http://www.dwheeler.com/sloccount/
Wonderful, keep it up thanks.
Thank you..really informative!!
This looks really cool. Also, it reminds me that I keep wishing I knew a tool to draw diagrams of my modules interdependencies. Is there such a thing, or should I be thinking about whipping up my own, maybe my examining static imports and producing output for graphvis or somesuch?
I’m second to none in trumpeting all things Python, but cloc is awesome: http://cloc.sourceforge.net/. I humbly suggest that it’s the gold standard for this kind of utility.
For the command line, you can simplify and avoid quoting issues if you use a pipe and xargs. The -print0 option for find and the -0 option for xargs are useful. Equivalent to your first one:
find . -type f \( -iname “*.css” -or -iname “*.py” -or -iname “*.js” -or -iname “*.html” -or -iname “*.txt” \) \! -path “*/migrations/*.py” -print0 | xargs -0 metrics -v
This is much more unixy, and will also deal with ‘funny’ filenames like those with spaces in them.