##// END OF EJS Templates
Add slightly more detailed guidelines about docstrings....
Fernando Perez -
Show More
@@ -1,104 +1,143 b''
1 .. _documenting-ipython:
1 .. _documenting-ipython:
2
2
3 =====================
3 =====================
4 Documenting IPython
4 Documenting IPython
5 =====================
5 =====================
6
6
7 When contributing code to IPython, you should strive for clarity and
8 consistency, without falling prey to a style straitjacket. Basically,
9 'document everything, try to be consistent, do what makes sense.'
10
11 By and large we follow existing Python practices in major projects like Python
12 itself or NumPy, this document provides some additional detail for IPython.
13
14
7 Standalone documentation
15 Standalone documentation
8 ========================
16 ========================
9
17
10 All standalone documentation should be written in plain text (``.txt``) files
18 All standalone documentation should be written in plain text (``.txt``) files
11 using reStructuredText [reStructuredText]_ for markup and formatting. All such
19 using reStructuredText [reStructuredText]_ for markup and formatting. All such
12 documentation should be placed in the directory :file:`docs/source` of the
20 documentation should be placed in the directory :file:`docs/source` of the
13 IPython source tree. Or, when appropriate, a suitably named subdirectory
21 IPython source tree. Or, when appropriate, a suitably named subdirectory
14 should be used. The documentation in this location will serve as the main
22 should be used. The documentation in this location will serve as the main
15 source for IPython documentation.
23 source for IPython documentation.
16
24
17 The actual HTML and PDF docs are built using the Sphinx [Sphinx]_
25 The actual HTML and PDF docs are built using the Sphinx [Sphinx]_
18 documentation generation tool. Once you have Sphinx installed, you can build
26 documentation generation tool. Once you have Sphinx installed, you can build
19 the html docs yourself by doing:
27 the html docs yourself by doing:
20
28
21 .. code-block:: bash
29 .. code-block:: bash
22
30
23 $ cd ipython-mybranch/docs
31 $ cd ipython-mybranch/docs
24 $ make html
32 $ make html
25
33
26 Our usage of Sphinx follows that of matplotlib [Matplotlib]_ closely. We are
34 Our usage of Sphinx follows that of matplotlib [Matplotlib]_ closely. We are
27 using a number of Sphinx tools and extensions written by the matplotlib team
35 using a number of Sphinx tools and extensions written by the matplotlib team
28 and will mostly follow their conventions, which are nicely spelled out in
36 and will mostly follow their conventions, which are nicely spelled out in
29 their documentation guide [MatplotlibDocGuide]_. What follows is thus a
37 their documentation guide [MatplotlibDocGuide]_. What follows is thus a
30 abridged version of the matplotlib documentation guide, taken with permission
38 abridged version of the matplotlib documentation guide, taken with permission
31 from the matplotlib team.
39 from the matplotlib team.
32
40
33 If you are reading this in a web browser, you can click on the "Show Source"
41 If you are reading this in a web browser, you can click on the "Show Source"
34 link to see the original reStricturedText for the following examples.
42 link to see the original reStricturedText for the following examples.
35
43
36 A bit of Python code::
44 A bit of Python code::
37
45
38 for i in range(10):
46 for i in range(10):
39 print i,
47 print i,
40 print "A big number:",2**34
48 print "A big number:",2**34
41
49
42 An interactive Python session::
50 An interactive Python session::
43
51
44 >>> from IPython.utils.path import get_ipython_dir
52 >>> from IPython.utils.path import get_ipython_dir
45 >>> get_ipython_dir()
53 >>> get_ipython_dir()
46 '/home/fperez/.ipython'
54 '/home/fperez/.ipython'
47
55
48 An IPython session:
56 An IPython session:
49
57
50 .. code-block:: ipython
58 .. code-block:: ipython
51
59
52 In [7]: import IPython
60 In [7]: import IPython
53
61
54 In [8]: print "This IPython is version:",IPython.__version__
62 In [8]: print "This IPython is version:",IPython.__version__
55 This IPython is version: 0.9.1
63 This IPython is version: 0.9.1
56
64
57 In [9]: 2+4
65 In [9]: 2+4
58 Out[9]: 6
66 Out[9]: 6
59
67
60
68
61 A bit of shell code:
69 A bit of shell code:
62
70
63 .. code-block:: bash
71 .. code-block:: bash
64
72
65 cd /tmp
73 cd /tmp
66 echo "My home directory is: $HOME"
74 echo "My home directory is: $HOME"
67 ls
75 ls
68
76
69 Docstring format
77 Docstring format
70 ================
78 ================
71
79
72 Good docstrings are very important. Unfortunately, Python itself only provides
80 Good docstrings are very important. Unfortunately, Python itself only provides
73 a rather loose standard for docstrings [PEP257]_, and there is no universally
81 a rather loose standard for docstrings [PEP257]_, and there is no universally
74 accepted convention for all the different parts of a complete docstring.
82 accepted convention for all the different parts of a complete docstring.
75 However, the NumPy project has established a very reasonable standard, and has
83 However, the NumPy project has established a very reasonable standard, and has
76 developed some tools to support the smooth inclusion of such docstrings in
84 developed some tools to support the smooth inclusion of such docstrings in
77 Sphinx-generated manuals. Rather than inventing yet another pseudo-standard,
85 Sphinx-generated manuals. Rather than inventing yet another pseudo-standard,
78 IPython will be henceforth documented using the NumPy conventions; we carry
86 IPython will be henceforth documented using the NumPy conventions; we carry
79 copies of some of the NumPy support tools to remain self-contained, but share
87 copies of some of the NumPy support tools to remain self-contained, but share
80 back upstream with NumPy any improvements or fixes we may make to the tools.
88 back upstream with NumPy any improvements or fixes we may make to the tools.
81
89
82 The NumPy documentation guidelines [NumPyDocGuide]_ contain detailed
90 The NumPy documentation guidelines [NumPyDocGuide]_ contain detailed
83 information on this standard, and for a quick overview, the NumPy example
91 information on this standard, and for a quick overview, the NumPy example
84 docstring [NumPyExampleDocstring]_ is a useful read.
92 docstring [NumPyExampleDocstring]_ is a useful read.
85
93
86 In the past IPython used epydoc so currently many docstrings still use epydoc
94
87 conventions. We will update them as we go, but all new code should be
95 For user-facing APIs, we try to be fairly strict about following the above
88 documented using the NumPy standard.
96 standards (even though they mean more verbose and detailed docstrings).
97 Wherever you can reasonably expect people to do introspection with::
98
99 In [1]: some_function?
100
101 the docstring should follow the NumPy style and be fairly detailed.
102
103 For purely internal methods that are only likely to be read by others extending
104 IPython itself we are a bit more relaxed, especially for small/short methods
105 and functions whose intent is reasonably obvious. We still expect docstrings
106 to be written, but they can be simpler. For very short functions with a
107 single-line docstring you can use something like::
108
109 def add(a, b):
110 """The sum of two numbers.
111 """
112 code
113
114 and for longer multiline strings::
115
116 def add(a, b):
117 """The sum of two numbers.
118
119 Here is the rest of the docs.
120 """
121 code
122
89
123
90 Here are two additional PEPs of interest regarding documentation of code.
124 Here are two additional PEPs of interest regarding documentation of code.
91 While both of these were rejected, the ideas therein form much of the basis of
125 While both of these were rejected, the ideas therein form much of the basis of
92 docutils (the machinery to process reStructuredText):
126 docutils (the machinery to process reStructuredText):
93
127
94 * `Docstring Processing System Framework <http://www.python.org/peps/pep-0256.html>`_
128 * `Docstring Processing System Framework <http://www.python.org/peps/pep-0256.html>`_
95 * `Docutils Design Specification <http://www.python.org/peps/pep-0258.html>`_
129 * `Docutils Design Specification <http://www.python.org/peps/pep-0258.html>`_
96
130
131 .. note::
132
133 In the past IPython used epydoc so currently many docstrings still use
134 epydoc conventions. We will update them as we go, but all new code should
135 be documented using the NumPy standard.
97
136
98 .. [reStructuredText] reStructuredText. http://docutils.sourceforge.net/rst.html
137 .. [reStructuredText] reStructuredText. http://docutils.sourceforge.net/rst.html
99 .. [Sphinx] Sphinx. http://sphinx.pocoo.org/
138 .. [Sphinx] Sphinx. http://sphinx.pocoo.org/
100 .. [MatplotlibDocGuide] http://matplotlib.sourceforge.net/devel/documenting_mpl.html
139 .. [MatplotlibDocGuide] http://matplotlib.sourceforge.net/devel/documenting_mpl.html
101 .. [PEP257] PEP 257. http://www.python.org/peps/pep-0257.html
140 .. [PEP257] PEP 257. http://www.python.org/peps/pep-0257.html
102 .. [NumPyDocGuide] NumPy documentation guide. http://projects.scipy.org/numpy/wiki/CodingStyleGuidelines
141 .. [NumPyDocGuide] NumPy documentation guide. http://projects.scipy.org/numpy/wiki/CodingStyleGuidelines
103 .. [NumPyExampleDocstring] NumPy example docstring. http://projects.scipy.org/numpy/browser/trunk/doc/EXAMPLE_DOCSTRING.txt
142 .. [NumPyExampleDocstring] NumPy example docstring. http://projects.scipy.org/numpy/browser/trunk/doc/EXAMPLE_DOCSTRING.txt
104
143
General Comments 0
You need to be logged in to leave comments. Login now