1+ from re import match , split
2+ from numexpr import evaluate
3+ from views .layouts .display_layout import DisplayLayout
4+
5+ class DisplayUtils ():
6+ def __init__ (self , displayLayout : DisplayLayout ):
7+ super ().__init__ ()
8+
9+ self .equationDisplay = displayLayout .equationDisplay
10+ self .resultDisplay = displayLayout .resultDisplay
11+
12+ def insert (self , value : str ):
13+ if value == "." :
14+ if "." not in split (r"\+|-|x|/|%" , self .resultDisplay .text ())[- 1 ]:
15+ text = self .resultDisplay .text ()
16+ self .resultDisplay .setText (text + value )
17+ elif bool (match (r"\+|-|x|/|%" , value )):
18+ if not bool (match (r"\+|-|x|/|%|\." , self .resultDisplay .text ()[- 1 ])):
19+ text = self .resultDisplay .text ()
20+ self .resultDisplay .setText (text + value )
21+ else :
22+ text = self .resultDisplay .text ()
23+ self .resultDisplay .setText (text + value )
24+
25+ def delete (self ):
26+ self .resultDisplay .setText (self .resultDisplay .text ()[:- 1 ])
27+
28+ def clear (self ):
29+ self .equationDisplay .setText ("" )
30+ self .resultDisplay .setText ("" )
31+
32+ def calculate (self ):
33+ if not bool (match (r"\+|-|x|/|%|\." , self .resultDisplay .text ()[- 1 ])):
34+ equation = self .resultDisplay .text ().replace ("x" , "*" )
35+ result = int (evaluate (equation ).item ()) if float (evaluate (equation ).item ()).is_integer () else float (evaluate (equation ).item ())
36+
37+ self .equationDisplay .setText (self .resultDisplay .text ())
38+ self .resultDisplay .setText (str (result ))
0 commit comments