-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscicalc.py
110 lines (106 loc) · 4.52 KB
/
scicalc.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#!/usr/bin/env python3
import sys
import math
import warnings
import prompt_toolkit
import re
import os
from handlers import _argHandler
from handlers._fixUi import *
from handlers._mathConstants import *
from handlers._usrConstants import *
from handlers._validators import *
from src.mathFunctions import *
from handlers._usrGlobals import newGlobal
warnings.filterwarnings("ignore")
args = vars(_argHandler.parseAndReturnArgs())
getUsrConstants(args)
sys.set_int_max_str_digits(0) # allowing very large numbers
uiSession = prompt_toolkit.PromptSession()
clear = lambda: os.system("cls" if os.name=="nt" else "clear")
while True:
try:
ui = uiSession.prompt(">")
ui = re.sub(r"\s{1,}", " ", ui)
exitAttempt = False
match ui:
case "" | None:
continue
case "clear":
clear()
case _:
fixedUi = fixUi(ui)
if fixedUi == None:
continue
if DEVELOPERS:
result = eval(fixedUi)
else:
result = eval(fixedUi, {"__builtins__": None}, newGlobal)
allowIsNumber = type(result).__name__ != "Decimal" and type(result).__name__ != "StringedNumber"
if EQUATION_RESULTS and type(result).__name__ != "StringedNumber":
print("%s = " % ui, end="")
if isNumber(result) and allowIsNumber:
if float(result).is_integer():
result = int(result)
addToHistory(ui, result)
if result >= 1e16 and result < sys.float_info.max:
print(f"{result:e}")
else:
print(f"{result:,}")
elif isinstance(result, (list, dict)):
addToHistory(ui, result, includeAll=True)
print(result)
elif isinstance(result, complex):
if result.imag == 1 and result.real == 0:
result = 'i'
elif result.real == 0 and result.imag != 0:
result = f"{int(result.imag)}i" if result.imag.is_integer() else f"{result.imag}i"
elif result.real != 0 and result.imag == 0:
result = float(result.real)
else:
result = f"{result.real} + {result.imag if not result.imag.is_integer() else int(result.imag)}i"
addToHistory(ui, result, includeAll=True, includeAllWithSymbols=True)
print(result)
elif isinstance(result, bool):
addToHistory(ui, result)
print(str(result).lower())
elif type(result).__name__ in ["module", "function"]:
print("{}: {}".format(type(result).__name__, result.__name__))
elif type(result).__name__ == "Decimal":
addToHistory(ui, str(result), includeAll=True)
print(result)
elif type(result).__name__ == "StringedNumber":
if result.addHistory:
addToHistory(ui, result.num, includeAll=True)
print(result.num)
else:
print(result.num)
elif result != None:
print(result)
except (KeyboardInterrupt, EOFError) as ERROR:
if type(ERROR).__name__ == "KeyboardInterrupt":
if exitAttempt:
raise SystemExit(0)
exitAttempt = True
print("Press ctrl+c again to confirm exit")
else:
raise SystemExit(0)
except Exception as ERROR:
ERROR_LINE = sys.exc_info()[-1].tb_lineno
ERROR_TYPE = type(ERROR).__name__
if DEVELOPERS:
print("Exception caught on line %d: %s: %s" % (ERROR_LINE, ERROR_TYPE, ERROR))
match ERROR_TYPE:
case "ZeroDivisionError":
print("undefined")
addToHistory(ui, "undefined", includeAll=True, includeAllWithSymbols=True)
case "OverflowError":
print("result too big")
addToHistory(ui, "result too big", includeAll=True, includeAllWithSymbols=True)
case "TypeError":
if str(ERROR) == "'NoneType' object is not subscriptable":
print("undefined function or variable")
continue
print(ERROR)
case _:
print(ERROR)