Search code examples
pythonhelperpydoc

Displaying python documentation of all methods and attributes with a certain word in a module


I am attempting to build a help system, and I would like to print all documentation of methods, from a Python module that contain a certain word, one after the other. This is for use in modules that have a very large number of methods or attributes. Right now this is what I am doing, for example, for a module such as Tkinter.Entry, looking for methods that have for example, 'set' in them:

q = []
for i in dir(Tkinter.Entry):
    if 'set' in i:
       q.append(i)
for item in q:
    help(item)

This shows seven items of pydoc for each method with 'set' in the name one after the other, and exits cleanly on the interpreter. However, I can't seem to be able to get the pydoc in an assignment, for example:

x = help(item)   # doesn't work.
# handle display code here.

What am I missing here?

The following is for context, as in not necessarily part of the question(for example):

Is there a workaround to get the same effect as this on, for example, a simple html that will show the next help item every time the user clicks, 'close' on the current element?

<html>
  <head> 
    Help page 
  </head>
  <script type="text/javascript">
    $ function closediv(){
      <!-- code to close the div goes here-->
      }
  </script>
  <body>
    <!--maybe a list of divs here-->
    <div id="help text">{% block content %} {{x}} {% endblock %}</div>
  </body>
 </html>

As long as I can get a way to find out a way to get the help text in a way that can be passed to the block content {{X}}, I will try to figure the rest out. Thanks.


Solution

  • You can try .__doc__ to get a pretty good understanding of some key terms:

    >>> print pow.__doc__
    pow(x, y[, z]) -> number
    
    With two arguments, equivalent to x**y.  With three arguments,
    equivalent to (x**y) % z, but may be more efficient (e.g. for longs).
    >>> print list.__doc__
    list() -> new empty list
    list(iterable) -> new list initialized from iterable's items
    >>> 
    

    Just help(item) will not work with simple statements. This is the same with .__doc__:

    >>> help(if)
      File "<stdin>", line 1
        help(if)
              ^
    SyntaxError: invalid syntax
    >>> if.__doc__
      File "<stdin>", line 1
        if.__doc__
          ^
    SyntaxError: invalid syntax
    >>> 
    

    I would suggest looking at pydoc.py:

    >>> import pydoc
    >>> pydoc
    <module 'pydoc' from '/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/pydoc.pyc'>
    >>> 
    

    Which has all the documentation for everything, including simple statements.