[Tutor] Recreating the help module

Peter Otten __peter__ at web.de
Wed Jul 25 08:47:28 CEST 2012


M Nickey wrote:

> Hey all,
> 
> I'm trying to recreate the 'help' on various modules that are available.
> So far, I have a bit of code and it seems to be working for the most part.
> I can get the modules available but I also want to be able to print the
> information that is available for each module.
> 
> Current output:
> ['__add__', '__class__', '__contains__', ... 'partition', 'replace',
> ['rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
> ['splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate',
> ['upper', 'zfill']
> str(object) -> string
> 
> Return a nice string representation of the object.
> If the argument is a string, the return value is the same object.
> 
> Desired result:
> ['__add__', '__class__', '__contains__', ... 'partition', 'replace',
> ['rfind', 'rindex', 'rjust', 'rpartition', 'rsplit', 'rstrip', 'split',
> ['splitlines', 'startswith', 'strip', 'swapcase', 'title', 'translate',
> ['upper', 'zfill']
> 
> atof(s):
> Return the floating point number represented by the st
> atof(s) -> float
> 
> I'm close (I think) but I just can figure out how to grab that part of the
> documentation. Any advice?

I'm guessing you want the functions in the string module rather than the 
methods of the str type? 

If so: you can get the module object from the name with the __import__() 
builtin function:

> Code:
> import os
> import string
> import inspect
> 
> def getDirName():
>     modList = []

      module_name = "string"
      module = __import__(module_name) 
      cwd = dir(module)

>     print cwd
>     info = inspect.getdoc(cwd[35])
>     print info
>     return
> 
> if __name__ == '__main__':
>     getDirName()

There's one complication with __import__(), it always gives you the toplevel 
package, e. g. to get the os.path module you have to do

package = __import__("os.path")
module = getattr(package, "path")

You might also take a look into the source of the pydoc module.



More information about the Tutor mailing list