Skip to content

Commit 72baaaf

Browse files
committed
add example code for python docstrings tutorial
1 parent 863607f commit 72baaaf

23 files changed

+238
-0
lines changed

python-docstrings/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# How To Write Docstrings in Python
2+
3+
This folder provides the code examples for the Real Python tutorial [How To Write Docstrings in Python](https://realpython.com/how-to-write-docstrings-in-python/).
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class Potion:
2+
"""
3+
Represents a magical potion composed of various ingredients.
4+
5+
Attributes
6+
----------
7+
name : str
8+
The name of the potion.
9+
ingredients : list of str
10+
A list of ingredients used in the potion.
11+
potency : int
12+
The strength level of the potion.
13+
14+
Methods
15+
-------
16+
brew():
17+
Completes the potion and sets its potency.
18+
describe():
19+
Returns a human-readable summary of the potion.
20+
"""
21+
22+
def __init__(self, name, ingredients):
23+
self.name = name
24+
self.ingredients = ingredients
25+
self.potency = 0
26+
27+
def brew(self):
28+
"""Simulate brewing the potion by calculating potency."""
29+
self.potency = len(self.ingredients) * 10
30+
31+
def describe(self):
32+
"""Return a string describing the potion and its strength."""
33+
return f"{self.name} (Potency: {self.potency})"

python-docstrings/docstring_format.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
def determine_magic_level(magic_number):
2+
"""
3+
Multiply a wizard's favorite number by 3 to reveal their magic level.
4+
"""

python-docstrings/doctest_style.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def undo_spell(spell):
2+
"""
3+
Reverses characters in a spell incantation thereby undoing a spell.
4+
5+
Example:
6+
>>> undo_spell("Expelliarmus")
7+
"sumraillepxE"
8+
9+
>>> undo_spell("Lumos")
10+
"somuL"
11+
"""
12+
return spell[::1]
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def enchant_wand(wand_type, level=1):
2+
"""
3+
Enhance a wand with magical properties.
4+
5+
Args:
6+
wand_type (str): The type of wand to enchant.
7+
level (int, optional): The enchantment level. Defaults to 1.
8+
9+
Returns:
10+
str: A message confirming the enchantment.
11+
12+
Raises:
13+
ValueError: If the enchantment level is invalid.
14+
"""
15+
if level < 1:
16+
raise ValueError("Enchantment level must be at least 1.")
17+
return f"{wand_type.title()} enchanted to level {level}!"

python-docstrings/get_potter_books.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from books import get_harry_potter_books
2+
3+
print(get_harry_potter_books().__doc__)

python-docstrings/google_style.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def get_magic_items(user_id, include_potions=False):
2+
"""
3+
Retrieve a list of magical items for a specific user.
4+
5+
Args:
6+
user_id (int): The ID of the user whose items should be retrieved.
7+
include_potions (bool, optional): include a potions option.
8+
9+
Returns:
10+
list[str]: A list of item names associated with the user.
11+
"""
12+
return ["wand", "cloak", "crystal ball"]
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
def summon(spell, caster):
2+
"""Summons a creature.
3+
Parameters:
4+
spell - name of the spell
5+
caster: name of the caster
6+
Return:
7+
The summoned creature
8+
"""
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
def summon(spell, caster):
2+
"""
3+
Summons a creature using the given spell.
4+
5+
Args:
6+
spell (str): The name of the summoning spell.
7+
caster (str): The name of the person casting the spell.
8+
9+
Returns:
10+
str: The name of the summoned creature.
11+
"""
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
"""A magical module for adding and listing magical characters."""
2+
3+
4+
def add_characters(magical_being):
5+
"""Add a new magical character."""
6+
return f"You've added {magical_being} to the magical beings record"
7+
8+
9+
if __name__ == "__main__":
10+
print(add_characters("Gandalf"))

0 commit comments

Comments
 (0)