pycount
pycount.py -- A very initial effort to Python code metrics.
The pycount module is accessible via the buildutils module.
This program started as a hack, bending and twisting pylint.py by Tim Peters. At that time I was interested on code metrics and thought let's do something quick and dirty. pycount then and today scans a Python file and reports back various cate- gories of lines it thinks it has found, like comments, doc strings, blank lines and, sometimes, real code as well.
The output can be either as a table with only the number of lines found for each file or as a listing of the file(s) prefixed with some running numbers and classification for each line.
The former is useful to scan a whole project e.g. when you need to know if the project is documented well or at all and where this info can be found. The latter is at least a nice new view to your own sources or that of others if nothing else!
There are a couple of minor known bugs with pycount like: Doc strings must be tripple-quoted ones otherwise they are classified as normal source code. Continuation lines ending with a backslash are not treated at all. Complex regular ex- pressions (as in pycount itself) can knock the parser down, quickly. There is a built-in quick-and-dirty solution to this which might work whenever the problem is on one line only. But in "most cases" it works...
Usage:
pycount.py [-v] <file1> [<file2> ...]
pycount.py [-v] <expr>
pycount.py [-F] <linetypes> <file>
pycount.py [-R] <expr>
where <fileN> is a Python file (usually ending in .py),
<linetypes> is a comma-seperated list of python line
type codes (code, comment, doc string, blank)
e.g. '###' or 'DOC,###,---'
<expr> is a shell expression with meta-characters
(note that for -R you must quote it)
-v verbose flag, listing the source
-F filter flag, listing the filtered source
-R apply recursively on subdirectories
TODO
- Don't filter first line if '#!<path> python'(?).
- De-obfuscate top-level if-stmt in main().
- Improve usage as as a module.
- Print statistics as percentage figures, maybe.
- Write some test cases using pyunit.
DONE
- Replace Unix 'find' with Python os.path.walk / fnmatch.
- Scanning should also work recursively (-R option).
- Test stdin case for single files.
- Return total count per category when run on multiple files.
- Add a feature to uncomment files.
HISTORY
-
0.0.1 : 1997-??-?? : copy/past from Tim Peter's pylint
-
0.0.2 : 1997-??-?? : included some refinements by Tim
-
0.0.3 : 1997-07-22 : doc & (C) (borrowed from M.-A. Lemburg)
-
- 0.0.4 : 1998-08-25 : replaced regex/regsub with re module,
-
added a global line counter in -v mode
-
0.0.5 : 1998-11-25 : code embellishments, recursive on files, ...
-
0.0.6 : 2000-07-04 : fixed typos, improved doc
FUTURE
- The future is always uncertain...
Copyright by Dinu C. Gherman, 1998, gherman@europemail.com
Permission to use, copy, modify, and distribute this software and its documentation without fee and for any purpose, except direct commerial advantage, is hereby granted, provided that the above copyright notice appear in all copies and that both that copyright notice and this permission notice appear in supporting documentation.
THE AUTHOR DINU C. GHERMAN DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE!
Dinu C. Gherman,
2000-07-04
Attributes
Functions
f collectFiles(listPatCwd, dirname, names) ...
Recursively add filenames matching a certain pattern to a list.
Classes
C Formatter(...) ...
Sort of a formatter class for filtering Python source code.
This class contains 2 members.
See the source for more information.