« URL's are files, again, apparentlyWeb Site Process Bus »

Lines of code


Permalink 12:27:50 pm, by fumanchu Email , 241 words   English (US)
Categories: Python, Dejavu, CherryPy

Lines of code

I was asked last week how many lines of code some of my projects are, and didn't have an answer handy. Fortunately, it's easy to write a LOC counter in Python:

"""Calculate LOC (lines of code) for a given package directory."""

import os
import re

def loc(path, pattern="^.*\.py$"):
    """Return the number of lines of code for all files in the given path.

    If the 'pattern' argument is provided, it must be a regular expression
    against which each filename will be matched. By default, all filenames
    ending in ".py" are analyzed.
    lines = 0
    for root, dirs, files in os.walk(path):
        for name in files:
            if re.match(pattern, name):
                f = open(os.path.join(root, name), 'rb')
                for line in f:
                    line = line.strip()
                    if line and not line.startswith("#"):
                        lines += 1
    return lines

I've added the above to my company's public-domain misc package at http://projects.amor.org/misc/. Here are the results for my high-priority projects (some are proprietary):

>>> from misc import loc
>>> loc.loc(r"C:\Python24\Lib\site-packages\raisersedge")
>>> loc.loc(r"C:\Python24\Lib\site-packages\dejavu")
>>> loc.loc(r"C:\Python24\Lib\site-packages\geniusql")
>>> loc.loc(r"C:\Python24\Lib\site-packages\cherrypy")
>>> loc.loc(r"C:\Python24\Lib\site-packages\endue")
>>> loc.loc(r"C:\Python24\Lib\site-packages\mcontrol")
>>> loc.loc(r"C:\Python24\Lib\site-packages\misc")

~= 61 kloc. Pretty hefty for a single in-house web app stack. :/ But, hey, nobody said integration projects were easy.


Comment from: all_in_sane [Visitor]

nice script, i can really use that :-)

I just needed to be able to run on mutliple directories so this works:

if name=="main":
import sys
if len(sys.argv) > 1:
for pth in sys.argv[1:]:
if os.path.exists(pth):
print "%10s lines found in: %s" % (loc(pth), pth)
print "Error %s Not Found" % pth


07/09/07 @ 13:31
Comment from: Tennessee Leeuwenburg [Visitor] · http://pythonpapers.org

While I'm a python fanboy, it's hard to beat
wc -l *.py


07/09/07 @ 18:40
Comment from: fumanchu [Member] Email

Tennessee, now try excluding blank, pure-whitespace, and commented lines (as the Python code does). ;)

07/09/07 @ 23:08
Comment from: ants [Visitor]

Or to replicate the exact behaviour in shellscript:
find $dir -name '.py' | xargs grep -v '^[[:space:]](#|$)' | wc -l

07/10/07 @ 07:57
Comment from: Benji York [Visitor] · http://benjiyork.com

I suggest SLOCCount (http://www.dwheeler.com/sloccount/).

07/10/07 @ 08:50
Comment from: Christopher Lenz [Visitor] · http://www.cmlenz.net/

I wrote a more extensive LOC counter for my Bitten project some time ago, which differentiates between blank, code, comment, and docstring lines, if you're interested in that kind of precision:


07/13/07 @ 03:56

This article is very informative when it comes to spiritual aspects.

02/20/16 @ 04:51

Leave a comment

Your email address will not be revealed on this site.

Your URL will be displayed.

Please enter the phrase "I am a real human." in the textbox above.
(Line breaks become <br />)
(Name, email & website)
(Allow users to contact you through a message form (your email will not be revealed.)
September 2017
Sun Mon Tue Wed Thu Fri Sat
 << <   > >>
          1 2
3 4 5 6 7 8 9
10 11 12 13 14 15 16
17 18 19 20 21 22 23
24 25 26 27 28 29 30


The requested Blog doesn't exist any more!

XML Feeds

powered by b2evolution