-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.py
95 lines (73 loc) · 2.57 KB
/
helpers.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
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
import math
from tkinter import END, Button
def addTextToScreen(screen, text):
"""
Enables the screen to add text then locks it again to prevent user from typing manually
Parameters:
screen : tkinter.Entry screen to add character to
text : text to be added
"""
screen.config(state='normal')
elementsOnScreen = screen.get()
screen.insert(len(elementsOnScreen), str(text))
screen.config(state='disabled')
def screenClear(screen):
screen.config(state='normal')
screen.delete(0, END)
screen.config(state='disabled')
def errorCheck(screenText, operations):
"""
Checks if input makes sense to the current expression on screen
Parameters:
screenText : current Text on screen
operations : list of supported operations
Returns:
True : if text is valid
False : if there's a problem in the text
"""
# check if trying to enter operation as a first character
if len(screenText) == 0:
return False
# check if trying to enter two operations next to each other
# or at the end of text to evaluate
if screenText[-1] in operations:
return False
return True
def changeAnglesInTextToRadians(text):
"""
Since math library in python takes the angles as radians this function
locates angles in string and converts it to radians
:param text:
:return: text after converting angles to radians
"""
text = str(text)
text = findTrigFunctionAndReplaceAngle(text, "sin(")
text = findTrigFunctionAndReplaceAngle(text, "cos(")
text = findTrigFunctionAndReplaceAngle(text, "tan(")
return text
def findTrigFunctionAndReplaceAngle(mainText, textToFind):
"""
Helper function for changeAnglesInTextToRadians
:param textToFind: sin cos or tan
:return: text after converting angle
"""
index = mainText.find(textToFind)
# if not found return
if index == -1:
return mainText
index += len(textToFind)
angleString = ""
numbers = ['1', '2', '3', '4', '5', '6', '7', '8', '9', '0']
for character in mainText[index:]:
if character == ')':
break
elif character in numbers: # make sure the character is a number
angleString += character
else: # if invalid character found return
return mainText
if angleString == "":
return mainText
angle = float(angleString)
angle = angle * (math.pi / 180)
mainText = mainText.replace(angleString, str(angle))
return mainText