Skip to content

Commit e77714c

Browse files
echedey-lsRDaxiniAdamRJensenIoannisSifnaioscwhanse
authored
[DOC]: Add docstring example to the contributing docs (#2254)
* Add docstring example * Update style_guide.rst Co-Authored-By: RDaxini <[email protected]> * Update style_guide.rst * Update style_guide.rst Co-Authored-By: Adam R. Jensen <[email protected]> * Update example_function_screenshot.png * Double check parameters Co-Authored-By: Ioannis Sifnaios <[email protected]> * datatypes Co-Authored-By: Ioannis Sifnaios <[email protected]> * Update interactive example Co-Authored-By: Ioannis Sifnaios <[email protected]> * Update example_function_screenshot.png * Add some little code to example Co-Authored-By: Adam R. Jensen <[email protected]> Co-Authored-By: Ioannis Sifnaios <[email protected]> * Update v0.11.2.rst * Apply suggestions from Cliff Co-authored-by: Cliff Hansen <[email protected]> * Update example_function_screenshot.png --------- Co-authored-by: RDaxini <[email protected]> Co-authored-by: Adam R. Jensen <[email protected]> Co-authored-by: Ioannis Sifnaios <[email protected]> Co-authored-by: Cliff Hansen <[email protected]>
1 parent 2a70f4b commit e77714c

File tree

3 files changed

+113
-1
lines changed

3 files changed

+113
-1
lines changed
Loading

docs/sphinx/source/contributing/style_guide.rst

+111-1
Original file line numberDiff line numberDiff line change
@@ -163,7 +163,7 @@ An easy way to do this is with::
163163

164164
Note: Anaconda users may have trouble using the above command to update an
165165
older version of docutils. If that happens, you can update it with ``conda``
166-
(e.g. ``conda install docutils=0.15.2``) and run the above command again.
166+
(e.g. ``conda install docutils=0.21``) and run the above command again.
167167

168168
Once the ``doc`` dependencies are installed, navigate to ``/docs/sphinx`` and
169169
execute::
@@ -182,6 +182,116 @@ Note that Windows users need not have the ``make`` utility installed as pvlib
182182
includes a ``make.bat`` batch file that emulates its interface.
183183

184184

185+
.. _example-docstring:
186+
187+
Example Docstring
188+
-----------------
189+
190+
Here is a template for a function docstring that encapsulates the main
191+
features that may be used in any docstring. Note that not all sections are
192+
required for every function.
193+
194+
.. code-block:: python
195+
196+
def example_function(poa_global, exponents, degree_symbol, time_ref='UT',
197+
optional_arg=None):
198+
r"""
199+
One-sentence summary of the function (no citations).
200+
201+
A longer description of the function. This can include citations
202+
(references) to literature [1]_, websites [2]_, and other code elements
203+
such as functions (:py:func:`pvlib.location.lookup_altitude`) and
204+
classes (:py:class:`pvlib.location.Location`).
205+
206+
.. versionadded:: 0.0.1
207+
There are many more purpose-specific directives, admonitions and such
208+
available at `this link <admonitions>`_. E.g.: ``.. versionchanged::``,
209+
``.. deprecated::``, ``.. note::`` and ``.. warning::``.
210+
211+
.. _admonitions: https://www.sphinx-doc.org/en/master/usage/restructuredtext/directives.html#admonitions-messages-and-warnings
212+
213+
Parameters
214+
----------
215+
poa_global : numeric
216+
Plane-of-array global irradiance, see :term:`poa_global`. [Wm⁻²].
217+
exponents : array-like
218+
A list of exponents. [x⁰¹²³⁴⁵⁶⁷⁸⁹⁻].
219+
degree_symbol : pandas.Series or pandas.DataFrame
220+
It's different from superscript zero. [°].
221+
time_ref : ``'UT'`` or ``'TST'``, default: ``'UT'``
222+
``'UT'`` (universal time) or ``'TST'`` (True Solar Time).
223+
optional_arg : integer, optional
224+
A description of ``optional_arg``. [Unitless].
225+
226+
Specify a suitable datatype for each parameter. Other common
227+
data types include ``str``, ``bool``, ``float``, ``numeric``
228+
and ``pandas.DatetimeIndex``.
229+
230+
Returns
231+
-------
232+
name : numeric
233+
A description of the return value.
234+
235+
Raises
236+
------
237+
ValueError
238+
If ``poa_global`` is negative.
239+
KeyError
240+
If ``time_ref`` does not exist.
241+
242+
Notes
243+
-----
244+
This section can include additional information about the function.
245+
246+
For example, an equation using LaTeX markup:
247+
248+
.. math::
249+
250+
a = \left(\frac{b}{c}\right)^2
251+
252+
where :math:`a` is the result of the equation, and :math:`b` and :math:`c`
253+
are inputs.
254+
255+
Or a figure with a caption:
256+
257+
.. figure:: ../../_images/pvlib_logo_horiz.png
258+
:scale: 10%
259+
:alt: alternate text
260+
:align: center
261+
262+
Figure caption.
263+
264+
See Also
265+
--------
266+
pvlib.location.lookup_altitude, pvlib.location.Location
267+
268+
Examples
269+
--------
270+
>>> example_function(1, 1, pd.Series([1]), "TST", 2)
271+
'Something'
272+
273+
References
274+
----------
275+
A IEEE citation to a relevant reference. You may use an automatic
276+
citation generator to format the citation correctly.
277+
278+
.. [1] Anderson, K., Hansen, C., Holmgren, W., Jensen, A., Mikofski, M.,
279+
and Driesse, A. "pvlib python: 2023 project update." Journal of Open
280+
Source Software, 8(92), 5994, (2023). :doi:`10.21105/joss.05994`.
281+
.. [2] J. Smith and J. Doe. "Obama inaugurated as President." CNN.com.
282+
Accessed: Feb. 1, 2009. [Online.]
283+
Available: http://www.cnn.com/POLITICS/01/21/obama_inaugurated/index.html
284+
"""
285+
a = "Some"
286+
b = "thing"
287+
return a + b
288+
289+
A preview of how this docstring would render in the documentation can be seen in the
290+
following file: :download:`Example docstring<../_images/example_function_screenshot.png>`.
291+
292+
Remember that to show the docstring in the documentation, you must list
293+
the function in the appropriate ``.rst`` file in the ``docs/sphinx/source/reference`` folder.
294+
185295
.. _example-gallery:
186296

187297
Example Gallery

docs/sphinx/source/whatsnew/v0.11.2.rst

+2
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ Documentation
2525
page. (:issue:`2202`, :pull:`2226`)
2626
* Updated :py:func:`~pvlib.irradiance.reindl` to include definitions of terms
2727
and a new "notes" section (:issue:`2183`, :pull:`2193`)
28+
* Explained how to write docstrings for new functions in :ref:`example-docstring`
29+
(:discussion:`2081`, :pull:`2254`)
2830

2931
Testing
3032
~~~~~~~

0 commit comments

Comments
 (0)