PYTHON IS CRAP

Alex Martelli alex at magenta.com
Thu Aug 17 03:53:31 EDT 2000


"Vespe Savikko" <vespe at cs.tut.fi> wrote in message
news:vphaeeca1i2.fsf at hiirihaukka.cs.tut.fi...
> Also sprach David Bolen <db3l at fitlinxx.com>:
>
>   Yeah, but that's still less convenient than just do something like:
>
>   > pyman string.split
>
>   (e.g., a direct lookup on precisely the API entry you are interested
>   in, in just the way it gets used by the application)
>
> In most (?) cases doc strings do just that:

They surely should, so here's a first-cut attempt at pyman.py:


"""pyman.py: give docstrings for one or more Python items

Use: python pyman.py [list of fully qualified modules, functions, ...]

Example: python pyman.py string.join
"""

import sys
import string

def do_import(name):
    components = string.split(name, '.')
    try:
        mod = __import__(name)
    except ImportError:
        if len(components)==1:
            try:
                mod = __builtins__[name]
            except KeyError:
                raise ValueError("not builtin nor module")
        else:
            mod = __import__(string.join(components[:-1]))
    for comp in components[1:]:
        mod = getattr(mod, comp)
    return mod

def explain(use):
    if not use: return
    try: themod=do_import(use)
    except (ValueError,ImportError), err:
        print "Sorry, can't import '"+use+"':",err
        return
    try: print themod.__doc__
    except:
        print "Sorry, no doc for",use

if __name__=='__main__':
    if len(sys.argv)>1:
        for arg in sys.argv[1:]:
            explain(arg)
    else:
        explain('pyman')


Running the given example produces:

D:\Python\Py152>python pyman.py string.join
join(list [,sep]) -> string
joinfields(list [,sep]) -> string

Return a string composed of the words in list, with
intervening occurences of sep.  Sep defaults to a single
space.

(join and joinfields are synonymous)

D:\Python\Py152>


Of course, besides the explicit "python pyman.py whatever",
all the usual tricks to run a Python script, depending on
platform, are also OK.

This little script no doubt could and should be enhanced,
made more robust, etc, etc, for example with switches to
get at richer info than just docstrings, and so on.  But
I hope it can provide a basis for you to apply whatever
such enhancements you desire.


Alex







More information about the Python-list mailing list