-
Notifications
You must be signed in to change notification settings - Fork 0
/
compute_services.py
64 lines (53 loc) · 2.61 KB
/
compute_services.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
# ================================================
# Compute Services
# ================================================
from typing import Optional, Tuple, Union, Dict, Callable
from calculator_domain import (Expression, Value, Operator, Parenthesis, Function, Compound, CalculatorInput, Number, CalculatorMathOp,
ErrorStateData, StartStateData, NumberInputStateData,
OperatorInputStateData, ResultStateData, ParenthesisOpenStateData, FunctionInputStateData)
import math
class ComputeServices:
def __init__(self):
super().__init__()
self.digit_display = " "
def receive_ten_key_display(self, display: str):
self.digit_display = display
print(f"Service received ten key display: {display}")
def get_digit_display(self):
out = self.digit_display
return out
def get_display_from_state(self, error_msg: str):
"""
Returns the display strings based on the current state of the computation.
"""
def inner(calculator_state) -> str:
if isinstance(calculator_state, StartStateData):
return " "
elif isinstance(calculator_state, NumberInputStateData):
return calculator_state.expression_tree
elif isinstance(calculator_state, OperatorInputStateData):
return calculator_state.expression_tree
elif isinstance(calculator_state, ResultStateData):
return calculator_state.result
elif isinstance(calculator_state, ParenthesisOpenStateData):
return calculator_state.expression_tree
elif isinstance(calculator_state, FunctionInputStateData):
return calculator_state.expression_tree
elif isinstance(calculator_state, ErrorStateData):
return error_msg + calculator_state.math_error.value
return inner
"""
Returns the initial state of the calculator.
"""
initial_state = StartStateData()
"""
Returns a dictionary of charachter mappings to calculator inputs
"""
input_mapping = {
'+': (CalculatorInput.MATHOP, CalculatorMathOp.ADD),
'-': (CalculatorInput.MATHOP, CalculatorMathOp.SUBTRACT),
'*': (CalculatorInput.MATHOP, CalculatorMathOp.MULTIPLY),
'/': (CalculatorInput.MATHOP, CalculatorMathOp.DIVIDE),
'=': (CalculatorInput.EQUALS, None),
'√': (CalculatorInput.MATHOP, CalculatorMathOp.ROOT)
}