package introspection...

Skip Montanaro skip at pobox.com
Mon Feb 24 15:38:23 EST 2003


I frequently poke around in modules using dir() when I can't quite remember
what I'm looking for.  This technique generally fails for packages because
they generally don't import their subsidiary modules unless asked for:

>>> import spambayes
>>> dir(spambayes)
['__builtins__', '__doc__', '__file__', '__name__', '__path__', '__version__']

The best I've come up with so far is

>>> [os.path.splitext(p)[0] for p in os.listdir(os.path.dirname(spambayes.__file__)) if p.endswith(".py")]
['__init__', 'cdb', 'cdb_classifier', 'chi2', 'classifier', 'compatheapq',
'compatsets', 'Corpus', 'CostCounter', 'dbmstorage', 'Dibbler',
'FileCorpus', 'hammie', 'hammiebulk', 'Histogram', 'mboxutils', 'msgs',
'optimize', 'OptionConfig', 'Options', 'PyMeldLite', 'storage',
'TestDriver', 'Tester', 'tokenizer'] 

which I will add to my personal toolkit as something like

    def pkgdir(mod):
        contents = dir(mod)
        if hasattr(mod, "__file__"):
            contents += ["[%s]"%os.path.splitext(p)[0]
                          for p in os.listdir(os.path.dirname(mod.__file__))
                            if (p.endswith(".py") or p.endswith(".so")) and
                                os.path.splitext(p)[0] not in contents and
                                p != "__init__.py"]
        contents.sort()
        return contents

if someone doesn't have a better suggestion.  It generates what to me seem
like reasonable results:

>>> pkgdir(email)
['Charset', 'Encoders', 'Errors', 'Generator', 'Header', 'Iterators',
'MIMEAudio', 'MIMEBase', 'MIMEImage', 'MIMEMessage', 'MIMEMultipart',
'MIMENonMultipart', 'MIMEText', 'Message', 'Parser', 'Utils', '[_compat21]',
'[_compat22]', '[_parseaddr]', 'base64MIME', 'message_from_file',
'message_from_string', 'quopriMIME'] 
>>> pkgdir(spambayes)
['[Corpus]', '[CostCounter]', '[Dibbler]', '[FileCorpus]', '[Histogram]',
'[OptionConfig]', '[Options]', '[PyMeldLite]', '[TestDriver]', '[Tester]',
'[cdb]', '[cdb_classifier]', '[chi2]', '[classifier]', '[compatheapq]',
'[compatsets]', '[dbmstorage]', '[hammie]', '[hammiebulk]', '[mboxutils]',
'[msgs]', '[optimize]', '[storage]', '[tokenizer]', '__builtins__',
'__doc__', '__file__', '__name__', '__path__', '__version__'] 

Thx,

Skip





More information about the Python-list mailing list