[Numpy-discussion] Improving Docs on Wiki

Joris De Ridder Joris.DeRidder@ster.kuleuven...
Fri Mar 21 22:17:37 CDT 2008

On 21 Mar 2008, at 18:22, Joe Harrington wrote:

> What you have brought up is really a documentation problem: how do I
> find the name of the routine I want?

One way of dealing with this, could be the implementation of a "doc()"  
function in numpy that helps you to find what you want. A (still  
fairly basic) version of such a doc() function is given below. It  
understands the unix-like wildcards * and ?, and it will also find  
numpy classes/functions in the subpackages linalg, fft, and random. If  
it finds several possibilities it lists them, if only 1 match is  
found, the docstring is immediately given. As an example:

 >>> doc("*inv")

It's should not be difficult to improve doc() by letting it also  
search in the docstrings, or by letting it respond intelligently to  
some "magic" search terms like e.g. category names.



import numpy
from inspect import getdoc
import re

def doc(searchstr):

     searchstr = searchstr.strip().replace('*','\w*').replace('?','\w')
     pattern = re.compile('^'+searchstr+'$')

     results = []
     for package in [numpy, numpy.linalg, numpy.fft, numpy.random]:
         searchlist = [a for a in dir(package) if a[0] != '_']
         results += [package.__name__ + "." + s for s in searchlist if  
pattern.search(s) != None]

     if len(results) == 0:
         print "Sorry, no matches"
     elif len(results) == 1:
         print results[0]
         mod = numpy
         components = results[0].split('.')
         for comp in components[1:]:
             mod = getattr(mod, comp)
         docstring = getdoc(mod)
         if docstring is not None:
             print docstring
             print results[0] + " exists, but no docstring was found"
         for s in results:
             print s

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

More information about the Numpy-discussion mailing list