faqts : Computers : Programming : Languages : Python : Snippets

+ Search
Add Entry AlertManage Folder Edit Entry Add page to http://del.icio.us/
Did You Find This Entry Useful?

2 of 3 people (67%) answered Yes

Entry

pydoc (easy doc generation)

Jul 5th, 2000 09:59
Nathan Wallace, Hans Nowak, Snippet 42, Scott Cotton


"""
Packages: text.docstrings
"""

"""
I know that there are much more extensive documentation
systems out there for python with doc strings, but
sometimes quick and dirty is nice.

The script is far from fully complete, but in combination
with the latest beta's loads of new doc strings, it's the
fastest way I've found to get python documentation...

enjoy.
"""

import sys
import string
import getopt

try:
   optlist, args = getopt.getopt(sys.argv[1:], "d")
except getopt.error:
   print "invalid option.  available options are: -d"
   sys.exit(0)

dodir = 0
if optlist:
   dodir = 1

if not args:
   if not dodir:
      print "what do you want documentation for?"
      sys.exit(0)
   else:
      print "options for __builtins__"
      print "\n\t* " + string.join(dir(__builtins__), "\n\t* ")
      
      

things = args

for thing in things:
   parts = string.split(thing, ".")
   try:
      mod = __import__(parts[0])
   except ImportError:
      try:
         mod = eval(parts[0])
      except:
         print "no such thing:", parts[0]
         sys.exit(0)
   current = mod
   for part in parts[1:]:
      try:
         current = getattr(current, part)
      except AttributeError:
         try:
            current = __import__(current.__name__,
                                 globals(),
                                 locals(),
                                 [part])
            current = getattr(current, part)
         except ImportError:
            print "No docs for", part
            sys.exit(0)

   if dodir:
      print "attributes of ", current
      print "\n\t* " + string.join(dir(current), "\n\t* ")
   else:
      if hasattr(current, "__doc__"):
         print current.__doc__
      else:
         print "no, docs, sorry"