Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I produce a numpy-like documentation?

I'm working a lot with spyder and the object inspector, which I find pretty convenient as an instant help function. Some modules seem to profit very nicely from this function. For example a pretty basic numpy function (numpy.absolute) produces the following view in the object inspector:

View of numpy.absolute function in object inspector

I want to know, how I can write my own modules in such a way, that such a nice view is produced when I call my functions in spyder.

like image 605
Dschoni Avatar asked Jul 03 '14 13:07

Dschoni


People also ask

How is the NumPy documentation generated?

The NumPy Documentation has the details covered. API reference documentation is generated directly from docstrings in the code when the documentation is built. Although we have mostly complete reference documentation for each function and class exposed to users, there is a lack of usage examples for some of them.

What is NumPy style Docstring?

numpydoc style docstrings are written in restructured text. They are composed of a short description of the object followed by a few required and optional sections. For functions and methods, these sections are: Required. Parameters.

What is Docstring in Python?

A Python docstring is a string used to document a Python module, class, function or method, so programmers can understand what it does without having to read the details of the implementation. Also, it is a common practice to generate online (html) documentation automatically from docstrings.

What is Pyment Python?

Pyment is a tool that can convert Python docstrings and create missing ones skeletons. It can manage Google, Epydoc (javadoc style), Numpydoc, reStructuredText (reST, Sphinx default) docstring formats. It accepts a single file or a folder (exploring also sub-folders).


2 Answers

For your documentation to render as nicely as the numpy one, you need to follow the NumpyDoc standard. Suppose you have a function called func with two arguments like this:

def func(arg1, arg2):
    return True

To add documentation to it you need to write a multiline string below its definition (called in the Python world docstring), like this

def func(arg1, arg2):
    """Summary line.

    Extended description of function.

    Parameters
    ----------
    arg1 : int
        Description of arg1
    arg2 : str
        Description of arg2

    Returns
    -------
    bool
        Description of return value

    Examples
    --------
    >>> func(1, "a")
    True
    """
    return True

What Spyder does is that it takes this plain text description, parses and renders it as html and finally shows it in the Object Inspector.

To see it you just need to call func somewhere else in your code and press Ctrl+i next to it, like this:

func<Ctrl+i>(1, "a")

This is also shown automatically when you write a left parenthesis next to func.

like image 127
Carlos Cordoba Avatar answered Oct 09 '22 07:10

Carlos Cordoba


If you have your Python project (or file) already documented with an other style (as reStructuredText or Epytext) or not documented, you can generate/convert the docstrings into NumpyDoc style using Pyment:

pyment -o numpydoc /my/python/project

Note that the previous command, run after installed Pyment, will generate patches that should be applied to your code.

Once your project is documented using Numpydoc style, you can use the Sphinx extension to generate your nice readable NumpyDoc style documentation!

like image 21
daouzli Avatar answered Oct 09 '22 06:10

daouzli