Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Quick way to calculate lines of code/comments in a collection of Python scripts?

77 views
Skip to first unread message

Malcolm Greene

unread,
Oct 5, 2016, 1:57:14 PM10/5/16
to
Looking for a quick way to calculate lines of code/comments in a
collection of Python scripts. This isn't a LOC per day per developer
type analysis - I'm looking for a metric to quickly judge the complexity
of a set of scripts I'm inheriting.

Thank you,
Malcolm

Chris Angelico

unread,
Oct 5, 2016, 2:09:13 PM10/5/16
to
Post the code to GitHub, then click on the file. Up the top, you'll
see something like this:

https://github.com/Rosuav/LetMeKnow/blob/master/letmeknow.py
231 lines (212 sloc)

The first figure is the raw line count. The second is, in theory at
least, the number of lines of "actual code".

Cheating? Totally. Effective? Yup.

ChrisA

Brandon McCaig

unread,
Oct 20, 2016, 4:42:23 PM10/20/16
to
(Sorry for the late reply)

On Wed, Oct 05, 2016 at 01:56:59PM -0400, Malcolm Greene wrote:
> Looking for a quick way to calculate lines of code/comments in a
> collection of Python scripts. This isn't a LOC per day per developer
> type analysis - I'm looking for a metric to quickly judge the complexity
> of a set of scripts I'm inheriting.

There is a CPAN module for a Perl application that calculates
SLOC. You or somebody else may find it useful for this task...

I believe it is packaged for some Linux distros. If applicable
you should be able to just install it:

sudo aptitude install cloc

Otherwise, if you have Perl installed already (e.g., *nix) and
already have a CPAN client [configured] it should be relatively
easy. cpanm is the most user-friendly client I've used.

cpanm App::cloc

Alternatively, you can do it with 'cpan' too, but that will
typically prompt you 3 million times... There are a few other
clients available. Use whatever suits you. You could also fetch
the module directly from CPAN and install it manually if so
inclined.

If you have none of these things (e.g., Windows) you could
install the free software Strawberry Perl distribution. It comes
with batteries included. If you're lucky cpanm will just work(tm)
to install it from there.

Hope that helps...

Regards,


--
Brandon McCaig <bamc...@gmail.com> <bam...@castopulence.org>
Castopulence Software <https://www.castopulence.org/>
Blog <http://www.bambams.ca/>
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'

signature.asc

John Strick

unread,
Oct 20, 2016, 5:50:13 PM10/20/16
to
pylint will report on lines of code, comments, blank lines, # of modules, functions, etc. It will also report long lines, non-standard object names, etc. I think it will answer some of your questions.

https://www.pylint.org/

Just install and use:

pylint *.py


Regards,
John

Tomasz Rola

unread,
Oct 23, 2016, 5:00:40 PM10/23/16
to
On Wed, Oct 05, 2016 at 01:56:59PM -0400, Malcolm Greene wrote:
> Looking for a quick way to calculate lines of code/comments in a
> collection of Python scripts. This isn't a LOC per day per developer
> type analysis - I'm looking for a metric to quickly judge the complexity
> of a set of scripts I'm inheriting.
>
> Thank you,
> Malcolm

A bit more than what you asked for (and sorry for being late) but I
find sloccount quite good. Or at least interesting (computes sloc and
some stats about project, given project dir or a single file with
code):

http://www.dwheeler.com/sloccount/

--
Regards,
Tomasz Rola

--
** A C programmer asked whether computer had Buddha's nature. **
** As the answer, master did "rm -rif" on the programmer's home **
** directory. And then the C programmer became enlightened... **
** **
** Tomasz Rola mailto:tomas...@bigfoot.com **

Stephen Tucker

unread,
Oct 24, 2016, 5:03:55 AM10/24/16
to
Tomasz,

How about using the command prompt command FIND /C on each of your source
files as follows:

FIND/C "#" <SourceFile.py >>NumbersOfLinesContainingPythonComments.dat
FIND/C /V "#" <SourceFile.py >>NumbersOfLinesNotContainingPythonComments.dat

You would end up with two files each with a column of line counts;

Import these lines into an Excel Spreadsheet and calculate whatever you
like with them.

Stephen.
> --
> https://mail.python.org/mailman/listinfo/python-list
>

MrJean1

unread,
Oct 24, 2016, 4:20:28 PM10/24/16
to
Here is a basic LOC counter for Python source files

<https://code.activestate.com/recipes/580709-lines-of-code-loc/

/Jean

Tomasz Rola

unread,
Oct 24, 2016, 6:05:58 PM10/24/16
to
On Mon, Oct 24, 2016 at 10:03:29AM +0100, Stephen Tucker wrote:
> Tomasz,
>
> How about using the command prompt command FIND /C on each of your source
> files as follows:
>
> FIND/C "#" <SourceFile.py >>NumbersOfLinesContainingPythonComments.dat
> FIND/C /V "#" <SourceFile.py >>NumbersOfLinesNotContainingPythonComments.dat
>
> You would end up with two files each with a column of line counts;
>
> Import these lines into an Excel Spreadsheet and calculate whatever you
> like with them.

If this is what you really want to do, then why not. Albeit I would
rather go with sh script for this, with ability to process either a
directory or single file of Python code. Also, I tend to avoid tools
that are "click to work" as much as possible, so for me, this is not
good.

Using "find/c" or "grep|wc" might look like simple and quick and good
solution, but it may soon turn out to be too little, which is why I do
not consider sloccount to be an overkill (which you seem to
suggest). Especially that OP mentioned something about code
complexity, if memory serves. On my system, all it takes is:

(as root) apt-get install sloccount

and: sloccount alioth_nbody.lsp (or .py or what you like)

I guess it is similarly easy to install under other OSes, even under
Windows - I would try cygwin installer for this.

On the other hand, using Office (or equivalent) only to count lines
seems like royal excess. And if I want to calculate, I use lisp
interpreter interactively (believe it or not). Spritesheep, like
Excel, has some merits but I consider them poor choice for computing
anything important (I see no formula, I see no errors).
0 new messages