¿Cómo contar líneas de código en Python excluyendo comentarios y cadenas de documentos? [cerrado]

I want to count the lines of code in a multi-file Python project as accurately as possible, but without including comments, docstrings or blank lines in the total.

I first tried using cloc, which is available as a Debian package. But cloc treats most docstrings as code - even though they are comments. (Actualizar: no longer - recent versions of cloc now treat Python docstrings as comments.)

I notice some comments below saying that docstrings should be included in the total because they might be used by the code to influence behaviour at runtime and hence count as part of the programs code/data/config. A prominent example of this is 'ply', which asks you to write functions with docstrings which, as I recall, contain grammar and regular expressions which are central to the program's operation. However, this seems to me to be very much a rare exception. Most of the time docstrings act just like comments. Specifically, I know for a fact that is true for all the code I want to measure. So I want to exclude them as such from my line counts.

preguntado el 31 de enero de 12 a las 08:01

I'd say counting comments is the right way, because in general the comments are just as valuable as the actual code lines -

@Kimvais I must say I've had the opposite experience in 20 years of programming - comments are generally worthless because the compiler never checks them :-) -

Cadenas de documentación de Python son code - they become the __doc__ attribute of the function and can contain tests. Maybe you need to define what you mean by 'lines of code' -

@AdrianCornish: LOC count is pretty worthless, too, so that works out just fine then. -

@AdrianCornish WTF are you talking about .. python compiler? and in your 20 years of programming you learned that "comments are generally worthless"? -

4 Respuestas

It is probably correct to include Python docstrings in a "lines of code" count. Normally a comment would be discarded by the compiler, but docstrings are parsed:

Consulte nuestra página PEP 257 - Docstring Conventions:

A docstring is a string literal that occurs as the first statement in a module, function, class, or method definition. Such a docstring becomes the __doc__ atributo especial de ese objeto.


String literals occurring en otra parte in Python code may also act as documentation. They are not recognized by the Python bytecode compiler and are not accessible as runtime object attributes..

In other words, docstrings son compiled and constitute, in a very real way, the code of the program. Additionally, they're commonly used by the doctest module for unit testing, as usage strings for command line utilities, and so on.

Respondido el 31 de enero de 12 a las 13:01

Disagree. While docstrings are compiled and can be used by the code, their use and semantic is as comments. They should be excluded from any meaningful line count. - Jonathan Hartley

@JonathanHartley personally I think that "compiled and can be used by the code" is a good argument para it being counted. - Hamish

Hey. I guess I feel the opposite because even though they can be used by the code, they almost never are. By which I mean, yes they are used by 'pydoc' et al, but I think the only program I've seen that stores data in docstrings and then examines that data is David Beazley's 'Ply'. So it's very rare. If you're comparing two modules to see which contains more code, and one has docstrings but the other does not, it seems most useful to me to exclude the docstrings and get the result 'they are about the same'. - Jonathan Hartley

No part of my project relies on the docstrings in any way whatsoever. I just want to get the number of Python instructions in my program, without my huge docstrings included, so I can check my ratio of production code to test code. For my purposes it makes zero sense to include them in a "lines of code" count. - Dan Passaro

@leo-the-manic ok, well do whatever you need to do for "your purposes". Note that "number of Python instructions" is totally different to LOC. "number of instructions" might be a better metric, for some definitions of "better". - Hamish

Comment lines can be lines of code in python. See doctest por ejemplo.

Moreover, you will have trouble to find a sensible/reliable way to consider a case like this as being a comment or code:

foo = ('spam', 
       '''more spam''',

Just count the comment lines as well, I think most programmers will agree it is as good a measure for whatever you are actually trying to measure.

Respondido el 31 de enero de 12 a las 13:01

Disagree. While technically docstrings are compiled and accessible from code, the vast predominance of their usage and semantic is a comments. They should be excluded from line counts. The way to detect ambiguous looking cases like the one in this answer is to do the line count using the AST. - Jonathan Hartley

Tahar doesn't count the docstrings. Here's its count_loc function :

def count_loc(lines):
    nb_lines  = 0
    docstring = False
    for line in lines:
        line = line.strip()

        if line == "" \
           or line.startswith("#") \
           or docstring and not (line.startswith('"""') or line.startswith("'''"))\
           or (line.startswith("'''") and line.endswith("'''") and len(line) >3)  \
           or (line.startswith('"""') and line.endswith('"""') and len(line) >3) :

        # this is either a starting or ending docstring
        elif line.startswith('"""') or line.startswith("'''"):
            docstring = not docstring

            nb_lines += 1

    return nb_lines

Respondido el 05 de enero de 13 a las 14:01

Thank you for the reasonable recommendation, and for not making preposterous and pontificating claims, like your fellow responders, about docstrings being code. Lines of code is a valid (and in fact the best: herraiz.org/blog/2010/11/22/making-software-is-out) measure of code complexity and when I need that complexity to reflect the raw source code (rather than my copious amount of matemáticas notes in docstrings), I need to omit docstrings! - Ahmed Fasih

I beleive that the doc in docstrings is for documentation - chaouche

The above code would fail on docstrings which use single quotes, or on some regular strings which use triple quotes. The right way to solve this problem is to look at the AST. - Jonathan Hartley

@JonathanHartley Can you provide an example where the code would possibly fail ? - chaouche

@ychaouche The OP's code doesn't fall into any of the categories I enumerated. - Jonathan Hartley

Has mirado http://www.ohloh.net/p/ohcount - always been pretty on the money for me - although I do not use python

Respondido el 31 de enero de 12 a las 12:01

Thanks, but like cloc this tool also counts docstrings with triple apostrophes as code, so it's also not really Python-aware. - Wolkenarchitekt

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.