home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / share / doc / diveintopython / examples / apihelper.py next >
Encoding:
Python Source  |  2004-05-05  |  1.7 KB  |  48 lines

  1. """Cheap and simple API helper
  2.  
  3. This program is part of "Dive Into Python", a free Python book for
  4. experienced programmers.  Visit http://diveintopython.org/ for the
  5. latest version.
  6. """
  7.  
  8. __author__ = "Mark Pilgrim (mark@diveintopython.org)"
  9. __version__ = "$Revision: 1.3 $"
  10. __date__ = "$Date: 2004/05/05 21:57:19 $"
  11. __copyright__ = "Copyright (c) 2001 Mark Pilgrim"
  12. __license__ = "Python"
  13.  
  14. # While this is a good example script to teach about introspection,
  15. # in real life it has been superceded by PyDoc, which is part of the
  16. # standard library in Python 2.1 and later.
  17. # Your IDE may already import the "help" function from pydoc
  18. # automatically on startup; if not, do this:
  19. # >>> from pydoc import help
  20. # The help function in this module takes the object itself to get
  21. # help on, but PyDoc can also take a string, like this:
  22. # >>> help("string") # gets help on the string module
  23. # >>> help("apihelper.help") # gets help on the function below
  24. # >>> help() # enters an interactive help mode
  25. # PyDoc can also act as an HTTP server to dynamically produce
  26. # HTML-formatted documentation of any module in your path.
  27. # That's wicked cool.  Read more about PyDoc here:
  28. #   http://www.onlamp.com/pub/a/python/2001/04/18/pydoc.html
  29.  
  30. def info(object, spacing=10, collapse=1):
  31.     """Print methods and doc strings.
  32.  
  33.     Takes module, class, list, dictionary, or string."""
  34.     methodList = [e for e in dir(object) if callable(getattr(object, e))]
  35.     processFunc = collapse and (lambda s: " ".join(s.split())) or (lambda s: s)
  36.     print "\n".join(["%s %s" %
  37.                      (method.ljust(spacing),
  38.                       processFunc(str(getattr(object, method).__doc__)))
  39.                      for method in methodList])
  40.  
  41. if __name__ == "__main__":
  42.     print help.__doc__
  43.