|
1 | | -Documenting Your Code |
2 | | -===================== |
3 | | - |
4 | | -With readability of the code being a main focus for Python developers, proper |
5 | | -commenting is naturally important. Some best practice apply to code comments |
6 | | -and project documents, depending on the scope of the project. |
7 | | - |
8 | | -Project documents |
9 | | ------------------ |
10 | | - |
11 | | -A README file at the root directory give general information to the users and |
12 | | -the maintainers. It should be raw text or written in some very easy to read |
13 | | -markup, such as reStructuredText and Markdown. It should contain a few lines |
14 | | -explaining the purpose of the project or the library (without assuming the user |
15 | | -knows anything about the project), the url of the main source for the software, |
16 | | -and some basic credit information. This file is the main entry point for |
17 | | -readers of the code. |
18 | | - |
19 | | -An INSTALL file is less often necessary with python, except if the dependencies |
20 | | -are complex or unusual, or if some C modules need to be compiled before use. |
21 | | -The installation instructions are often reduced to one command, such as ``pip |
22 | | -install module`` or ``python setup.py install`` and added to the README file. |
23 | | - |
24 | | -A LICENSE file should always be present and specify the license under which the |
| 1 | +Documentation |
| 2 | +============= |
| 3 | + |
| 4 | +Readability is a primary focus for Python developers, in both project |
| 5 | +and code documentation. Following some simple best practices can save |
| 6 | +both you and others a lot of time. |
| 7 | + |
| 8 | +Project Documentation |
| 9 | +--------------------- |
| 10 | + |
| 11 | +A ``README`` file at the root directory should give general |
| 12 | +information to the users and the maintainers. It should be raw text or |
| 13 | +written in some very easy to read markup, such as |
| 14 | +:ref:`reStructuredText-ref` and Markdown. It should contain a few |
| 15 | +lines explaining the purpose of the project or the library (without |
| 16 | +assuming the user knows anything about the project), the url of the |
| 17 | +main source for the software, and some basic credit information. This |
| 18 | +file is the main entry point for readers of the code. |
| 19 | + |
| 20 | +An ``INSTALL`` file is less necessary with python. The installation |
| 21 | +instructions are often reduced to one command, such as ``pip install |
| 22 | +module`` or ``python setup.py install`` and added to the ``README`` |
| 23 | +file. |
| 24 | + |
| 25 | +A ``LICENSE`` file should *always* be present and specify the license under which the |
25 | 26 | software is made available to the public. |
26 | 27 |
|
27 | | -A TODO file or a TODO section in README should list the planned modifications |
28 | | -of the code. |
| 28 | +A ``TODO`` file or a ``TODO`` section in ``README`` should list the |
| 29 | +planned development for the code. |
29 | 30 |
|
30 | | -A CHANGELOG file or section in README should compile a short overview of the |
31 | | -changes in the code base for the latest versions. |
| 31 | +A ``CHANGELOG`` file or section in ``README`` should compile a short |
| 32 | +overview of the changes in the code base for the latest versions. |
32 | 33 |
|
33 | | -Documentation |
34 | | -------------- |
35 | | - |
36 | | -As the project or library reaches a certain level of complexity, it may require |
37 | | -a fuller documentation, which can be of different flavors: |
38 | | - |
39 | | -The introduction may show a very short overview of what can be done with the |
40 | | -product, using one or two extremely simplified use cases. |
41 | | - |
42 | | -The tutorials will show in more details some main use cases. The reader will |
43 | | -follow a step-by-step procedure to set-up a working prototype. |
| 34 | +Project Publication |
| 35 | +------------------- |
44 | 36 |
|
45 | | -The API reference, which is often generated automatically from the code, will |
46 | | -list all publicly available interfaces, their parameters and their return |
47 | | -values, with an explanation of their use. |
| 37 | +Depending on the project, your documentation might include some or all |
| 38 | +of the following components: |
48 | 39 |
|
49 | | -Some documents intended for developers might give guidance about code |
50 | | -convention and general design decision of the project. |
| 40 | +- A *introduction* should show a very short overview of what can be |
| 41 | + done with the product, using one or two extremely simplified use |
| 42 | + cases. This is the thirty-second pitch for your project. |
51 | 43 |
|
52 | | -Comments |
53 | | --------- |
| 44 | +- A *tutorial* should show some primary use cases in more detail. The reader will |
| 45 | + follow a step-by-step procedure to set-up a working prototype. |
54 | 46 |
|
55 | | -Comments are written directly inside the code, either using the hash sign (#) |
56 | | -or a docstring_. |
57 | | - |
58 | | -.. _docstring: docstrings_ |
| 47 | +- An *API reference* is typically generated from the code (see |
| 48 | + :ref:`docstrings <docstring-ref>`). It will list all publicly available interfaces, |
| 49 | + parameters, and return values. |
59 | 50 |
|
60 | | -Finding the correct balance between undocumented code and verbose and useless |
61 | | -comment boilerplates is difficult, and is the subject of heated discussion |
62 | | -among developers. |
| 51 | +- *Developer documentation* is intended for potential contributors. This can |
| 52 | + include code convention and general design strategy of the project. |
63 | 53 |
|
64 | | -The following guidelines seem to be most commonly agreed upon: |
65 | | - |
66 | | -**Wrong or outdated comments are worse than no comments at all.** Following the |
67 | | -saying that it is better, on a boat, to know that we do not know were we are |
68 | | -than to wrongly believe we know where we are, wrong or outdated comments can be |
69 | | -misleading for the maintainers, slow down considerably bug hunting or |
70 | | -refactoring, and then, when discovered wrong, they will throw suspicion on all |
71 | | -other comments in the code, regardless of their individual correctness. |
72 | | - |
73 | | -**There's no need to comment perfect code...** An hypothetical perfectly readable |
74 | | -code, with a crystal clear logic stream, expressive variable and function |
75 | | -names, orthogonal segmentation passing exactly between the flesh and the bones, |
76 | | -and no implicit assumptions of any kind, would not require any comment at all. |
77 | | -When striving for coding excellence, it is useful to see any existing comment, |
78 | | -or any feeling of a need for a comment, as the sign that the code do not |
79 | | -express clearly enough its intent and can be improved. |
80 | | - |
81 | | -**.. but no code is perfect.** Perfect code is a chimera, it exists only in |
82 | | -our dreams. In real life, a code base is full of trade offs, and comments are |
83 | | -often needed in the most difficult parts. Moreover, any special case, any |
84 | | -obscure hack, any monkey patch and any ugly workaround MUST be signaled and |
85 | | -explained by a proper comment. This should be enforced by the law! |
86 | | - |
87 | | -**TODOs** are special comments that a developer write as a reminder for later |
88 | | -use. It is said that its original intent was that someone might, one day, |
89 | | -search for the string "TODO" in the code base and actually roll their sleeves |
90 | | -and start *to do the TODOs*. There is no available record that it ever |
91 | | -happened. However, TODOs comment are still very useful, because they mark the |
92 | | -current limits of the code, and it is not unlikely that, when required to add a |
93 | | -new behavior to the actual code, looking at the TODOs will show where to start. |
94 | | - |
95 | | -**Do not use triple-quote strings to comment code.** A common operation when |
96 | | -modifying code is to comment out some lines or even a full function or class |
97 | | -definition. This can be done by adding triple-quotes around the code block to |
98 | | -be skipped, but this is not a good practice, because line-oriented command-line |
99 | | -tools such as ``grep`` will not be aware that the commented code is inactive. |
100 | | -It is better to add hashes at the proper indentation level for every commented |
101 | | -line. Good editors allow to do this with few keystrokes (ctrl-v on Vim). |
102 | | - |
103 | | -**Bad** |
104 | | - |
105 | | -.. code-block:: python |
106 | | -
|
107 | | - def tricky_function(): |
108 | | - ''' |
109 | | - Commented out because its breaks something. |
110 | | - if foo: |
111 | | - do_bar() |
112 | | - ''' |
113 | | - return baz |
114 | | -
|
115 | | - def tricky_function(): |
116 | | - # Commented out because its breaks something. |
117 | | - #if foo: |
118 | | - #do_bar() |
119 | | - return baz |
120 | | -
|
121 | | -
|
122 | | - def tricky_function(): |
123 | | - # Commented out because its breaks something. |
124 | | - # if foo: |
125 | | - # do_bar() |
126 | | - return baz |
127 | | -
|
128 | | -**Good** |
129 | | - |
130 | | -.. code-block:: python |
131 | | -
|
132 | | - def tricky_function(): |
133 | | - # Commented out because its breaks something. |
134 | | - #if foo: |
135 | | - # do_bar() |
136 | | - return baz |
137 | | -
|
138 | | -Note that comment text is properly written and separated from the hash by a |
139 | | -space. Commented code is not separated from the hash by an additional space; |
140 | | -this helps when uncommented the code. |
141 | | - |
142 | | -The Basics |
143 | | -:::::::::: |
144 | | - |
145 | | - |
146 | | -Code Comments |
147 | | -------------- |
148 | | - |
149 | | -Information regarding code comments is taken from PEP 008 (http://www.python.org/dev/peps/pep-0008/). |
150 | | -Block comment styling should be used when commenting out multiple lines of code.: :: |
151 | | - |
152 | | - Block comments generally apply to some (or all) code that follows them, |
153 | | - and are indented to the same level as that code. Each line of a block |
154 | | - comment starts with a # and a single space (unless it is indented text |
155 | | - inside the comment). |
156 | | - Paragraphs inside a block comment are separated by a line containing a |
157 | | - single #. |
158 | | - |
159 | | -In-line comments are used for individual lines and should be used sparingly.: :: |
160 | | - |
161 | | - An inline comment is a comment on the same line as a statement. Inline |
162 | | - comments should be separated by at least two spaces from the statement. |
163 | | - They should start with a # and a single space. |
164 | | - Inline comments are unnecessary and in fact distracting if they state |
165 | | - the obvious. Don't do this: |
166 | | - x = x + 1 # Increment x |
167 | | - But sometimes, this is useful: :: |
168 | | - x = x + 1 # Compensate for border |
169 | | - |
170 | | -Docstrings |
171 | | ------------ |
| 54 | +.. _sphinx-ref: |
172 | 55 |
|
173 | | -PEP 257 is the primary reference for docstrings. (http://www.python.org/dev/peps/pep-0257/) |
| 56 | +Sphinx |
| 57 | +~~~~~~ |
174 | 58 |
|
175 | | -There are two types of docstrings, one-line and multi-line. Their names |
176 | | -should be fairly self explanatory. |
177 | | -One-line docstrings: :: |
| 59 | +Sphinx_ is far and away the most popular python documentation |
| 60 | +tool. **Use it.** It converts :ref:`restructuredtext-ref` markup language |
| 61 | +into a range of output formats including HTML, LaTeX (for printable |
| 62 | +PDF versions), manual pages, and plain text. |
178 | 63 |
|
179 | | - def kos_root(): |
180 | | - """Return the pathname of the KOS root directory.""" |
181 | | - global _kos_root |
182 | | - if _kos_root: return _kos_root |
183 | | - ... |
| 64 | +There is also **great**, **free** hosting for your Sphinx_ docs: |
| 65 | +`Read The Docs`_. Use it. You can configure it with commit hooks to |
| 66 | +your source repository so that rebuilding your documentation will |
| 67 | +happen automatically. |
184 | 68 |
|
185 | | -Multi-line docstrings: :: |
| 69 | +.. note:: |
186 | 70 |
|
187 | | - def complex(real=0.0, imag=0.0): |
188 | | - """Form a complex number. |
| 71 | + Sphinx is famous for its API generation, but it also works well |
| 72 | + for general project documentation. This Guide is built with |
| 73 | + Sphinx_ and is hosted on `Read The Docs`_ |
189 | 74 |
|
190 | | - Keyword arguments: |
191 | | - real -- the real part (default 0.0) |
192 | | - imag -- the imaginary part (default 0.0) |
| 75 | +.. _Sphinx: http://sphinx.pocoo.org |
| 76 | +.. _Read The Docs: http://readthedocs.org |
193 | 77 |
|
194 | | - """ |
195 | | - if imag == 0.0 and real == 0.0: return complex_zero |
196 | | - ... |
| 78 | +.. _restructuredtext-ref: |
197 | 79 |
|
| 80 | +reStructuredText |
| 81 | +~~~~~~~~~~~~~~~~ |
198 | 82 |
|
199 | | -.. _sphinx-ref: |
| 83 | +Most Python documentation is written with reStructuredText_. It's like |
| 84 | +Markdown with all the optional extensions built in. |
200 | 85 |
|
| 86 | +The `reStructuredText Primer`_ and the `reStructuredText Quick |
| 87 | +Reference`_ should help you familiarize yourself with its syntax. |
201 | 88 |
|
202 | | -Sphinx |
203 | | ------- |
| 89 | +.. _reStructuredText: http://docutils.sourceforge.net/rst.html |
| 90 | +.. _reStructuredText Primer: http://sphinx.pocoo.org/rest.html |
| 91 | +.. _reStructuredText Quick Reference: http://docutils.sourceforge.net/docs/user/rst/quickref.html |
204 | 92 |
|
205 | | -Sphinx_ is a tool which converts documentation in the :ref:`restructuredtext-ref` |
206 | | -markup language into a range of output formats including HTML, LaTeX (for |
207 | | -printable PDF versions), manual pages and plain text. |
208 | 93 |
|
209 | | -There is also a great free hosting for your Sphinx_ docs: `Read The Docs`_ |
| 94 | +Code Documentation Advice |
| 95 | +------------------------- |
210 | 96 |
|
211 | | -.. note:: This Guide is built with Sphinx_ and hosted on `Read The Docs`_ |
| 97 | +Comments clarify code and begin with a hash (``#``). |
212 | 98 |
|
213 | | -.. _Sphinx: http://sphinx.pocoo.org |
214 | | -.. _Read The Docs: http://readthedocs.org |
| 99 | +.. _docstring-ref: |
215 | 100 |
|
216 | | -.. _restructuredtext-ref: |
| 101 | +In Python, *docstrings* describe modules, classes, and functions: :: |
217 | 102 |
|
| 103 | + def square_and_rooter(x): |
| 104 | + """Returns the square root of self times self.""" |
| 105 | + ... |
218 | 106 |
|
219 | | -reStructuredText |
220 | | ----------------- |
| 107 | +In general, follow the `comment section of PEP 0008`_ (the "Python Style Guide"). |
221 | 108 |
|
222 | | -Most Python documentation is written with reStructuredText_. The |
223 | | -`reStructuredText Primer <http://sphinx.pocoo.org/rest.html>`_ and the |
224 | | -`reStructuredText Quick Reference <http://docutils.sourceforge.net/docs/user/rst/quickref.html>`_ |
225 | | -should help you familiarize yourself with its syntax. |
| 109 | +.. _comment section of PEP 0008: http://www.python.org/dev/peps/pep-0008/#comments |
| 110 | + |
| 111 | +Commenting Sections of Code |
| 112 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 113 | + |
| 114 | +*Do not use triple-quote strings to comment code*. This is not a good |
| 115 | +practice, because line-oriented command-line tools such as grep will |
| 116 | +not be aware that the commented code is inactive. It is better to add |
| 117 | +hashes at the proper indentation level for every commented line. Your |
| 118 | +editor probably has the ability to do this easily, and it is worth |
| 119 | +learning the comment/uncomment toggle. (*e.g.* ctrl-v on Vim) |
| 120 | + |
| 121 | +Docstrings and Magic |
| 122 | +~~~~~~~~~~~~~~~~~~~~ |
| 123 | + |
| 124 | +Some tools use docstrings to embed more-than-documentation behavior, |
| 125 | +such as unit test logic. Those can be nice, but you won't ever go |
| 126 | +wrong with vanilla "here's what this does." |
| 127 | + |
| 128 | +Docstrings versus Block comments |
| 129 | +~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 130 | + |
| 131 | +These aren't interchangeable. For a function or class, the leading |
| 132 | +comment block is a programmer's note. The docstring describes the |
| 133 | +operation of the function or class: :: |
| 134 | + |
| 135 | + # This function slows down program execution for some reason. |
| 136 | + def square_and_rooter(x): |
| 137 | + """Returns the square root of self times self.""" |
| 138 | + ... |
| 139 | + |
| 140 | +.. seealso:: Further reading on docstrings: `PEP 0257`_ |
| 141 | + |
| 142 | +.. _PEP 0257: http://www.python.org/dev/peps/pep-0257/ |
226 | 143 |
|
227 | | -.. _reStructuredText: http://docutils.sourceforge.net/rst.html |
228 | 144 |
|
229 | 145 | Other Tools |
230 | | -::::::::::: |
| 146 | +----------- |
| 147 | + |
| 148 | +You might see these in the wild. Use :ref:`sphinx-ref`. |
| 149 | + |
| 150 | +Pycco_ |
| 151 | + Pycco is a "literate-programming-style documentation generator" |
| 152 | + and is a port of the node.js Docco_. It makes code into a |
| 153 | + side-by-side HTML code and documentation. |
231 | 154 |
|
| 155 | +.. _Pycco: http://fitzgen.github.com/pycco |
| 156 | +.. _Docco: http://jashkenas.github.com/docco |
232 | 157 |
|
233 | | -Epydoc |
234 | | ------- |
235 | | -`Epydoc <http://epydoc.sourceforge.net/>`_ generates API documentation based on docstrings. |
236 | | -Epydoc is able to parse docstrings marked up with :ref:`reStructuredText-ref`, |
237 | | -`Javadoc <http://www.oracle.com/technetwork/java/javase/documentation/index-jsp-135444.html#javadocdocuments>`_, |
238 | | -`epytext <http://epydoc.sourceforge.net/manual-epytext.html>`_ or plaintext. |
239 | | -It supports various output formats, most notable HTML, PDF or LaTeX documents. |
| 158 | +Ronn_ |
| 159 | + Ronn builds unix manuals. It converts human readable textfiles to roff for terminal display, and also to HTML for the web. |
240 | 160 |
|
241 | | -The development of Epydoc is discontinued. You should use :ref:`sphinx-ref` instead. |
| 161 | +.. _Ronn: https://github.com/rtomayko/ronn |
242 | 162 |
|
243 | | -pycco / docco / shocco |
244 | | ----------------------- |
| 163 | +Epydoc_ |
| 164 | + Epydoc is discontinued. Use :ref:`sphinx-ref` instead. |
245 | 165 |
|
246 | | -Ronn |
247 | | ----- |
| 166 | +.. _Epydoc: http://epydoc.sourceforge.net |
0 commit comments