I started using Python a few weeks ago, something finally clicked and I started writing tools for my personal use. I started writing something with a GUI using GTK and the code lines started multiplying rapidly, so I thought to myself "Am I commenting my source to death? How many comments do I have?". Since I'm new to the language I wasn't really sure if I was going about everything the right way. At the time I had never heard of pycount or CLOC, I just found out about those two today after a google search. I think the authors of those tools had the same idea, before me ofcourse. My script counts code lines, blank lines, and comments, but it also counts inline-comments, imports , variables, variable assignments, classes, functions, if / try / & print statements, and reports the count or percentage of each. It also has the ability to list all imports in a script, plus the ability to list all variables in a script in the order of discovery, or alphabetical order. That can be accomplished by passing the -v or -i flag on the command-line. There are a few short comings which are explained on my site and in the comments, like Doc Strings and multiple variable assignments on the same line. But for most projects it's dead on. I don't want to say too much, you can visit my site for more info. Here is what the default basic output looks like when codecount.py is ran on itself:
Finished analyzing: codecount.py... Total Lines: 447 Code Lines: 245 (54.8098%) Blank Lines: 63 (14.0939%) Comment Lines: 139 (31.0961%) Inline Comments: 22 Total Comments: 161 Longest Code Line: 113 Shortest Code Line: 5 Longest Comment: 75 Shortest Comment: 3 Imports: 3 Functions: 5 Classes: 0 If Statements: 52 Try Statements: 1 Print Statements: 65 Assignments: 89 Variables: 38
I hope this helps some people out, or teaches someone something about Python. It's certainly done that for me. If anyone has any suggestions or ideas feel free to send me a message. Thanks for reading,
update : There is a very small bug in this script, the first line after
if __name__=="__main__" should read
if ("-h" in sys.argv): instead of "h".. sorry.
MODERATOR EDIT : Code edited and the double post removed
Edited by pyTony: Updated code from other post