-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcalculator.py
More file actions
73 lines (54 loc) · 1.69 KB
/
calculator.py
File metadata and controls
73 lines (54 loc) · 1.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
class Calculator:
"""A simple calculator class.
>>> calc = Calculator()
>>> calc.add(2, 3)
5
>>> calc.subtract(5, 2)
3
>>> calc.multiply(4, 3)
12
>>> calc.divide(5, 0) # Test division by zero exception
Traceback (most recent call last):
...
ValueError: Cannot divide by zero.
>>> calc.divide(10, 2)
5.0
### Testing parts we can't predict
>>> import random
>>> a, b = 1 + random.random(), 2
>>> calc.add(a, b)
3...
### Storytelling:
1. Let's prepare some data to use with the calculator. Use for that decimals
>>> import decimal
>>> numbers = [decimal.Decimal("10.0"), decimal.Decimal("20.0")]
2. Create a calculator instance
>>> calc = Calculator()
3. Modify numbers for some not very clear reason
>>> numbers = [2 * n for n in numbers]
3. Use the calculator to add the numbers together
>>> float(calc.add(*numbers))
60.0
4. But remember that the calculator is not perfect
>>> numbers = [1, 2, 3]
>>> calc.add(*numbers)
Traceback (most recent call last):
...
TypeError: ...add() takes 3 positional arguments but 4 were given
And that's how we can use `Calculator` and write some story with doctest at the same time
"""
def add(self, a, b):
"""Add two numbers."""
return a + b
def subtract(self, a, b):
"""Subtract two numbers."""
return a - b
def multiply(self, a, b):
"""Multiply two numbers."""
return a * b
def divide(self, a, b):
"""Divide two numbers."""
if b == 0:
err_msg = "Cannot divide by zero."
raise ValueError(err_msg)
return a / b