Created
September 20, 2012 03:15
-
-
Save clvrobj/3753764 to your computer and use it in GitHub Desktop.
python file code line counter (http://code.activestate.com/recipes/527746-line-of-code-counter/)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# -*- coding: utf-8 -*- | |
import sys | |
import os | |
import fnmatch | |
def Walk(root='.', recurse=True, pattern='*'): | |
""" | |
Generator for walking a directory tree. | |
Starts at specified root folder, returning files | |
that match our pattern. Optionally will also | |
recurse through sub-folders. | |
""" | |
for path, subdirs, files in os.walk(root): | |
for name in files: | |
if fnmatch.fnmatch(name, pattern): | |
yield os.path.join(path, name) | |
if not recurse: | |
break | |
def LOC(root='', recurse=True): | |
""" | |
Counts lines of code in two ways: | |
maximal size (source LOC) with blank lines and comments | |
minimal size (logical LOC) stripping same | |
Sums all Python files in the specified folder. | |
By default recurses through subfolders. | |
""" | |
count_mini, count_maxi = 0, 0 | |
for fspec in Walk(root, recurse, '*.py'): | |
skip = False | |
for line in open(fspec).readlines(): | |
count_maxi += 1 | |
line = line.strip() | |
if line: | |
if line.startswith('#'): | |
continue | |
if line.startswith('"""'): | |
skip = not skip | |
continue | |
if not skip: | |
count_mini += 1 | |
return count_mini, count_maxi | |
if __name__ == '__main__': | |
path = sys.argv[1] | |
if path: | |
print 'Counting lines in %s' % path | |
print 'Minimal size: %s, maxium size: %s' % LOC(path) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment