How can I get a list of all public symbols in Python librararies

Steve Holden sholden at holdenweb.com
Thu Nov 2 22:45:43 CET 2000


Alain:

Sorry, didn't see your original post.

> >
> > >  | In order to build support for dictation of Python code, I need a list of
> > >  | all the symbols (functions, methods, classes, variables) defined in the
> > >  | standard Python libraries.
> > > [...]
> > >  | Any suggestions on what would be the easiest way to get such a list?
> > >
Well, I have this little program which should provide you with a sorted list
of the functions, classes and methods.  Sorry, don't do anything about the
variables.

On my laptop (Celeron 300 Thinkpad) it takes about fifteen seconds to process
the whole top level of the Python 1.5.2 library, producing just over 2800
lines of output. It should work with Python 2 as well, and it should be easy
to make it work over a whole directory tree if you want to analyse the packages
as well as the modules.

This last is left as an exercise for the reader...

regards
 Steve
------------------------------------------------------------------------------
#
# Index python module files' function and class definitions.
#

import sys, string, re, glob, os.path

p = re.compile(r"([\t ]*)(def|class)\s*(\w+)\s*(.*):$")

def slen(s, tlen=8):
    """Returns effective length of string, allowing for tabs of given
length."""
    r = 0
    for c in s:
        if c == " ": r = r+1
        elif c == "\t": r = ((r+tlen)/tlen)*tlen
    return r

def process(filename):
    """Extract class, function and method definitions from a file.

    Returns a sortable list of references, each created by setof()."""
    lengths = [0]
    names = ["__main__"]
    types = ["builtin"]
    clen = 0
    ll = open(filename).readlines()
    bname = os.path.basename(filename)
    r = []
    for i in range(len(ll)):
        m = p.match(ll[i])
        if m:
            s = slen(m.group(1))
            if s > clen:
                clen = s
                types.append(None)
                names.append(None)
            elif s < clen:
                clen = s
                del types[-1]
                del names[-1]
            try:
                types[-1] = m.group(2)
                names[-1] = m.group(3)
            except KeyError:
                print "Impossible keyword:", m.group(2)
            r.append(setof(types, names, bname, i+1))
    return r

def setof(types, names, fn, i):
    """Produce a quintuple for a class/method/function reference.

    Value returned is (sort_name, name, type, line_number, filename)."""
    # print "###", types, types[-2:]
    if types[-2:] == ['class','def']:
        type = 'Method'
        name = '%s [%s]' % (names[-1], names[-2])
    elif types[-1] == 'class':
        type = 'Class'
        name = names[-1]
    elif types[-1] == 'def':
        type = 'Function'
        name = names[-1]
    else:
        type = '   ???'
        name = names[-1]
    return (string.lower(name), name, type, i, fn)

r = []
for pat in sys.argv[1:]:
    fn = glob.glob(pat)
    for f in fn:
        r = r + process(f)

r.sort()

for rr in r:
    print "%-8s %-40s %5d: %s" % (rr[2], rr[1], rr[3], rr[4])


-- 
Helping people meet their information needs with training and technology.
703 967 0887      sholden at bellatlantic.net      http://www.holdenweb.com/





More information about the Python-list mailing list