-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathcamper_program.py
More file actions
396 lines (339 loc) · 12.9 KB
/
camper_program.py
File metadata and controls
396 lines (339 loc) · 12.9 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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
PyCamp: a collaborative project
How to test this program:
.. code:: bash
python ./camper_program.py -t
python -m pip install pytest-cov
python ./camper_program.py --coverage
TODO:
- [ ] TST: Get as close as possible || reasonable to 100% coverage
"""
import math
import unittest
from fractions import Fraction
from io import StringIO
from unittest.mock import patch
class Calculator:
# These methods don't need any local state stored on the class
# so we mark them with the @staticmethod decorator
# and omit the 'self' parameter that instance methods would receive:
@staticmethod
def squares(num):
return num ** 2
@staticmethod
def square_roots(num):
return math.sqrt(num)
@staticmethod
def any_power(num, exp):
return pow(num, exp)
@staticmethod
def evalFraction(fracstr):
if "/" in fracstr:
spl = fracstr.split("/")
return float(spl[0]) / float(spl[1])
else:
return fracstr
@staticmethod
def isFrac(fracstr):
if "/" in fracstr:
return True
else:
return False
class CalculatorREPL:
def __init__(self, calculator=None):
# Parametrize so that we can specify
# a different calculator implementation:
self.calc = Calculator if calculator is None else calculator
def squares(self):
print("Choose a number 1 through 100.")
number = int(input())
square = self.calc.squares(number)
print("The square of", number, "is", square)
def square_roots(self):
print("Choose a number 1 through 100.")
num = int(input())
root = self.calc.square_roots(num)
print("The square root of", num, "is", root)
def any_power(self):
num = float(input("Choose a number > "))
exp = input("Raise to what power? > ")
if "/" in exp:
exp = float(Fraction(exp))
else:
exp = float(exp)
print(self.calc.any_power(num, exp))
def isFrac(self, f):
isfrac = self.calc.isFrac(f)
if isfrac:
print(f + " is a fraction")
else:
print(f + " is not a fraction")
return isfrac
def get_decimal(self):
print("We are here")
The_answer = input(
'Would you like to find the decimal value of a fraction type "1" >>> '
)
if The_answer != "1":
return
print("Type a fraction like _/_ ")
validfrac = False
while not validfrac:
fraction = input("What is your fraction >>> ")
validfrac = self.isFrac(fraction)
print("Your fraction = ", self.calc.evalFraction(fraction))
def get_menu_options(self):
# this could come in handy
return {
"1": ["Squares", self.squares],
"2": ["Square roots", self.square_roots],
"3": ["Raise to any power", self.any_power],
"4": ["Get decimal", self.get_decimal],
"0": ["Exit", None],
}
def run_loop(self):
menu_options = self.get_menu_options()
looping = True
while looping:
for n, (menustr, _) in menu_options.items():
print(f" {n}. {menustr}")
print("\n Pick one please")
try:
do_it = input(" >>> ")
# print("You picked", do_it)
if do_it not in menu_options:
print(
"Please pick one of "
f"{{ {' '.join(menu_options.keys())} }}"
)
continue
option_chosen = menu_options.get(do_it)
if option_chosen is None:
print(f"{do_it} is not a recognized option")
continue
menustr, calculator_function = option_chosen
if calculator_function is None:
looping = False
else:
try:
calculator_function()
except ValueError as e:
print("Error: %r" % e)
continue
except KeyboardInterrupt:
print("\nReceived Ctrl-C. Returning to menu.")
continue
except EOFError:
print("\nReceived EOF (Ctrl-D). Returning to menu.")
continue
except KeyboardInterrupt:
print("\nReceived Ctrl-C. Exiting.")
return
except EOFError:
print("\nReceived EOF (Ctrl-D). Exiting.")
return
class TestCalculator(unittest.TestCase):
def setUp(self):
self.calc = Calculator
def test_squares(self):
squares = self.calc.squares
# pytest has good error messages for 'assert ...' expressions
# unittest has good error messages for self.assertEqual exprs
assert squares(2) == 4
self.assertEqual(squares(2), 4)
self.assertEqual(squares(0), 0)
self.assertEqual(squares(1), 1)
self.assertEqual(squares(-1), 1)
def test_square_roots(self):
square_roots = self.calc.square_roots
self.assertEqual(square_roots(0), 0)
self.assertEqual(square_roots(1), 1)
self.assertEqual(square_roots(4), 2)
with self.assertRaises(ValueError):
self.assertEqual(square_roots(-1), 1j)
def test_any_power(self):
any_power = self.calc.any_power
self.assertEqual(any_power(0, 0), 1)
self.assertEqual(any_power(10, 0), 1)
self.assertEqual(any_power(-1, 0), 1)
self.assertEqual(any_power(-1, 2), 1)
self.assertEqual(any_power(4, 0.5), 2)
self.assertEqual(any_power(1.2, 2), 1.44)
self.assertEqual(any_power(1j, 2), -1 + 0j)
# with self.assertRaises(AssertionError):
# self.assertEqual(any_power(-1, 1/2), 0+1j)
def test_evalFraction(self):
evalf = self.calc.evalFraction
self.assertEqual(evalf("0/1"), 0)
self.assertEqual(evalf("1/1"), 1)
self.assertEqual(evalf("1/2"), 1 / 2)
self.assertEqual(evalf("2/3"), 2 / 3)
with self.assertRaises(ZeroDivisionError):
evalf("1/0")
self.assertEqual(evalf("0"), "0")
with self.assertRaises(TypeError):
self.assertEqual(evalf(0), 0)
def test_isFrac(self):
isfrac = self.calc.isFrac
self.assertTrue(isfrac("1/2"))
self.assertTrue(isfrac("1/2/3")) # TODO
self.assertFalse(isfrac("0"))
self.assertFalse(isfrac("0.1"))
self.assertFalse(isfrac("1.0"))
class TestCalculatorREPL(unittest.TestCase):
def setUp(self):
self.calc = CalculatorREPL()
self.stdout = patch("sys.stdout", new_callable=StringIO())
def test_init(self):
repl = CalculatorREPL()
self.assertTrue(repl)
self.assertEqual(repl.calc, Calculator)
repl = CalculatorREPL(calculator=dict)
self.assertTrue(repl)
self.assertEqual(repl.calc, dict)
@patch("sys.stdin", StringIO("1"))
@patch("sys.stdout", new_callable=StringIO)
def test_squares_1(self, stdout):
self.calc.squares()
stdout.seek(0)
self.assertEqual(next(stdout), "Choose a number 1 through 100.\n")
self.assertEqual(next(stdout), "The square of 1 is 1\n")
@patch("sys.stdin", StringIO("101"))
@patch("sys.stdout", new_callable=StringIO)
def test_squares_101(self, stdout):
self.calc.squares()
stdout.seek(0)
self.assertEqual(next(stdout), "Choose a number 1 through 100.\n")
self.assertEqual(next(stdout), "The square of 101 is 10201\n")
@patch("sys.stdin", StringIO("0"))
@patch("sys.stdout", new_callable=StringIO)
def test_square_roots_1(self, stdout):
self.calc.square_roots()
stdout.seek(0)
self.assertEqual(next(stdout), "Choose a number 1 through 100.\n")
self.assertEqual(next(stdout), "The square root of 0 is 0.0\n")
@patch("sys.stdin", StringIO("10201"))
@patch("sys.stdout", new_callable=StringIO)
def test_square_roots_10201(self, stdout):
self.calc.square_roots()
stdout.seek(0)
self.assertEqual(next(stdout), "Choose a number 1 through 100.\n")
self.assertEqual(next(stdout), "The square root of 10201 is 101.0\n")
@patch("sys.stdin", StringIO("-1"))
@patch("sys.stdout", new_callable=StringIO)
def test_square_roots_negative1(self, stdout):
with self.assertRaises(ValueError):
self.calc.square_roots()
@patch("sys.stdin", StringIO("10\n0"))
@patch("sys.stdout", new_callable=StringIO)
def test_any_power_10_0(self, stdout):
self.calc.any_power()
stdout.seek(0)
self.assertEqual(
next(stdout), "Choose a number > Raise to what power? > 1.0\n"
)
@patch("sys.stdout", new_callable=StringIO)
def test_isFrac(self, stdout):
output = self.calc.isFrac("1/1")
self.assertTrue(output)
stdout.seek(0)
self.assertEqual(next(stdout), "1/1 is a fraction\n")
@patch("sys.stdout", new_callable=StringIO)
def test_isFrac(self, stdout):
output = self.calc.isFrac("0")
self.assertFalse(output)
stdout.seek(0)
self.assertEqual(next(stdout), "0 is not a fraction\n")
@patch("sys.stdin", StringIO("0"))
@patch("sys.stdout", new_callable=StringIO)
def test_get_decimal__not_1(self, stdout):
self.calc.get_decimal()
stdout.seek(0)
next(stdout) # "We are here"
self.assertEqual(
next(stdout),
"Would you like to find the decimal value of a fraction "
'type "1" >>> ',
)
with self.assertRaises(StopIteration):
next(stdout)
@patch("sys.stdin", StringIO("1\n1/2"))
@patch("sys.stdout", new_callable=StringIO)
def test_get_decimal__1_2(self, stdout):
self.calc.get_decimal()
stdout.seek(0)
next(stdout) # "We are here"
self.assertEqual(
next(stdout),
"Would you like to find the decimal value of a fraction "
'type "1" >>> Type a fraction like _/_ \n',
)
self.assertEqual(
next(stdout), "What is your fraction >>> 1/2 is a fraction\n"
)
def test_get_menu_options(self):
output = self.calc.get_menu_options()
self.assertIsInstance(output, dict)
self.assertIn("0", output)
self.assertEqual(output["0"], ["Exit", None])
@patch("sys.stdin", StringIO("0"))
@patch("sys.stdout", new_callable=StringIO)
def test_run_loop_0(self, stdout):
output = self.calc.run_loop()
self.assertEqual(output, None)
stdout.seek(0)
lines = list(stdout)
self.assertRegex(lines[-2], "Pick one please\n$")
self.assertRegex(lines[-1], ">>> $")
# raise Exception(list(enumerate(lines)))
@patch("sys.stdin", StringIO("1\n1"))
@patch("sys.stdout", new_callable=StringIO)
def test_run_loop_0(self, stdout):
output = self.calc.run_loop()
menulen = len(self.calc.get_menu_options()) + 2
self.assertEqual(output, None)
stdout.seek(0)
lines = list(stdout)[menulen:]
self.assertRegex(lines[0], "Choose a number 1 through 100.\n$")
self.assertEqual(lines[1], "The square of 1 is 1\n")
self.assertEqual(lines[-1], "Received EOF (Ctrl-D). Exiting.\n")
# raise Exception(list(enumerate(lines)))
@patch("sys.stdin", StringIO("-1"))
@patch("sys.stdout", new_callable=StringIO)
def test_run_loop_invalid_option(self, stdout):
output = self.calc.run_loop()
menulen = len(self.calc.get_menu_options()) + 2
self.assertEqual(output, None)
stdout.seek(0)
lines = list(stdout)[menulen:]
self.assertRegex(lines[0], ">>> Please pick one of (.*)\n$")
self.assertEqual(lines[-1], "Received EOF (Ctrl-D). Exiting.\n")
if __name__ == "__main__":
import os
import sys
import subprocess
if "-t" in sys.argv[1:]:
sys.argv.remove("-t")
sys.argv.append("-v")
unittest.main()
elif "--pytest" in sys.argv[1:] or "--coverage" in sys.argv[1:]:
# You can install pytest and pytest-cov and coverage.py by running:
# python -m pip install pytest-cov
cmd = ["pytest", "-v"]
try:
sys.argv.remove("--pytest")
except ValueError:
pass
try:
sys.argv.remove("--coverage")
modname = os.path.splitext(os.path.basename(__file__))[0]
cmd += [f"--cov={modname}", "--cov-report=term-missing"]
except ValueError:
pass
cmd = cmd + [__file__] + sys.argv[1:]
print(f"+ {' '.join(cmd)}")
subprocess.call(cmd)
else:
CalculatorREPL().run_loop()