NEWBIE TIP: Displaying the contents of an imported module...from ENGSOL
fleet at teachout.org
fleet at teachout.org
Mon Aug 6 17:17:26 EDT 2001
> From: andy_todd at spam.free.yahoo.com (Andy Todd)
>Subject: Re: NEWBIE TIP: Displaying the contents of an imported module...from ENGSOL
Thanks! (more below)
>
><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.
>>
>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 I *do* want it as an executable!
>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.
I didn't realize that situation was such common knowledge! (And, of course!)
>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?
Yup! (At least once!)
>- 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 ;-)
>
I hate it when people say that! :) Thanks for the pointers.
- fleet -
More information about the Python-list
mailing list