Skip to content

Commit c9635fb

Browse files
authored
[Basics Concept Exercise]: Update docstrings and documents for guidos-gorgeous-lasagna (#4151)
* Updated stub docstrings to Google-style docstrings. * Updated docstring explanations and refs to use Google style.
1 parent 7de4681 commit c9635fb

4 files changed

Lines changed: 53 additions & 29 deletions

File tree

exercises/concept/guidos-gorgeous-lasagna/.docs/hints.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
## 5. Update the recipe with notes
3939

4040
- Clearly [commenting][comments] and [documenting][docstrings] your code according to [PEP257][pep257] is always recommended.
41+
- Some examples of Google-style docstrings can be found in the Sphinx documentation for the [napoleon module][napoleon].
4142

4243
[assignment]: https://docs.python.org/3/reference/simple_stmts.html#grammar-token-assignment-stmt
4344
[comments]: https://realpython.com/python-comments-guide/
@@ -46,6 +47,7 @@
4647
[docstrings]: https://docs.python.org/3/tutorial/controlflow.html#tut-docstrings
4748
[magic-numbers]: https://en.wikipedia.org/wiki/Magic_number_(programming)
4849
[naming]: https://realpython.com/python-variables/
50+
[napoleon]: https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#module-sphinx.ext.napoleon
4951
[numbers]: https://docs.python.org/3/tutorial/introduction.html#numbers
5052
[pep257]: https://www.python.org/dev/peps/pep-0257/
5153
[python as a calculator]: https://docs.python.org/3/tutorial/introduction.html#using-python-as-a-calculator

exercises/concept/guidos-gorgeous-lasagna/.docs/instructions.md

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,18 @@ Go back through the recipe, adding "notes" in the form of [function docstrings][
7878
```python
7979
def elapsed_time_in_minutes(number_of_layers, elapsed_bake_time):
8080
"""Calculate the elapsed cooking time.
81-
82-
:param number_of_layers: int - the number of layers in the lasagna.
83-
:param elapsed_bake_time: int - time the lasagna has been baking in the oven.
84-
:return: int - total time elapsed (in minutes) preparing and baking.
81+
82+
Parameters:
83+
number_of_layers (int): The number of layers in the lasagna.
84+
elapsed_bake_time (int): Time the lasagna has been baking in the oven.
85+
86+
Returns:
87+
int: The total time elapsed (in minutes) preparing and baking.
8588
8689
This function takes two integers representing the number of lasagna
8790
layers and the time already spent baking the lasagna. It calculates
8891
the total elapsed minutes spent cooking (preparing + baking).
92+
8993
"""
9094
```
9195

exercises/concept/guidos-gorgeous-lasagna/.docs/introduction.md

Lines changed: 33 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -209,13 +209,18 @@ Docstrings are declared using triple double quotes (""") indented at the same le
209209

210210
```python
211211

212-
# An example from PEP257 of a multi-line docstring.
212+
# An example from PEP257 of a multi-line docstring
213+
# reformatted to use Google style non-type hinted docstrings.
214+
# Some additional details can be found in the Sphinx documentation:
215+
# https://www.sphinx-doc.org/en/master/usage/extensions/napoleon.html#getting-started
216+
213217
def complex(real=0.0, imag=0.0):
214218
"""Form a complex number.
215219
216-
Keyword arguments:
217-
real -- the real part (default 0.0)
218-
imag -- the imaginary part (default 0.0)
220+
Keyword Arguments:
221+
real (float): The real part of the number (default 0.0)
222+
imag (float): The imaginary part of the number (default 0.0)
223+
219224
"""
220225

221226
if imag == 0.0 and real == 0.0:
@@ -224,36 +229,45 @@ def complex(real=0.0, imag=0.0):
224229
```
225230

226231

227-
Docstrings are read by automated documentation tools and are returned by calling the special attribute `.__doc__` on the function, method, or class name.
228-
Docstring conventions are laid out in [PEP257][pep257].
232+
Docstrings are read by automated documentation tools such as [Sphinx][sphinx] and are returned by calling the special attribute `.__doc__` on the function, method, or class name.
233+
General docstring conventions are laid out in [PEP257][pep257], but exact formats will vary by project and team.
234+
Exercism concept exercises try to follow the Google style for un-type hinted code.
229235

230236
Docstrings can also function as [lightweight unit tests][doctests], which will be covered in a later exercise.
231237

232238

233239
```python
234240
# An example on a user-defined function.
241+
# This uses Google style docstrings.
235242
>>> def raise_to_power(number, power):
236-
"""Raise a number to an arbitrary power.
237-
238-
:param number: int the base number.
239-
:param power: int the power to raise the base number to.
240-
:return: int - number raised to the specified power.
243+
"""Raise a number to an arbitrary power.
244+
245+
Parameters:
246+
number (int): The base number.
247+
power (int): The power to raise the base number to.
248+
249+
Returns:
250+
int: The number raised to the specified power.
251+
252+
Takes a number and raises it to the specified power, returning the result.
241253
242-
Takes a number and raises it to the specified power, returning the result.
243-
"""
254+
"""
244255

245-
return number ** power
256+
return number ** power
246257
...
247258

248259
# Calling the .__doc__ attribute of the function and printing the result.
249260
>>> print(raise_to_power.__doc__)
250261
Raise a number to an arbitrary power.
251262

252-
:param number: int the base number.
253-
:param power: int the power to raise the base number to.
254-
:return: int - number raised to the specified power.
263+
Parameters:
264+
number (int): The base number.
265+
power (int): The power to raise the base number to.
255266

256-
Takes a number and raises it to the specified power, returning the result.
267+
Returns:
268+
int: The number raised to the specified power.
269+
270+
Takes a number and raises it to the specified power, returning the result.
257271
```
258272

259273
[calls]: https://docs.python.org/3/reference/expressions.html#calls
@@ -271,4 +285,5 @@ Raise a number to an arbitrary power.
271285
[parameters]: https://docs.python.org/3/glossary.html#term-parameter
272286
[pep257]: https://www.python.org/dev/peps/pep-0257/
273287
[return]: https://docs.python.org/3/reference/simple_stmts.html#return
288+
[sphinx]: https://www.sphinx-doc.org/en/master/usage/index.html
274289
[type hints]: https://docs.python.org/3/library/typing.html

exercises/concept/guidos-gorgeous-lasagna/lasagna.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,18 @@
88
"""
99

1010

11-
#TODO: define your EXPECTED_BAKE_TIME (required) and PREPARATION_TIME (optional) constants below.
11+
#TODO (student): define your EXPECTED_BAKE_TIME (required) and PREPARATION_TIME (optional) constants below.
1212

1313

14-
#TODO: Remove 'pass' and complete the 'bake_time_remaining()' function below.
14+
#TODO (student): Remove 'pass' and complete the 'bake_time_remaining()' function below.
1515
def bake_time_remaining():
1616
"""Calculate the bake time remaining.
1717
18-
:param elapsed_bake_time: int - baking time already elapsed.
19-
:return: int - remaining bake time (in minutes) derived from 'EXPECTED_BAKE_TIME'.
18+
Parameters:
19+
elapsed_bake_time (int): The baking time already elapsed.
20+
21+
Returns:
22+
int: The remaining bake time (in minutes) derived from 'EXPECTED_BAKE_TIME'.
2023
2124
Function that takes the actual minutes the lasagna has been in the oven as
2225
an argument and returns how many minutes the lasagna still needs to bake
@@ -26,16 +29,16 @@ def bake_time_remaining():
2629
pass
2730

2831

29-
#TODO: Define the 'preparation_time_in_minutes()' function below.
32+
#TODO (student): Define the 'preparation_time_in_minutes()' function below.
3033
# To avoid the use of magic numbers (see: https://en.wikipedia.org/wiki/Magic_number_(programming)), you should define a PREPARATION_TIME constant.
3134
# You can do that on the line below the 'EXPECTED_BAKE_TIME' constant.
3235
# This will make it easier to do calculations, and make changes to your code.
3336

3437

3538

36-
#TODO: define the 'elapsed_time_in_minutes()' function below.
39+
#TODO (student): define the 'elapsed_time_in_minutes()' function below.
3740

3841

3942

40-
# TODO: Remember to go back and add docstrings to all your functions
43+
# TODO (student): Remember to go back and add docstrings to all your functions
4144
# (you can copy and then alter the one from bake_time_remaining.)

0 commit comments

Comments
 (0)