-
Notifications
You must be signed in to change notification settings - Fork 0
/
model.py
48 lines (36 loc) · 1.21 KB
/
model.py
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
class Model:
def __init__(self):
"""
:return:
"""
self.previous_value = ''
self.value = ''
self.operator = ''
def calculate(self, caption):
if caption == 'C':
self.previous_value = ''
self.value = ''
self.operator = ''
elif caption == '+/-':
self.value = self.value[1:] if self.value[0] == '-' else '-' + self.value
elif caption == "%":
value = float(self.value) if '.' in self.value else int(self.value)
self.value = str(value / 100)
elif caption == "=":
value = self._evaluate()
if '.0' in str(value):
value = int(value)
self.value = str(value)
elif caption == ".":
if caption not in self.value:
self.value += caption
elif isinstance(caption, int):
self.value += str(caption)
else:
if self.value:
self.operator = caption
self.previous_value = self.value
self.value = ''
return self.value
def _evaluate(self):
return eval(self.previous_value + self.operator + self.value)