forked from Auston-Mtabane/python-calculator
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutils.py
More file actions
73 lines (56 loc) · 2.11 KB
/
utils.py
File metadata and controls
73 lines (56 loc) · 2.11 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
from pathlib import Path
"""Utilities: Calculator and History implementations.
Completed implementations for Calculator and History according to the tests.
"""
from typing import List
class Calculator():
"""
Rounding behavior:
- If rounding_digits == 0, integer-like results should be returned as int.
- Otherwise return a float rounded to rounding_digits.
"""
rounding_digits = 0
def __init__(self, round_digits: int = 0):
self.rounding_digits = int(round_digits)
def _round(self, value):
if self.rounding_digits == 0:
v = round(value, 0)
try:
if float(v).is_integer():
return int(v)
except Exception:
pass
return int(v)
else:
return round(value, self.rounding_digits)
def addtion(self, a, b):
return self._round(a + b)
def subtraction(self, a, b):
return self._round(a - b)
def multiplication(self, a, b):
return self._round(a * b)
def division(self, a, b):
return self._round(a / b)
def set_ound_digits(self, digits):
self.rounding_digits = int(digits)
class History():
"""Simple history backed by a text file.
- save(expression: str): appends the expression (with newline) to file.
- restore() -> list[str]: returns list of lines (as strings). If file doesn't exist or is empty, return [].
- clear(): empties the file.
"""
filepath_ = ""
def __init__(self, filepath: str):
self.filepath_ = filepath
def save(self, expression: str):
Path(self.filepath_).parent.mkdir(parents=True, exist_ok=True)
with open(self.filepath_, "a", encoding="utf-8") as f:
f.write(expression.rstrip() + "\n")
def restore(self) -> List[str]:
if not Path(self.filepath_).exists():
return []
with open(self.filepath_, "r", encoding="utf-8") as f:
lines = [line.rstrip("\n") for line in f.readlines() if line.strip() != ""]
return lines
def clear(self):
open(self.filepath_, "w", encoding="utf-8").close()