Logical lines of code counter

L

Leif K-Brooks

I got bored with working on larger projects and wrote a little
script/module for counting logical (not physical) lines of Python code
in a file or directory. It uses ASTs generated by compiler.parse instead
of text manipulation functions, so it isn't incredibly fast, but the
speed is good enough to be usable for me.

If you want to take a look, the file is at
<http://ecritters.biz/linecount.py>. I don't really care about how
people license their modifications (and the GPL is longer than the
script itself), so it's licensed under the MIT License.

I'm sure there are a few bugs (AST nodes I forgot to count?), and I'd
appreciate being emailed if anyone finds one.
 
L

Leif K-Brooks

Tim said:
If you want something which can do this and so much more, have a look at pylint.

http://www.logilab.org/projects/pylint

Neat-looking tool, but not really usable for me. It counts physical
lines, not logical lines; it doesn't seem to have an option to
recursively scan all sub-directories and files in a directory; and it
refuses to process a file if some of the modules code wants to import
aren't importable, which is the case with mod_python code.
 
T

Tim Leslie

Neat-looking tool, but not really usable for me. It counts physical
lines, not logical lines; it doesn't seem to have an option to
recursively scan all sub-directories and files in a directory; and it
refuses to process a file if some of the modules code wants to import
aren't importable, which is the case with mod_python code.

Ah ok, I guess that's quite a drawback. I've been keeping my eye out
recently for software development tools like pylint/pychecker, so if
you can find a tool which can provide better line count type
statistics I'd be interested to hear about it.

Cheers,

Tim
 
S

Sylvain Thenault

Neat-looking tool, but not really usable for me. It counts physical lines,
not logical lines; it doesn't seem to have an option to recursively scan
all sub-directories and files in a directory; and it refuses to process a
file if some of the modules code wants to import aren't importable, which
is the case with mod_python code.

pylint counts both physical lines and logical lines, but all collected
values are not displayed in reports. You should easily add a new report to
display some additional information if you need it. Well, it lacks some
documentation but the code is quite clean and I would be happy to help.

It's also possible to recursivly scan for all python modules in a
given package ("pylint mypackage"). The problem with failing import is the
most annoying one. I plan to fix it, one day ;)
 
T

Tim Leslie

Hi Sylvain,

Since you're alive on this list, I thought I'd throw you a couple of questions:

1) Where should I best be asking pylint questions? The mailing list
looks a bit quite so I wasn't sure if I would get a reply.

2) Is there someway to specify a set of variables which won't throw
errors for being unused, eg _ when used as a dummy in a for/list comp.

3) Why does this trigger?

W0622 datacore.ns5 Ns5._parseHeaders 156 Redefining built-in '_'

The offending line is:

electrodes = [{} for _ in range(self.num_channels)]

Cheers

Tim
 
S

Sylvain Thenault

Hi Sylvain,

Hi !
Since you're alive on this list, I thought I'd throw you a couple of
questions:

1) Where should I best be asking pylint questions? The mailing list looks
a bit quite so I wasn't sure if I would get a reply.

the mailing list is the right place to ask pylint related questions. There
isn't a lot of traffic there, but you're almost sure to get an answer by
asking to this list (more than here, since I've not always the time to
read c.l.py).
2) Is there someway to specify a set of variables which won't throw
errors for being unused, eg _ when used as a dummy in a for/list comp.

yes, see "good-names" option / configuration variable. But this will allow
the name anywhere in your program.
3) Why does this trigger?

W0622 datacore.ns5 Ns5._parseHeaders 156 Redefining built-in '_'

The offending line is:

electrodes = [{} for _ in range(self.num_channels)]

because I guess that at some point "_" is added to builtins. This is
usually the case with internationalizable programs using gettext. It seems
that pylint itself is importing a module which add this name to the
builtins, it shouldn't. I'll investigate into this...

regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top