Debian Codegraph @

doc/usage/extensions/example_numpy.py

raw
1 """Example NumPy style docstrings.
2
3 This module demonstrates documentation as specified by the `NumPy
4 Documentation HOWTO`_. Docstrings may extend over multiple lines. Sections
5 are created with a section header followed by an underline of equal length.
6
7 Example
8 -------
9 Examples can be given using either the ``Example`` or ``Examples``
10 sections. Sections support any reStructuredText formatting, including
11 literal blocks::
12
13 $ python example_numpy.py
14
15
16 Section breaks are created with two blank lines. Section breaks are also
17 implicitly created anytime a new section starts. Section bodies *may* be
18 indented:
19
20 Notes
21 -----
22 This is an example of an indented section. It's like any other section,
23 but the body is indented to help it stand out from surrounding text.
24
25 If a section is indented, then a section break is created by
26 resuming unindented text.
27
28 Attributes
29 ----------
30 module_level_variable1 : int
31 Module level variables may be documented in either the ``Attributes``
32 section of the module docstring, or in an inline docstring immediately
33 following the variable.
34
35 Either form is acceptable, but the two should not be mixed. Choose
36 one convention to document module level variables and be consistent
37 with it.
38
39
40 .. _NumPy docstring standard:
41 https://numpydoc.readthedocs.io/en/latest/format.html#docstring-standard
42
43 """
44
45 module_level_variable1 = 12345
46
47 module_level_variable2 = 98765
48 """int: Module level variable documented inline.
49
50 The docstring may span multiple lines. The type may optionally be specified
51 on the first line, separated by a colon.
52 """
53
54
55 def function_with_types_in_docstring(param1, param2):
56 """Example function with types documented in the docstring.
57
58 :pep:`484` type annotations are supported. If attribute, parameter, and
59 return types are annotated according to `PEP 484`_, they do not need to be
60 included in the docstring:
61
62 Parameters
63 ----------
64 param1 : int
65 The first parameter.
66 param2 : str
67 The second parameter.
68
69 Returns
70 -------
71 bool
72 True if successful, False otherwise.
73 """
74
75
76 def function_with_pep484_type_annotations(param1: int, param2: str) -> bool:
77 """Example function with PEP 484 type annotations.
78
79 The return type must be duplicated in the docstring to comply
80 with the NumPy docstring style.
81
82 Parameters
83 ----------
84 param1
85 The first parameter.
86 param2
87 The second parameter.
88
89 Returns
90 -------
91 bool
92 True if successful, False otherwise.
93
94 """
95
96
97 def module_level_function(param1, param2=None, *args, **kwargs):
98 """This is an example of a module level function.
99
100 Function parameters should be documented in the ``Parameters`` section.
101 The name of each parameter is required. The type and description of each
102 parameter is optional, but should be included if not obvious.
103
104 If ``*args`` or ``**kwargs`` are accepted,
105 they should be listed as ``*args`` and ``**kwargs``.
106
107 The format for a parameter is::
108
109 name : type
110 description
111
112 The description may span multiple lines. Following lines
113 should be indented to match the first line of the description.
114 The ": type" is optional.
115
116 Multiple paragraphs are supported in parameter
117 descriptions.
118
119 Parameters
120 ----------
121 param1 : int
122 The first parameter.
123 param2 : :obj:`str`, optional
124 The second parameter.
125 *args
126 Variable length argument list.
127 **kwargs
128 Arbitrary keyword arguments.
129
130 Returns
131 -------
132 bool
133 True if successful, False otherwise.
134
135 The return type is not optional. The ``Returns`` section may span
136 multiple lines and paragraphs. Following lines should be indented to
137 match the first line of the description.
138
139 The ``Returns`` section supports any reStructuredText formatting,
140 including literal blocks::
141
142 {
143 'param1': param1,
144 'param2': param2,
145 }
146
147 Raises
148 ------
149 AttributeError
150 The ``Raises`` section is a list of all exceptions
151 that are relevant to the interface.
152 ValueError
153 If `param2` is equal to `param1`.
154
155 """
156 if param1 == param2:
157 msg = 'param1 may not be equal to param2'
158 raise ValueError(msg)
159 return True
160
161
162 def example_generator(n):
163 """Generators have a ``Yields`` section instead of a ``Returns`` section.
164
165 Parameters
166 ----------
167 n : int
168 The upper limit of the range to generate, from 0 to `n` - 1.
169
170 Yields
171 ------
172 int
173 The next number in the range of 0 to `n` - 1.
174
175 Examples
176 --------
177 Examples should be written in doctest format, and should illustrate how
178 to use the function.
179
180 >>> print([i for i in example_generator(4)])
181 [0, 1, 2, 3]
182
183 """
184 yield from range(n)
185
186
187 class ExampleError(Exception):
188 """Exceptions are documented in the same way as classes.
189
190 The __init__ method may be documented in either the class level
191 docstring, or as a docstring on the __init__ method itself.
192
193 Either form is acceptable, but the two should not be mixed. Choose one
194 convention to document the __init__ method and be consistent with it.
195
196 Note
197 ----
198 Do not include the `self` parameter in the ``Parameters`` section.
199
200 Parameters
201 ----------
202 msg : str
203 Human readable string describing the exception.
204 code : :obj:`int`, optional
205 Numeric error code.
206
207 Attributes
208 ----------
209 msg : str
210 Human readable string describing the exception.
211 code : int
212 Numeric error code.
213
214 """
215
216 def __init__(self, msg, code):
217 self.msg = msg
218 self.code = code
219
220
221 class ExampleClass:
222 """The summary line for a class docstring should fit on one line.
223
224 If the class has public attributes, they may be documented here
225 in an ``Attributes`` section and follow the same formatting as a
226 function's ``Args`` section. Alternatively, attributes may be documented
227 inline with the attribute's declaration (see __init__ method below).
228
229 Properties created with the ``@property`` decorator should be documented
230 in the property's getter method.
231
232 Attributes
233 ----------
234 attr1 : str
235 Description of `attr1`.
236 attr2 : :obj:`int`, optional
237 Description of `attr2`.
238
239 """
240
241 def __init__(self, param1, param2, param3):
242 """Example of docstring on the __init__ method.
243
244 The __init__ method may be documented in either the class level
245 docstring, or as a docstring on the __init__ method itself.
246
247 Either form is acceptable, but the two should not be mixed. Choose one
248 convention to document the __init__ method and be consistent with it.
249
250 Note
251 ----
252 Do not include the `self` parameter in the ``Parameters`` section.
253
254 Parameters
255 ----------
256 param1 : str
257 Description of `param1`.
258 param2 : list(str)
259 Description of `param2`. Multiple
260 lines are supported.
261 param3 : :obj:`int`, optional
262 Description of `param3`.
263
264 """
265 self.attr1 = param1
266 self.attr2 = param2
267 self.attr3 = param3 #: Doc comment *inline* with attribute
268
269 #: list(str): Doc comment *before* attribute, with type specified
270 self.attr4 = ['attr4']
271
272 self.attr5 = None
273 """str: Docstring *after* attribute, with type specified."""
274
275 @property
276 def readonly_property(self):
277 """str: Properties should be documented in their getter method."""
278 return 'readonly_property'
279
280 @property
281 def readwrite_property(self):
282 """list(str): Properties with both a getter and setter
283 should only be documented in their getter method.
284
285 If the setter method contains notable behavior, it should be
286 mentioned here.
287 """
288 return ['readwrite_property']
289
290 @readwrite_property.setter
291 def readwrite_property(self, value):
292 _ = value
293
294 def example_method(self, param1, param2):
295 """Class methods are similar to regular functions.
296
297 Note
298 ----
299 Do not include the `self` parameter in the ``Parameters`` section.
300
301 Parameters
302 ----------
303 param1
304 The first parameter.
305 param2
306 The second parameter.
307
308 Returns
309 -------
310 bool
311 True if successful, False otherwise.
312
313 """
314 return True
315
316 def __special__(self):
317 """By default special members with docstrings are not included.
318
319 Special members are any methods or attributes that start with and
320 end with a double underscore. Any special member with a docstring
321 will be included in the output, if
322 ``napoleon_include_special_with_doc`` is set to True.
323
324 This behavior can be enabled by changing the following setting in
325 Sphinx's conf.py::
326
327 napoleon_include_special_with_doc = True
328
329 """
330 pass
331
332 def __special_without_docstring__(self):
333 pass
334
335 def _private(self):
336 """By default private members are not included.
337
338 Private members are any methods or attributes that start with an
339 underscore and are *not* special. By default they are not included
340 in the output.
341
342 This behavior can be changed such that private members *are* included
343 by changing the following setting in Sphinx's conf.py::
344
345 napoleon_include_private_with_doc = True
346
347 """
348 pass
349
350 def _private_without_docstring(self):
351 pass