NEWBIE TIP: Displaying the contents of an imported module...from ENGSOL
Andy Todd
andy_todd at spam.free.yahoo.com
Mon Aug 6 02:25:33 EDT 2001
<fleet at teachout.org> wrote in
<<mailman.997066209.23085.python-list at python.org>:
>
>I think I'm hung up on the substitution thing again. If I wanted to
>name this 'show.py' and be able to use 'python show.py math' on the
>command line - how do I get "import sys.argv[1]" to work. (and I would
>already have imported 'sys.') I tried 'eval()' without success - kept
>getting "invalid syntax" although the proper string was shown.
>
> - fleet -
OK, using the code below and encapsulating it in a function, we get
something like;
"""
def getDoc(moduleName):
"Print all of the doc strings for a module"
module=__import__(moduleName)
for x in dir(module):
try:
print "%s\n%s\n%s\n" % (x, '-'*35, module.__dict__[x].__doc__)
except AttributeError:
print "*** no __doc__ for %s.%s\n" % (moduleName, x)
"""
If you want to put this into an executable (getdoc.py for instance), add
this at the bottom of the file;
"""
if __name__=="__main__":
if sys.argv[1]:
getDoc(sys.argv[1])
"""
And Bob's your Auntie's live in lover. Of course, you need to import sys
and os at the top of the file for it to actually work.
Oh, and there are a couple of smalls problem with the function 'getDoc' as
it stands;
- It prints the __doc__ strings of all of the modules that are imported by
module. Thats potentially an awful lot of output. Do you really want to see
all of it?
- It doesn't print the __doc__ string of module. It will try to print
module.__doc__.__doc__ which will always (probably) raise an
AttributeError.
I leave the resolution of those two 'issues' to the reader ;-)
>
>> From: Tom Robinson <tom at alkali.spamfree.org>
>>Subject: Re: NEWBIE TIP: Displaying the contents of an imported
>>module...from ENGSOL
>>
>>Here's a hack to show the __doc__ for everything in a module:
>>
>>import os
>>
>>for x in dir(os):
>> try:
>> print "%s\n%s\n%s\n" % (x, '-'*35, os.__dict__[x].__doc__)
>> except AttributeError:
>> print "*** no __doc__ for os.%s\n" % (x)
>>
>>--
>>tom at alkali.spamfree.org
>>remove 'spamfree.' to respond
>>--
>
>
>
Regards,
Andy
--
Content free posts a speciality
More information about the Python-list
mailing list