The pyclbr module, shown in Example 14-1, contains a basic Python class parser.
In 1.5.2, the module exports a single function, readmodule, which parses a given module, and returns a list of all classes defined at the modules top level.
File: pyclbr-example-1.py
import pyclbr
mod = pyclbr.readmodule("cgi")
for k, v in mod.items():
print k, v
MiniFieldStorage
InterpFormContentDict
FieldStorage
SvFormContentDict
StringIO
FormContent
FormContentDict
In 2.0 and later, theres also an alternative interface, readmodule_ex, which returns global functions as well. This is shown in Example 14-2.
File: pyclbr-example-3.py
import pyclbr
# 2.0 and later
mod = pyclbr.readmodule_ex("cgi")
for k, v in mod.items():
print k, v
MiniFieldStorage
parse_header
test
print_environ_usage
parse_multipart
FormContentDict
initlog
parse
StringIO
SvFormContentDict
...
To get more information about each class, use the various attributes in the Class instances, as Example 14-3 shows.
File: pyclbr-example-2.py
import pyclbr
import string
mod = pyclbr.readmodule("cgi")
def dump(c):
# print class header
s = "class " + c.name
if c.super:
s = s + "(" + string.join(map(lambda v: v.name, c.super), ", ") + ")"
print s + ":"
# print method names, sorted by line number
methods = c.methods.items()
methods.sort(lambda a, b: cmp(a[1], b[1]))
for method, lineno in methods:
print " def " + method
print
for k, v in mod.items():
dump(v)
class MiniFieldStorage:
def _ _init_ _
def _ _repr_ _
class InterpFormContentDict(SvFormContentDict):
def _ _getitem_ _
def values
def items
...
Core Modules
More Standard Modules
Threads and Processes
Data Representation
File Formats
Mail and News Message Processing
Network Protocols
Internationalization
Multimedia Modules
Data Storage
Tools and Utilities
Platform-Specific Modules
Implementation Support Modules
Other Modules