CptS 481 - Python Software Construction

Digression: Restructured Text (aka "ReST")

Before we talk about Python documentation, let me show you something that will be useful even if you never write another line of Python. (We'll merge the two later.)

Unit 12: Documenting

Issues

pydoc (actually, pydoc3)

General idea: Put Python strings ("docstrings") at strategic places at the start of:

They appear as __doc__ attributes.

Example: The polar Module

Here's a module that implements polar coordinates. Recall we map $(r, \theta) \rightarrow (x, y)$ via $$ x = r \cos{\theta} \\ y = r \sin{\theta} $$ and we can map $(x, y)$ to a complex number $z$ via $$ z = x + j y $$ Here's the contents of polar.py:

Here's what happens if you use the pydoc3 command on this module.

Actually, pydoc3 is actually shorthand for python3 -m pydoc. We could have just as easily entered:

(A ", /" in a function prototype indicates that all of the preceding arguments are "positional only": You can't pass them by keyword. This is a new feature of Python 3.8. You can use this in your own function and method definitions.)

Or we can generate HTML output:

Or we can start a small HTTP server. (Do this in a shell window, not Jupyter.)

% pydoc3 -b

Note that, except for magic methods, functions and methods whose names begin with a leading underscore (e.g., _hiddenFunction()) won't appear in the documentation. (We'll demonstrate this.)

You'll notice that the documentation included that of sin() and cos(), even though they weren't defined in the module. This is because we imported them into the module's namespace. If you don't want them to appear in the documentation, you can use the "_" prefix when you import them:

from module import function as _function.

sphinx

Current Python documentation standard. Used for:

Here's an enhanced version of polar.py:

Additional work will be in the demonstration directories.