Skip to content

Commit 5c44e38

Browse files
committed
Sample code for the doctest article
1 parent 043e2a5 commit 5c44e38

File tree

16 files changed

+394
-0
lines changed

16 files changed

+394
-0
lines changed

python-doctest/README.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
# Python's doctest: Document and Test You Code at Once
2+
3+
This folder provides the code examples for the article [Python's doctest: Document and Test You Code at Once](https://realpython.com/python-doctest/).

python-doctest/broken_tests.txt

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
>>> # The output must be an integer
2+
>>> 5 + 7
3+
12.0
4+
5+
>>> # The output must not contain quotes
6+
>>> print("Hello, World!")
7+
'Hello, World!'
8+
9+
>>> # The output must not use double quotes
10+
>>> "Hello," + "World!"
11+
"Hello, World!"
12+
13+
>>> # The output must not contain leading or trailing spaces
14+
>>> print("Hello, World!")
15+
Hello, World!
16+
17+
>>> # The traceback doesn't include the correct exception message
18+
>>> raise ValueError("incorrect value")
19+
Traceback (most recent call last):
20+
ValueError: invalid value
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Functions to Perform Arithmetic Calculations
2+
3+
The `calculations.py` Python module provides basic arithmetic operations, including sum, subtraction, multiplication, and division.
4+
5+
Here are a few examples of how to use the functions in `calculations.py`:
6+
7+
```python
8+
>>> import calculations
9+
10+
>>> calculations.add(2, 2)
11+
4.0
12+
13+
>>> calculations.subtract(2, 2)
14+
0.0
15+
16+
>>> calculations.multiply(2, 2)
17+
4.0
18+
19+
>>> calculations.divide(2, 2)
20+
1.0
21+
22+
```
23+
24+
These examples show how to use the `calculations.py` module in your code.
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
"""Provide several sample math calculations.
2+
3+
This module allows the user to make mathematical calculations.
4+
5+
Module-level tests:
6+
>>> add(2, 4)
7+
6.0
8+
>>> subtract(5, 3)
9+
2.0
10+
>>> multiply(2.0, 4.0)
11+
8.0
12+
>>> divide(4.0, 2)
13+
2.0
14+
"""
15+
16+
17+
def add(a, b):
18+
"""Compute and return the sum of two numbers.
19+
20+
Tests for add():
21+
>>> add(4.0, 2.0)
22+
6.0
23+
>>> add(4, 2)
24+
6.0
25+
"""
26+
return float(a + b)
27+
28+
29+
def subtract(a, b):
30+
"""Calculate the difference of two numbers.
31+
32+
Tests for subtract():
33+
>>> subtract(4.0, 2.0)
34+
2.0
35+
>>> subtract(4, 2)
36+
2.0
37+
"""
38+
return float(a - b)
39+
40+
41+
def multiply(a, b):
42+
"""Compute and return the product of two numbers.
43+
44+
Tests for multiply():
45+
>>> multiply(4.0, 2.0)
46+
8.0
47+
>>> multiply(4, 2)
48+
8.0
49+
"""
50+
return float(a * b)
51+
52+
53+
def divide(a, b):
54+
"""Compute and return the quotient of two numbers.
55+
56+
Tests for divide():
57+
>>> divide(4.0, 2.0)
58+
2.0
59+
>>> divide(4, 2)
60+
2.0
61+
>>> divide(4, 0)
62+
Traceback (most recent call last):
63+
ZeroDivisionError: division by zero
64+
"""
65+
return float(a / b)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import doctest
2+
import unittest
3+
4+
import calculations
5+
6+
7+
def load_tests(loader, tests, ignore):
8+
tests.addTests(doctest.DocFileSuite("tests_file.txt"))
9+
tests.addTests(doctest.DocTestSuite(calculations))
10+
return tests
11+
12+
13+
if __name__ == '__main__':
14+
unittest.main()
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
>>> import calculations
2+
3+
>>> calculations.add(2, 2)
4+
4.0
5+
6+
>>> calculations.subtract(2, 2)
7+
0.0
8+
9+
>>> calculations.multiply(2, 2)
10+
4.0
11+
12+
>>> calculations.divide(2, 2)
13+
1.0

python-doctest/fizz_buzz.py

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
# Replace numbers that are divisible by 3 with "fizz"
2+
def fizz_buzz(numbers):
3+
"""Implement the Fizz buzz game.
4+
5+
>>> fizz_buzz([3, 6, 9, 12])
6+
['fizz', 'fizz', 'fizz', 'fizz']
7+
"""
8+
9+
# # Replace numbers that are divisible by 3 with "fizz"
10+
# def fizz_buzz(numbers):
11+
# """Implement the Fizz buzz game.
12+
13+
# >>> fizz_buzz([3, 6, 9, 12])
14+
# ['fizz', 'fizz', 'fizz', 'fizz']
15+
# """
16+
# result = []
17+
# for number in numbers:
18+
# if number % 3 == 0:
19+
# result.append("fizz")
20+
# else:
21+
# result.append(number)
22+
# return result
23+
24+
25+
# Replace numbers that are divisible by 5 with "buzz"
26+
# def fizz_buzz(numbers):
27+
# """Implement the Fizz buzz game.
28+
29+
# >>> fizz_buzz([3, 6, 9, 12])
30+
# ['fizz', 'fizz', 'fizz', 'fizz']
31+
32+
# >>> fizz_buzz([5, 10, 20, 25])
33+
# ['buzz', 'buzz', 'buzz', 'buzz']
34+
# """
35+
# result = []
36+
# for number in numbers:
37+
# if number % 3 == 0:
38+
# result.append("fizz")
39+
# elif number % 5 == 0:
40+
# result.append("buzz")
41+
# else:
42+
# result.append(number)
43+
# return result
44+
45+
46+
# Replace numbers that are divisible by 3 and 5 with "fizz buzz"
47+
# def fizz_buzz(numbers):
48+
# """Implement the Fizz buzz game.
49+
50+
# >>> fizz_buzz([3, 6, 9, 12])
51+
# ['fizz', 'fizz', 'fizz', 'fizz']
52+
53+
# >>> fizz_buzz([5, 10, 20, 25])
54+
# ['buzz', 'buzz', 'buzz', 'buzz']
55+
56+
# >>> fizz_buzz([15, 30, 45])
57+
# ['fizz buzz', 'fizz buzz', 'fizz buzz']
58+
59+
# >>> fizz_buzz([3, 6, 5, 10, 15, 30])
60+
# ['fizz', 'fizz', 'buzz', 'buzz', 'fizz buzz', 'fizz buzz']
61+
# """
62+
# result = []
63+
# for number in numbers:
64+
# if number % 15 == 0:
65+
# result.append("fizz buzz")
66+
# elif number % 3 == 0:
67+
# result.append("fizz")
68+
# elif number % 5 == 0:
69+
# result.append("buzz")
70+
# else:
71+
# result.append(number)
72+
# return result

python-doctest/greet.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
# def greet(name="World"):
2+
# """Print a greeting.
3+
4+
# Usage examples:
5+
# >>> greet("Pythonista")
6+
# Hello, Pythonista!
7+
# <BLANKLINE>
8+
# How have you been?
9+
# """
10+
# print(f"Hello, {name}!")
11+
# print()
12+
# print("How have you been?")
13+
14+
15+
# def greet(name="World"):
16+
# r"""Print a greeting.
17+
18+
# Usage examples:
19+
# >>> greet("Pythonista")
20+
# /== Hello, Pythonista! ==\
21+
# \== How have you been? ==/
22+
# """
23+
# print(f"/== Hello, {name}! ==\\")
24+
# print("\\== How have you been? ==/")
25+
26+
27+
def greet(name="World"):
28+
"""Print a greeting.
29+
30+
Usage examples:
31+
>>> greet("Pythonista")
32+
/== Hello, Pythonista! ==\\
33+
\\== How have you been? ==/
34+
"""
35+
print(f"/== Hello, {name}! ==\\")
36+
print("\\== How have you been? ==/")

python-doctest/identity.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
def get_id(obj):
2+
"""Return the identity of an object.
3+
4+
>>> get_id(1) # doctest: +ELLIPSIS
5+
...
6+
"""
7+
return id(obj)

python-doctest/options.txt

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
# options.txt
2+
3+
>>> 5 < 7
4+
True
5+
6+
>>> print("Hello, Pythonista! Welcome to Real Python!")
7+
Hello, ... Python!
8+
9+
>>> print("\tHello, World!")
10+
Hello, World!

0 commit comments

Comments
 (0)