PyMOTW: pyclbr

By Doug Hellmann
June 28, 2009

pyclbr – Python class browser support

Purpose:Implements an API suitable for use in a source code editor for making a class browser.
Python Version:1.4 and later

pyclbr can scan Python source to find classes and stand-alone functions. The information about class, method, and function names and line numbers is gathered using tokenize without importing the code.

The examples below use this source file as input:

"""Example source for pyclbr.
"""

class Base(object):
"""This is the base class.
"""

def method1(self):
return

class Sub1(Base):
"""This is the first subclass.
"""

class Sub2(Base):
"""This is the second subclass.
"""

class Mixin:
"""A mixin class.
"""

def method2(self):
return

class MixinUser(Sub2, Mixin):
"""Overrides method1 and method2
"""

def method1(self):
return

def method2(self):
return

def method3(self):
return

def my_function():
"""Stand-alone function.
"""
return


Scanning for Classes

There are two public functions exposed by pyclbr. readmodule() takes the name of the module as argument returns a mapping of class names to Class objects containing the meta-data about the class source.

import pyclbr
import os
from operator import itemgetter

def show_class(name, class_data):
print 'Class:', name
print '\tFile: {0} [{1}]'.format(os.path.basename(class_data.file), class_data.lineno)
show_super_classes(name, class_data)
show_methods(name, class_data)
print
return

def show_methods(class_name, class_data):
for name, lineno in sorted(class_data.methods.items(), key=itemgetter(1)):
print '\tMethod: {0} [{1}]'.format(name, lineno)
return

def show_super_classes(name, class_data):
super_class_names = []
for super_class in class_data.super:
if super_class == 'object':
continue
if isinstance(super_class, basestring):
super_class_names.append(super_class)
else:
super_class_names.append(super_class.name)
if super_class_names:
print '\tSuper classes:', super_class_names
return

example_data = pyclbr.readmodule('pyclbr_example')

for name, class_data in sorted(example_data.items(), key=lambda x:x[1].lineno):
show_class(name, class_data)

The meta-data for the class includes the file and line number where it is defined, as well as the names of super classes. The methods of the class are saved as a mapping between method name and line number. The output below shows the classes and methods listed in order based on their line number in the source file.

$ python pyclbr_readmodule.py
Class: Base
File: pyclbr_example.py [10]
Method: method1 [14]

Class: Sub1
File: pyclbr_example.py [17]
Super classes: ['Base']

Class: Sub2
File: pyclbr_example.py [21]
Super classes: ['Base']

Class: Mixin
File: pyclbr_example.py [25]
Method: method2 [29]

Class: MixinUser
File: pyclbr_example.py [32]
Super classes: ['Sub2', 'Mixin']
Method: method1 [36]
Method: method2 [39]
Method: method3 [42]


Scanning for Functions

The other public function in pyclbr is readmodule_ex(). It does everything that readmodule() does, and adds functions to the result set.

import pyclbr
import os
from operator import itemgetter

example_data = pyclbr.readmodule_ex('pyclbr_example')

for name, data in sorted(example_data.items(), key=lambda x:x[1].lineno):
if isinstance(data, pyclbr.Function):
print 'Function: {0} [{1}]'.format(name, data.lineno)

Each Function object has properties much like the Class object.

$ python pyclbr_readmodule_ex.py
Function: my_function [45]

See also

pyclbr
The standard library documentation for this module.
inspect
The inspect module can discover more meta-data about classes and functions, but requires importing the code.
tokenize
The tokenize module parses Python source code into tokens.

PyMOTW Home

The canonical version of this article


You might also be interested in:

News Topics

Recommended for You

Got a Question?