Skip to content

Commit b712fd0

Browse files
committed
Update calculator.py
Moved docstrings into functions. Oops!
1 parent f2799af commit b712fd0

File tree

1 file changed

+78
-68
lines changed

1 file changed

+78
-68
lines changed

calculator/api/calculator.py

Lines changed: 78 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@ 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."""
22+
2323
def input_button(self, button: str):
24+
"""Accepts a string input and calls the appropriate function on the calculator."""
2425
if button.isdigit():
2526
self.add_digit(button)
2627
elif button in set(item.value for item in Operator):
@@ -37,14 +38,15 @@ def input_button(self, button: str):
3738
elif button == CLEAR_ENTRY:
3839
self.clear_entry()
3940

40-
"""Sets the current equation of the calculator.
4141

42-
>>> calc = Calculator()
43-
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
44-
>>> calc.get_equation()
45-
2 x 2
46-
"""
4742
def set_equation(self, first_part: str | float| int, operator: Operator = None, second_part: str | float | int = ""):
43+
"""Sets the current equation of the calculator.
44+
45+
>>> calc = Calculator()
46+
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
47+
>>> calc.get_equation()
48+
2 x 2
49+
"""
4850
self.equation_first = str(first_part)
4951
self.equation_type = operator or ""
5052
self.equation_last = str(second_part)
@@ -53,27 +55,29 @@ def set_equation(self, first_part: str | float| int, operator: Operator = None,
5355
else:
5456
self.current_part = "last"
5557

56-
"""Returns the current equation as a single line string.
5758

58-
>>> calc = Calculator()
59-
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
60-
>>> calc.get_equation()
61-
2 x 2
62-
"""
6359
def get_equation(self):
60+
"""Returns the current equation as a single line string.
61+
62+
>>> calc = Calculator()
63+
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
64+
>>> calc.get_equation()
65+
2 x 2
66+
"""
6467
eq = " ".join(self.equation_first, self.equation_type, self.equation_last)
6568
eq = eq.rstrip()
6669
return eq
6770

68-
"""Removes the last entry from the calculator. This could be a digit or the current operator.
6971

70-
>>> calc = Calculator()
71-
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
72-
>>> calc.clear_entry()
73-
>>> calc.clear_entry()
74-
2
75-
"""
7672
def clear_entry(self):
73+
"""Removes the last entry from the calculator. This could be a digit or the current operator.
74+
75+
>>> calc = Calculator()
76+
>>> calc.set_equation(2, Operator.MULTIPLY, 2)
77+
>>> calc.clear_entry()
78+
>>> calc.clear_entry()
79+
2
80+
"""
7781
if self.current_part == "first":
7882
self.equation_first = self.equation_first[:-1]
7983
elif self.current_part == "last":
@@ -83,53 +87,57 @@ def clear_entry(self):
8387
else:
8488
self.equation_last = self.equation_last[:-1]
8589

86-
"""Resets the state of the calculator. This is the same as calling self.set_equation('')."""
90+
8791
def all_clear(self):
92+
"""Resets the state of the calculator. This is the same as calling self.set_equation('')."""
8893
self.set_equation("")
8994

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-
"""
95+
9896
def add_digit(self, digit: str | int):
97+
"""Adds a digit to the current number. Accepts str and int.
98+
99+
>>> calc = Calculator()
100+
>>> calc.add_digit('2')
101+
>>> calc.add_digit(4)
102+
>>> calc.get_equation()
103+
24
104+
"""
99105
if self.current_part == "first":
100106
self.equation_first += str(digit)
101107
elif self.current_part == "last":
102108
self.equation_last += str(digit)
103109

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-
"""
110+
114111
def set_operator(self, operator: Operator):
112+
"""Sets the operator of the equation. Also changes the current number to the second one.
113+
114+
>>> calc = Calculator()
115+
>>> calc.add_digit(2)
116+
>>> calc.set_operator(Operator.ADD)
117+
>>> calc.add_digit(4)
118+
>>> calc.set_operator(Operator.SUB)
119+
>>> calc.get_equation()
120+
2 - 4
121+
"""
115122
self.equation_type = operator
116123
self.current_part = "last"
117124

118-
"""Flips the sign of the current number.
119125

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-
"""
132126
def flip_sign(self):
127+
"""Flips the sign of the current number.
128+
129+
>>> calc = Calculator()
130+
>>> calc.add_digit(2)
131+
>>> calc.flip_sign()
132+
>>> calc.get_equation()
133+
-2
134+
135+
>>> calc = Calculator()
136+
>>> calc.set_equation(-2)
137+
>>> calc.flip_sign()
138+
>>> calc.get_equation()
139+
2
140+
"""
133141
if self.current_part == "first":
134142
if self.equation_first.startswith("-"):
135143
self.equation_first = self.equation_first.removeprefix("-")
@@ -141,32 +149,34 @@ def flip_sign(self):
141149
else:
142150
self.equation_last = "-" + self.equation_last
143151

144-
"""Adds a decimal to the end of the current number if it doesn't have one. Otherwise it does nothing.
145152

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-
"""
153153
def add_decimal(self):
154+
"""Adds a decimal to the end of the current number if it doesn't have one. Otherwise it does nothing.
155+
156+
>>> calc = Calculator()
157+
>>> calc.add_digit(2)
158+
>>> calc.add_decimal()
159+
>>> calc.add_digit(5)
160+
>>> calc.get_equation()
161+
2.5
162+
"""
154163
if self.current_part == "first":
155164
if not "." in self.equation_first:
156165
self.equation_first += "."
157166
if self.current_part == "last":
158167
if not "." in self.equation_last:
159168
self.equation_last += "."
160169

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-
"""
170+
169171
def solve(self):
172+
"""Solves the equation currently stored in the calculator and sets the equation to the new result.
173+
174+
>>> calc = Calculator()
175+
>>> calc.set_equation(1, Operator.ADD, 1)
176+
>>> calc.solve()
177+
>>> calc.get_equation()
178+
2
179+
"""
170180
result = float(self.equation_first)
171181

172182
if self.equation_type == Operator.ADD:

0 commit comments

Comments
 (0)