Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python docstring search - similar to MATLAB `lookup` or Linux `apropos`

Tags:

python

Is there a way to perform keyword searching of module and function docstrings from the interpreter?

Often, when I want to do something in Python, I know there's a module that does what I want, but I don't know what it's called. I would like a way of searching for "the name of the function or module that does X" without having to Google "python do X".

Take the example of "how can I open an URL"? At a Linux shell, I might try >> apropos open url. Under MATLAB, I might try >> lookup open url. Both of these would give me listings of functions or modules that include the words 'open' and 'URL' somewhere in their man page or doc string. For example:

urllib.urlopen   : Create a file-like object for the specified URL to read from.
urllib2.urlopen  : ...
...

I'd like something that searches through all installed modules, not just the modules that have been imported into my current session.

Yes, Google is a great way to search Python doc strings, but the latency is a bit high. ;)

like image 657
Li-aung Yip Avatar asked May 18 '12 06:05

Li-aung Yip


1 Answers

The built-in support for that comes from pydoc.apropos:

import pydoc
pydoc.apropos('Zip')
# output: zipimport - zipimport provides support for importing Python modules from Zip archives.

Which, as you can see, is nearly useless. It also stops working whenever a module cannot be imported, which might mean 'always' depending on your package management style.

An alternative that I haven't used but looks promising is apropos.py:

Deep, dirty, and exhaustive 'apropos' for Python. Crawls all libraries in the system.path, and matches a querystring against all module names, as well as class, function, and method names and docstrings of the top-level modules.

Usage: ./apropos.py

This module was created due to the limits of PyDoc's apropos method, which fails hard when one of the top level modules cannot be imported. The native apropos method also does not crawl docstrings, or deep module names, which this module does.

like image 63
TryPyPy Avatar answered Nov 14 '22 20:11

TryPyPy