Skip to content

Commit 837e7d1

Browse files
committed
Update calculator.py with documentation
1 parent 0f01cc7 commit 837e7d1

File tree

1 file changed

+85
-7
lines changed

1 file changed

+85
-7
lines changed

calculator/api/calculator.py

Lines changed: 85 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ def __init__(self):
1919
self.equation_type = ""
2020
self.current_part = "first"
2121

22+
"""Accepts a string input and calls the appropriate function on the calculator."""
2223
def input_button(self, button: str):
2324
if button.isdigit():
2425
self.add_digit(button)
@@ -36,6 +37,13 @@ def input_button(self, button: str):
3637
elif button == CLEAR_ENTRY:
3738
self.clear_entry()
3839

40+
"""Sets the current equation of the calculator.
41+
42+
>>> calc = Calculator()
43+
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
44+
>>> calc.get_equation()
45+
2 x 2
46+
"""
3947
def set_equation(self, first_part: str | float| int, operator: Operator = None, second_part: str | float | int = ""):
4048
self.equation_first = str(first_part)
4149
self.equation_type = operator or ""
@@ -45,6 +53,26 @@ def set_equation(self, first_part: str | float| int, operator: Operator = None,
4553
else:
4654
self.current_part = "last"
4755

56+
"""Returns the current equation as a single line string.
57+
58+
>>> calc = Calculator()
59+
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
60+
>>> calc.get_equation()
61+
2 x 2
62+
"""
63+
def get_equation(self):
64+
eq = " ".join(self.equation_first, self.equation_type, self.equation_last)
65+
eq = eq.rstrip()
66+
return eq
67+
68+
"""Removes the last entry from the calculator. This could be a digit or the current operator.
69+
70+
>>> calc = Calculator()
71+
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
72+
>>> calc.clear_entry()
73+
>>> calc.clear_entry()
74+
2
75+
"""
4876
def clear_entry(self):
4977
if self.current_part == "first":
5078
self.equation_first = self.equation_first[:-1]
@@ -55,19 +83,52 @@ def clear_entry(self):
5583
else:
5684
self.equation_last = self.equation_last[:-1]
5785

86+
"""Resets the state of the calculator. This is the same as calling self.set_equation('')."""
5887
def all_clear(self):
5988
self.set_equation("")
6089

61-
def add_digit(self, digit):
90+
"""Adds a digit to the current number. Accepts str and int.
91+
92+
>>> calc = Calculator()
93+
>>> calc.add_digit('2')
94+
>>> calc.add_digit(4)
95+
>>> calc.get_equation()
96+
24
97+
"""
98+
def add_digit(self, digit: str | int):
6299
if self.current_part == "first":
63-
self.equation_first += digit
100+
self.equation_first += str(digit)
64101
elif self.current_part == "last":
65-
self.equation_last += digit
102+
self.equation_last += str(digit)
66103

104+
"""Sets the operator of the equation. Also changes the current number to the second one.
105+
106+
>>> calc = Calculator()
107+
>>> calc.add_digit(2)
108+
>>> calc.set_operator(Operator.ADD)
109+
>>> calc.add_digit(4)
110+
>>> calc.set_operator(Operator.SUB)
111+
>>> calc.get_equation()
112+
2 - 4
113+
"""
67114
def set_operator(self, operator: Operator):
68115
self.equation_type = operator
69116
self.current_part = "last"
70117

118+
"""Flips the sign of the current number.
119+
120+
>>> calc = Calculator()
121+
>>> calc.add_digit(2)
122+
>>> calc.flip_sign()
123+
>>> calc.get_equation()
124+
-2
125+
126+
>>> calc = Calculator()
127+
>>> calc.set_equation(-2)
128+
>>> calc.flip_sign()
129+
>>> calc.get_equation()
130+
2
131+
"""
71132
def flip_sign(self):
72133
if self.current_part == "first":
73134
if self.equation_first.startswith("-"):
@@ -80,6 +141,15 @@ def flip_sign(self):
80141
else:
81142
self.equation_last = "-" + self.equation_last
82143

144+
"""Adds a decimal to the end of the current number if it doesn't have one. Otherwise it does nothing.
145+
146+
>>> calc = Calculator()
147+
>>> calc.add_digit(2)
148+
>>> calc.add_decimal()
149+
>>> calc.add_digit(5)
150+
>>> calc.get_equation()
151+
2.5
152+
"""
83153
def add_decimal(self):
84154
if self.current_part == "first":
85155
if not "." in self.equation_first:
@@ -88,16 +158,24 @@ def add_decimal(self):
88158
if not "." in self.equation_last:
89159
self.equation_last += "."
90160

161+
"""Solves the equation currently stored in the calculator and sets the equation to the new result.
162+
163+
>>> calc = Calculator()
164+
>>> calc.set_equation(1, Operator.ADD, 1)
165+
>>> calc.solve()
166+
>>> calc.get_equation()
167+
2
168+
"""
91169
def solve(self):
92170
result = float(self.equation_first)
93171

94-
if self.equation_type == "+":
172+
if self.equation_type == Operator.ADD:
95173
result += float(self.equation_last)
96-
elif self.equation_type == "-":
174+
elif self.equation_type == Operator.SUBTRACT:
97175
result -= float(self.equation_last)
98-
elif self.equation_type == "/":
176+
elif self.equation_type == Operator.DIVIDE:
99177
result /= float(self.equation_last)
100-
elif self.equation_type == "x":
178+
elif self.equation_type == Operator.MULTIPLY:
101179
result *= float(self.equation_last)
102180

103181
if result == int(result):

0 commit comments

Comments
 (0)