-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathx-calc.py
More file actions
1042 lines (865 loc) · 41.6 KB
/
x-calc.py
File metadata and controls
1042 lines (865 loc) · 41.6 KB
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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env python3
#[x-cmds]: UPDATE
"""Do advanced calculations from the command line.
Supports a wide range of mathematical operations, functions and constants.
There's no number size limit — the only limit is your system's memory."""
from typing import Callable, Optional, Any
from xulbux import FormatCodes, Console
from xulbux.regex import LazyRegex
import sympy
import numpy
import sys
import re
sys.set_int_max_str_digits(0) # 0 = NO LIMIT
ARGS = Console.get_args({
"calculation": "before",
"ans": {"-a", "--ans"},
"precision": {"-p", "--precision"},
"format": {"-f", "--format"},
"debug": {"-d", "--debug"},
"help": {"-h", "--help"},
})
DEBUG = ARGS.debug.exists
REGEX = LazyRegex(thousands_seps=r"(?<=\d)[_'](?=\d)")
def sanitize(expression: Any, /) -> sympy.Expr:
return sympy.sympify(expression) # type: ignore[return-type]
def clean_num(token: str, /) -> str:
"""Remove underscores from numeric tokens for proper parsing."""
if (no_seps_num := REGEX.thousands_seps.sub("", token)
).replace(".", "").replace("-", "").isdigit():
return no_seps_num
return token
class OPERATORS:
# ARITHMETIC OPERATORS
MINUS = ("o:minus", ["-", "−"])
PLUS = ("o:plus", ["+", "+"])
MULTIPLY = ("o:multiply", ["*", "×", "∗", "·"])
DIVIDE = ("o:divide", ["/", "÷"])
FLOOR_DIVIDE = ("o:floor_divide", ["//", "⌊/⌋"])
MODULO = ("o:modulo", ["%", "mod"])
POWER = ("o:power", ["**", "^"])
# LOGIC OPERATORS
AND = ("o:and", ["and", "&&", "∧"])
OR = ("o:or", ["or", "||", "∨"])
NOT = ("o:not", ["not", "!", "¬"])
XOR = ("o:xor", ["xor", "⊻"])
# POSTFIX OPERATORS
FACTORIAL = ("o:factorial", ["!"])
# COMPARISON OPERATORS
EQUALS = ("o:equals", ["eq", "=", "==", "≡"])
NOT_EQUALS = ("o:not_equals", ["ne", "!=", "≠", "<>"])
LESS_THAN = ("o:less_than", ["lt", "<", "<"])
LESS_THAN_EQUAL = ("o:less_equal_than", ["le", "<=", "≤", "⩽"])
GREATER_THAN = ("o:greater_than", ["gt", ">", ">"])
GREATER_THAN_EQUAL = ("o:greater_equal_than", ["ge", ">=", "≥", "⩾"])
ALL = (
MINUS, PLUS, MULTIPLY, DIVIDE, FLOOR_DIVIDE, MODULO, POWER, AND, OR, NOT, XOR, FACTORIAL, EQUALS, NOT_EQUALS,
LESS_THAN, LESS_THAN_EQUAL, GREATER_THAN, GREATER_THAN_EQUAL
)
ALL_TOKENS: tuple[str, ...] = tuple(token for _, tokens in ALL for token in tokens)
PRECEDENCE: dict[str | tuple[str, ...], int] = {
# HIGHER VALUES REPRESENT HIGHER PRECEDENCE
FACTORIAL[0]: 5,
POWER[0]: 4,
(MULTIPLY[0], DIVIDE[0], FLOOR_DIVIDE[0], MODULO[0]): 3,
(PLUS[0], MINUS[0]): 2,
AND[0]: 1,
OR[0]: 0,
(EQUALS[0], NOT_EQUALS[0], LESS_THAN[0], LESS_THAN_EQUAL[0], GREATER_THAN[0], GREATER_THAN_EQUAL[0]): -1,
NOT[0]: -2,
XOR[0]: -3,
}
IMPLEMENT: dict[str, Callable[[Any, Any], Any]] = {
# ARITHMETIC OPERATORS
MINUS[0]: lambda a, b: sympy.Add(sanitize(a), sympy.Mul(sanitize(b), sympy.Integer(-1))),
PLUS[0]: lambda a, b: sympy.Add(sanitize(a), sanitize(b)),
MULTIPLY[0]: lambda a, b: sympy.Mul(sanitize(a), sanitize(b)),
DIVIDE[0]: lambda a, b: sympy.Mul(sanitize(a), sympy.Pow(sanitize(b), -1)),
FLOOR_DIVIDE[0]: lambda a, b: sympy.floor(sympy.Mul(sanitize(a), sympy.Pow(sanitize(b), -1))),
MODULO[0]: lambda a, b: sympy.Mod(sanitize(a), sanitize(b)),
POWER[0]: lambda a, b: sympy.Pow(sanitize(a), sanitize(b)),
# LOGIC OPERATORS
AND[0]: lambda a, b: 1 if (bool(a) and bool(b)) else 0,
OR[0]: lambda a, b: 1 if (bool(a) or bool(b)) else 0,
NOT[0]: lambda a, _: 1 if not bool(a) else 0,
XOR[0]: lambda a, b: 1 if ((bool(a) and not bool(b)) or (not bool(a) and bool(b))) else 0,
# POSTFIX OPERATORS
FACTORIAL[0]: lambda a, _: sympy.factorial(sanitize(a)),
# COMPARISON OPERATORS
EQUALS[0]: lambda a, b: 1 if a == b else 0,
NOT_EQUALS[0]: lambda a, b: 1 if a != b else 0,
LESS_THAN[0]: lambda a, b: 1 if a < b else 0,
LESS_THAN_EQUAL[0]: lambda a, b: 1 if a <= b else 0,
GREATER_THAN[0]: lambda a, b: 1 if a > b else 0,
GREATER_THAN_EQUAL[0]: lambda a, b: 1 if a >= b else 0,
}
@classmethod
def get(cls, operator_id: str, /):
"""Get the operator function by operator ID."""
return cls.IMPLEMENT.get(operator_id)
@classmethod
def get_id(cls, token: str, /) -> str | None:
"""Get the operator ID for a token by searching through the token lists."""
token_lower = token.lower()
for op_id, symbols in cls.ALL:
if token_lower in symbols: return op_id
return None
@classmethod
def is_operator(cls, token: str, /) -> bool:
"""Check if a token is an operator by searching through the token lists."""
return cls.get_id(token) is not None
@classmethod
def get_precedence(cls, operator_id: str, /) -> int:
"""Get the operator precedence by operator ID."""
for keys, val in cls.PRECEDENCE.items():
if isinstance(keys, tuple):
if operator_id in keys: return val
else:
if operator_id == keys: return val
return 5 # DEFAULT
class CONSTANTS:
# MATHEMATICAL CONSTANTS
ANS = ("c:ans", ["ans", "answer"])
E = ("c:e", ["e", "euler"])
INF = ("c:inf", ["inf", "infinity", "∞"])
PI = ("c:pi", ["pi", "π"])
TAU = ("c:tau", ["tau", "τ"])
PHI = ("c:phi", ["phi", "φ", "golden"])
ALL = (ANS, E, INF, PI, TAU, PHI)
ALL_TOKENS: tuple[str, ...] = tuple(token for _, tokens in ALL for token in tokens)
IMPLEMENT: dict[str, object] = {
ANS[0]: (ARGS.ans.values or [None])[0],
E[0]: sympy.E,
INF[0]: sympy.oo,
PI[0]: sympy.pi,
TAU[0]: 2 * sympy.pi,
PHI[0]: sympy.GoldenRatio,
}
@classmethod
def get(cls, constant_id: str, /):
"""Get the constant function by constant ID."""
return cls.IMPLEMENT.get(constant_id)
@classmethod
def get_id(cls, token: str, /) -> str | None:
"""Get the constant ID for a token by searching through the token lists."""
token_lower = token.lower()
for const_id, symbols in cls.ALL:
if token_lower in symbols: return const_id
return None
@classmethod
def is_constant(cls, token: str, /) -> bool:
"""Check if a token is a constant by searching through the token lists."""
return cls.get_id(token) is not None
class FUNCTIONS:
# PROGRAMMING FUNCTIONS
ABS = ("f:abs", ["abs", "absolute", "magnitude"])
FLOOR = ("f:floor", ["floor"])
CEIL = ("f:ceil", ["ceil", "ceiling"])
ROUND = ("f:round", ["round"])
SIGN = ("f:sign", ["sign", "sgn"])
# LOGARITHMIC FUNCTIONS
LN = ("f:ln", ["ln", "log_e", "natural_log", "loge"])
LOG = ("f:log", ["log", "logarithm"])
LOGB = ("f:logb", ["logb", "log_base"])
LOG2 = ("f:log2", ["log2", "log_2"])
LOG10 = ("f:log10", ["log10"])
EXP = ("f:exp", ["exp", "exponential"])
# TRIGONOMETRIC FUNCTIONS
RAD = ("f:rad", ["rad", "radians", "to_radians"])
DEG = ("f:deg", ["deg", "degrees", "to_degrees"])
SIN = ("f:sin", ["sin", "sine"])
ASIN = ("f:asin", ["asin", "arcsin", "arcsine", "sin_inv"])
COS = ("f:cos", ["cos", "cosine"])
ACOS = ("f:acos", ["acos", "arccos", "arccosine", "cos_inv"])
TAN = ("f:tan", ["tan", "tangent"])
ATAN = ("f:atan", ["atan", "arctan", "arctangent", "tan_inv"])
# HYPERBOLIC FUNCTIONS
SINH = ("f:sinh", ["sinh", "hyperbolic_sine"])
COSH = ("f:cosh", ["cosh", "hyperbolic_cosine"])
TANH = ("f:tanh", ["tanh", "hyperbolic_tangent"])
ASINH = ("f:asinh", ["asinh", "arcsinh", "inverse_sinh"])
ACOSH = ("f:acosh", ["acosh", "arccosh", "inverse_cosh"])
ATANH = ("f:atanh", ["atanh", "arctanh", "inverse_tanh"])
# ADDITIONAL TRIGONOMETRIC FUNCTIONS
COT = ("f:cot", ["cot", "cotangent"])
SEC = ("f:sec", ["sec", "secant"])
CSC = ("f:csc", ["csc", "cosecant"])
# ADDITIONAL FUNCTIONS
FAC = ("f:fac", ["fac", "factorial", "fact"])
SQRT = ("f:sqrt", ["sqrt", "square_root", "√"])
CBRT = ("f:cbrt", ["cbrt", "cube_root", "∛"])
POW = ("f:pow", ["pow", "power"])
# STATISTICAL FUNCTIONS
MIN = ("f:min", ["min", "minimum"])
MAX = ("f:max", ["max", "maximum"])
ALL = (
ABS, FLOOR, CEIL, ROUND, SIGN, LN, LOG, LOGB, LOG2, LOG10, EXP, RAD, DEG,
SIN, ASIN, COS, ACOS, TAN, ATAN, SINH, COSH, TANH, ASINH, ACOSH, ATANH,
COT, SEC, CSC, FAC, SQRT, CBRT, POW, MIN, MAX
)
ALL_TOKENS: tuple[str, ...] = tuple(token for _, tokens in ALL for token in tokens)
IMPLEMENT: dict[str, Callable[[Any], Any]] = {
# PROGRAMMING FUNCTIONS
ABS[0]: lambda a: abs(sanitize(a)),
FLOOR[0]: lambda a: sympy.floor(sanitize(a)),
CEIL[0]: lambda a: sympy.ceiling(sanitize(a)),
ROUND[0]: lambda a: sympy.floor(sanitize(a) + sympy.Rational(1, 2)), # type: ignore[operator-unsupported]
SIGN[0]: lambda a: sympy.sign(sanitize(a)),
# LOGARITHMIC FUNCTIONS
LN[0]: lambda a: sympy.log(sanitize(a)),
LOG[0]: lambda a, b=None: sympy.log(sanitize(a), sanitize(b)) if b is not None else sympy.log(sanitize(a), 10),
LOGB[0]: lambda a, b=None: sympy.log(sanitize(a), sanitize(b)) if b is not None else sympy.log(sanitize(a)),
LOG2[0]: lambda a: sympy.log(sanitize(a), 2),
LOG10[0]: lambda a: sympy.log(sanitize(a), 10),
EXP[0]: lambda a: sympy.exp(sanitize(a)),
# TRIGONOMETRIC FUNCTIONS
RAD[0]: lambda a: sympy.rad(sanitize(a)), # type: ignore[partially-unknown]
DEG[0]: lambda a: sympy.deg(sanitize(a)), # type: ignore[partially-unknown]
SIN[0]: lambda a: sympy.sin(sanitize(a)),
ASIN[0]: lambda a: sympy.asin(sanitize(a)),
COS[0]: lambda a: sympy.cos(sanitize(a)),
ACOS[0]: lambda a: sympy.acos(sanitize(a)),
TAN[0]: lambda a: sympy.tan(sanitize(a)),
ATAN[0]: lambda a: sympy.atan(sanitize(a)),
# HYPERBOLIC FUNCTIONS
SINH[0]: lambda a: sympy.sinh(sanitize(a)),
COSH[0]: lambda a: sympy.cosh(sanitize(a)),
TANH[0]: lambda a: sympy.tanh(sanitize(a)),
ASINH[0]: lambda a: sympy.asinh(sanitize(a)),
ACOSH[0]: lambda a: sympy.acosh(sanitize(a)),
ATANH[0]: lambda a: sympy.atanh(sanitize(a)),
# ADDITIONAL TRIGONOMETRIC FUNCTIONS
COT[0]: lambda a: sympy.cot(sanitize(a)),
SEC[0]: lambda a: sympy.sec(sanitize(a)),
CSC[0]: lambda a: sympy.csc(sanitize(a)),
# ADDITIONAL FUNCTIONS
FAC[0]: lambda a: sympy.factorial(sanitize(a)),
SQRT[0]: lambda a: sympy.sqrt(sanitize(a)), # type: ignore[partially-unknown]
CBRT[0]: lambda a: sympy.Pow(sanitize(a), sympy.Rational(1, 3)),
POW[0]: lambda a, b=None: sympy.Pow(sanitize(a), sanitize(b)) if b is not None else sanitize(a),
# STATISTICAL FUNCTIONS
MIN[0]: lambda a, b=None: sympy.Min(sanitize(a), sanitize(b)) if b is not None else sanitize(a),
MAX[0]: lambda a, b=None: sympy.Max(sanitize(a), sanitize(b)) if b is not None else sanitize(a),
}
@classmethod
def get(cls, func_id: str, /):
"""Get the function lambda by function ID."""
return cls.IMPLEMENT.get(func_id)
@classmethod
def get_id(cls, token: str, /) -> str | None:
"""Get the function ID for a token by searching through the token lists."""
token_lower = token.lower()
for func_id, symbols in cls.ALL:
if token_lower in symbols: return func_id
return None
@classmethod
def is_function(cls, token: str, /) -> bool:
"""Check if a token is a function by searching through the token lists."""
return cls.get_id(token) is not None
PATTERN = re.compile(
"|".join(map(re.escape, sorted(OPERATORS.ALL_TOKENS + CONSTANTS.ALL_TOKENS + FUNCTIONS.ALL_TOKENS, key=len, reverse=True)))
+ r"|[a-z]+|" + "|".join(map(re.escape, OPERATORS.MINUS[1])) + r"\d+(?:[_']\d+)*\.\d+(?:[_']\d+)*|" + "|".join(map(re.escape, OPERATORS.MINUS[1]))
+ r"\d+(?:[_']\d+)*|" + r"\d+(?:[_']\d+)*\.\d+(?:[_']\d+)*|\d+(?:[_']\d+)*|" + r"\(|\)|,",
re.IGNORECASE
)
def print_help():
o_list = "\n".join(f"[i|dim]({o_id.split(":")[1]:<22}){'[dim](,) '.join(symbols)}" for o_id, symbols in OPERATORS.ALL)
c_list = "\n".join(f"[i|dim]({c_id.split(":")[1]:<22}){'[dim](,) '.join(symbols)}" for c_id, symbols in sorted(CONSTANTS.ALL))
f_list = "\n".join(f"[i|dim]({f_id.split(":")[1]:<22}){'[dim](,) '.join(symbols)}" for f_id, symbols in sorted(FUNCTIONS.ALL))
help_text = f"""\
[b|in|bg:black]( Advanced Calculator — Perform complex calculations directly from the command line )
[b](Usage:) [br:green](x-calc) [br:cyan](<calculation>) [br:blue]([options])
[b](Arguments:)
[br:cyan](calculation) The calculation string to evaluate
[b](Options:)
[br:blue](-a), [br:blue](--ans[dim](=)VALUE) Value to use for 'ans' constant
[br:blue](-p), [br:blue](--precision[dim](=)N) Number of decimal places to calculate [dim]((default: 100, -1 for infinite))
[br:blue](-f), [br:blue](--format) Format the output with thousands separators
[br:blue](-d), [br:blue](--debug) Show debug information during calculation
[b](Examples:)
[br:green](x-calc) [br:cyan]("2 + 2 * 2") [dim](# [i](Simple arithmetic))
[br:green](x-calc) [br:cyan]("ans * 2") [br:blue](--ans=6) [dim](# [i](Using the 'ans' constant))
[br:green](x-calc) [br:cyan]"sqrt(ln(10) + 1) / cos(π / 4)" [br:blue](-p=1000) [dim](# [i](High precision with functions and constants))
[b](Possible operators:)
{o_list}
[b](Possible constants:)
{c_list}
[b](Possible functions:)
{f_list}
"""
FormatCodes.print(help_text)
def print_overwrite(*values: object, sep: str = " ", end: str = "\n") -> None:
FormatCodes.print(f"\033[2K\r{sep.join(str(val) for val in values)}", end=end)
def print_line(
title: Optional[str] = None,
/, *,
char: str = "═",
width: int = Console.w,
end: str = "\n"
) -> None:
if not title:
FormatCodes.print(f"[dim]{char * width}[_dim]", end=end)
return
line = char * round((width / 2) - ((len(title) + 2) / 2))
final = FormatCodes.to_ansi(f"[dim]{line}[_dim] [b]{title}[_b] [dim]{line}")
final_len = len(FormatCodes.remove_ansi(final))
if not final_len == width:
if final_len > width:
final = final[:width + (len(final) - final_len)]
if final_len < width:
final = final + (width - final_len) * char
FormatCodes.print(f"{final}[_dim]", end=end)
def clear_lines(num_lines: int = 1) -> None:
for _ in range(num_lines):
print("\033[F\033[K", end="", flush=True)
class Calc:
def __init__(
self, /, *,
calc_str: str,
last_ans: Optional[str] = None,
precision: int = 110,
max_num_len: int = 100
):
self.calc_str = calc_str
self.last_ans = last_ans
self.precision = precision
self.max_num_len = max_num_len
self.inf_precision = (precision == -1)
def __str__(self) -> str:
return self.calc_str
def __repr__(self) -> str:
return f"Calc(calc_str={self.calc_str!r}, last_ans={self.last_ans!r}, precision={self.precision}, max_num_len={self.max_num_len})"
def eval(self) -> str:
if DEBUG:
clear_lines()
print()
print_line(f"NEW CALCULATION")
FormatCodes.print(f"[dim](raw calculation string:)\n[b|dim](>>>) {self.calc_str}")
else:
print_overwrite("[dim|white](calculating...)", end="")
# SKIP PRECISION ADJUSTMENTS FOR INFINITE PRECISION (-1)
if not self.inf_precision and self.precision <= self.max_num_len:
self.max_num_len = self.precision
self.precision += 10
norm_calc_str = re.sub(r"\s+", "", self.calc_str.strip())
if DEBUG:
FormatCodes.print(f"[dim](normalized calculation string:)\n[b|dim](>>>) {norm_calc_str}")
FormatCodes.print(f"[dim](precision:) {self.precision}")
FormatCodes.print(f"[dim](max number length:) {self.max_num_len}")
self.last_ans = self._perform_eval(norm_calc_str)
return self.format_readability(self.last_ans)
def format_result(self, result: object, /) -> str:
if DEBUG:
print_line("FORMAT RESULT")
FormatCodes.print(f"[dim](result:) {result}")
FormatCodes.print(f"[dim](precision:) {self.precision} [dim]/(infinite:[_dim] {self.inf_precision}[dim])[_dim]")
# FOR INFINITE PRECISION, JUST CONVERT TO STRING WITHOUT FORMATTING
if self.inf_precision:
result_str = str(result)
if DEBUG:
FormatCodes.print(f"[dim](infinite precision result:) {result_str}")
return result_str
# CHECK IF RESULT IS AN EXACT INTEGER TO AVOID FLOAT PRECISION ERRORS
is_exact_integer = False
try:
if hasattr(result, 'is_integer') and getattr(result, 'is_integer', False):
is_exact_integer = True
elif isinstance(result, sympy.Integer):
is_exact_integer = True
elif hasattr(result, 'is_Integer') and getattr(result, 'is_Integer', False):
is_exact_integer = True
elif isinstance(result, int):
is_exact_integer = True
except:
pass
if is_exact_integer:
result_str = str(result)
if DEBUG:
FormatCodes.print(f"[dim](exact integer result (preserving for formatting):) {result_str}")
else:
try:
result_str = "{:.{}f}".format(result, self.precision)
result_str = (result_str.rstrip("0").rstrip(".") if "." in result_str else result_str)
except OverflowError:
result_str = str(result)
if DEBUG:
FormatCodes.print(f"[dim](formatted decimal result:) {result_str}")
return result_str
def format_readability(self, num_str: str, /) -> str:
if not DEBUG:
print_overwrite("[dim|white](formatting...)", end="")
# FORMAT WITH THOUSANDS SEPARATORS IF REQUESTED
if ARGS.format.exists:
if DEBUG:
print_line("FORMATTING WITH SEPARATORS")
FormatCodes.print(f"[dim](should format:) {ARGS.format.exists}")
sep = ARGS.format.values[0] if ARGS.format.values else ","
if DEBUG:
FormatCodes.print(f"[dim](separator:) {sep}")
FormatCodes.print(f"[dim](input num_str:) {num_str}")
if "." in num_str:
int_part, decimal_part = num_str.split(".", 1)
if int_part.lstrip("-").isdigit() and len(int_part.lstrip("-")) > 3:
formatted_int = ""
sign = "-" if int_part.startswith("-") else ""
digits = int_part.lstrip("-")
for i, digit in enumerate(reversed(digits)):
if i > 0 and i % 3 == 0:
formatted_int = sep + formatted_int
formatted_int = digit + formatted_int
num_str = sign + formatted_int + "." + decimal_part
if DEBUG:
FormatCodes.print(f"[dim](formatted decimal number:) {num_str}")
else:
if num_str.lstrip("-").isdigit() and len(num_str.lstrip("-")) > 3:
formatted_num = ""
sign = "-" if num_str.startswith("-") else ""
digits = num_str.lstrip("-")
for i, digit in enumerate(reversed(digits)):
if i > 0 and i % 3 == 0:
formatted_num = sep + formatted_num
formatted_num = digit + formatted_num
num_str = sign + formatted_num
if DEBUG:
FormatCodes.print(f"[dim](formatted whole number:) {num_str}")
# TRUNCATE REPEATING DECIMAL (skip for infinite precision)
if not self.inf_precision and len(num_str) > self.max_num_len and "." in num_str:
num_str = num_str[:-10]
int_part, decimal_part = num_str.split(".")
short_decimal_part = decimal_part[:self.max_num_len]
if DEBUG:
print_line(f"TRUNCATING REPEATING DECIMAL")
FormatCodes.print(f"[dim](input string:) {num_str}")
FormatCodes.print(f"[dim](decimal part:) {short_decimal_part}")
if self._is_recurring(short_decimal_part):
num_str = f"{int_part}.{short_decimal_part}…"
else:
num_str = f"{int_part}.{short_decimal_part}"
if DEBUG:
FormatCodes.print(f"[dim](formatted string:) {num_str}")
# FORMAT LONG NUMBERS TO EXPONENTS (skip for infinite precision)
elif not self.inf_precision and len(num_str) > self.max_num_len:
if DEBUG:
print_line(f"FORMATTING LONG NUMBERS TO EXPONENTS")
FormatCodes.print(f"[dim](input string:) {num_str}")
num_str = self._format_exponents(num_str)
if DEBUG:
FormatCodes.print(f"[dim](formatted string:) {num_str}")
return num_str
def _format_exponents(self, string: str, /) -> str:
pattern = re.compile(r"(\d*\.\d+|\d+)(?![\de])")
def replace_match(match: re.Match[str]) -> str:
if len(str(number_sequence := match.group(1))) <= self.max_num_len:
return number_sequence
base = number_sequence[:self.max_num_len]
exponent_value = len(number_sequence) - self.max_num_len
if exponent_value >= 0:
sign = "+"
else:
sign = "-"
return base + "e" + sign + str(abs(exponent_value))
return pattern.sub(replace_match, string)
def _is_recurring(self, string: str, /, *, max_check_loops: int = -1) -> list[bool] | bool:
if not (repts := list(self._get_rept(string))):
return False
repts.reverse()
loops = (len(repts) if max_check_loops < 0 or len(repts) < max_check_loops else max_check_loops)
for loop in range(loops):
rept = repts[loop]
if not string[-((len(rept)) * 2):] == rept * 2:
found = i = 0
for i in range(len(string), 1, -1):
if string[i - len(rept):i] == rept:
if found > 0:
break
else:
found += 1
if not found:
return False
else:
rept_i = 0
for char in string[i:]:
if char == rept[rept_i]:
i += 1
else:
i = 0
break
rept_i += 1
if rept_i == len(rept):
rept_i = 0
if i > 0:
return True
elif loop == loops - 1:
return False
else:
return True
return False
@staticmethod
def _get_rept(string: str, /):
for match in re.finditer(r"(.+?)\1+", string):
yield match.group(1)
def _convert_ids_to_symbols(self, tokens: list[str | object], /) -> str:
"""Convert operator/constant/function IDs back to symbols for sympy evaluation."""
result: list[str] = []
for token in tokens:
if isinstance(token, str):
if token.startswith("o:"):
for op_id, symbols in OPERATORS.ALL:
if op_id == token:
result.append(symbols[0])
break
else:
result.append(token)
elif token.startswith("c:"):
for const_id, symbols in CONSTANTS.ALL:
if const_id == token:
result.append(symbols[0])
break
else:
result.append(token)
elif token.startswith("f:"):
for func_id, symbols in FUNCTIONS.ALL:
if func_id == token:
result.append(symbols[0])
break
else:
result.append(token)
else:
result.append(token)
else:
result.append(str(token))
return "".join(result)
def _find_matches(self, text: str, /) -> list[str | object]:
preliminary_matches = [match for match in PATTERN.findall(text) if match] # FILTER OUT EMPTY STRINGS
matches: list[str | object] = []
i = 0
while i < len(preliminary_matches):
match = preliminary_matches[i]
# CHECK IF THIS IS A MINUS SIGN THAT SHOULD BE COMBINED WITH THE NEXT NUMBER
if (match in OPERATORS.MINUS[1]
and i + 1 < len(preliminary_matches)
and REGEX.thousands_seps.sub("", preliminary_matches[i + 1]).replace(".", "").isdigit()
):
# CHECK IF THIS SHOULD BE TREATED AS A NEGATIVE NUMBER (NOT SUBTRACTION)
should_be_negative = False
if i == 0: # AT THE BEGINNING
should_be_negative = True
else:
prev_match = preliminary_matches[i - 1]
# IF PREVIOUS TOKEN IS AN OPERATOR OR OPEN PARENTHESIS, TREAT AS NEGATIVE NUMBER
if (OPERATORS.is_operator(prev_match)
or prev_match == "("
or FUNCTIONS.is_function(prev_match)
):
should_be_negative = True
if should_be_negative:
# COMBINE MINUS WITH NEXT NUMBER AND CLEAN UNDERSCORES
matches.append(clean_num(match + preliminary_matches[i + 1]))
i += 2 # SKIP THE NEXT TOKEN SINCE WE CONSUMED IT
else:
# KEEP AS SEPARATE SUBTRACTION OPERATOR
matches.append(match)
i += 1
# DISTINGUISH BETWEEN 'FACTORIAL' AND 'NOT'
elif match == "!":
should_be_factorial = False
if i > 0:
prev_match = preliminary_matches[i - 1]
# IF PREVIOUS TOKEN IS A NUMBER, CLOSING PARENTHESIS, OR CONSTANT, TREAT AS FACTORIAL
if (REGEX.thousands_seps.sub("", prev_match).replace(".", "").replace("-", "").isdigit()
or prev_match == ")"
or CONSTANTS.is_constant(prev_match)
):
should_be_factorial = True
if should_be_factorial:
matches.append(OPERATORS.FACTORIAL[0])
else:
matches.append(OPERATORS.NOT[0])
i += 1
else:
# CONVERT TOKENS TO IDS FOR OPERATORS, CONSTANTS AND FUNCTIONS
if OPERATORS.is_operator(match):
matches.append(OPERATORS.get_id(match))
elif CONSTANTS.is_constant(match):
matches.append(CONSTANTS.get_id(match))
elif FUNCTIONS.is_function(match):
matches.append(FUNCTIONS.get_id(match))
else:
# CLEAN UNDERSCORES FROM NUMERIC TOKENS
matches.append(clean_num(match))
i += 1
if DEBUG:
print_line("FINDING MATCHES")
FormatCodes.print(f"[dim](input text:)\n[b|dim](>>>) {text}")
FormatCodes.print(f"[dim](preliminary matches:) {preliminary_matches}")
FormatCodes.print(f"[dim](final matches:) {matches}")
return matches
def _perform_eval(self, calc_str: str, /) -> str:
"""Internal recursive calculation function that doesn't do preprocessing."""
SAVE_CALC_STR = calc_str
# HANDLE MATHEMATICAL GROUPING PARENTHESES (NOT FUNCTION CALLS)
while "(" in calc_str and ")" in calc_str:
paren_stack: list[int] = []
start_idx = -1
end_idx = -1
# FIND THE INNERMOST PARENTHESES
for i, char in enumerate(calc_str):
if char == "(":
paren_stack.append(i)
elif char == ")":
if paren_stack:
start_idx = paren_stack.pop()
end_idx = i
# CHECK IF THIS IS A FUNCTION CALL BY LOOKING AT WHAT'S BEFORE THE OPENING PARENTHESIS
if start_idx > 0:
token_start = start_idx - 1
while token_start > 0 and calc_str[token_start - 1].isalnum():
token_start -= 1
token_before = calc_str[token_start:start_idx]
# IF IT'S A FUNCTION, DON'T PROCESS THESE PARENTHESES
if FUNCTIONS.is_function(token_before):
continue
inner_expr = calc_str[start_idx + 1:end_idx]
result = self._perform_eval(inner_expr)
should_add_mult = (start_idx > 0 and calc_str[start_idx - 1].isdigit())
calc_str = calc_str[:start_idx] + ("*" if should_add_mult else "") + result + calc_str[end_idx + 1:]
break
else:
break
numpy.set_printoptions(floatmode="fixed", formatter={"float_kind": "{:f}".format}) # HANDLE SCIENTIFIC NOTATION
split = self._find_matches(calc_str)
# CONVERT ALL OPERANDS TO 'SymPy' EXPRESSIONS
def sympify(split_matches: list[str | object], /) -> list[str | object]:
split_sympy: list[str | object] = []
for token in split_matches:
if isinstance(token, str) and token.startswith(("o:", "c:", "f:")):
split_sympy.append(token)
else:
try:
split_sympy.append(sanitize(token))
except:
split_sympy.append(token)
return split_sympy
split_sympy = sympify(split)
# ITERATE OVER CONSTANTS FIRST
for c_id, _ in CONSTANTS.ALL:
while c_id in split:
idx = split.index(c_id)
if DEBUG:
print_line(f"CALCULATING CONSTANT")
FormatCodes.print(f"[dim](constant ID:) {c_id}")
constant_value = CONSTANTS.get(c_id)
if c_id == CONSTANTS.ANS[0] and constant_value is None:
raise Exception("Answer constant was not specified")
if DEBUG:
FormatCodes.print(f"[dim](value:) {constant_value}")
formatted_result = str(self.format_result(constant_value))
new_split = split[:idx] + [formatted_result] + split[idx + 1:]
split = new_split
split_sympy = sympify(split)
# ITERATE OVER FUNCTIONS AVAILABLE
for f_id, _ in FUNCTIONS.ALL:
while f_id in split:
idx = split.index(f_id)
if (idx + 1 < len(split) and split[idx + 1] == "("):
paren_count = 0
end_paren_idx = -1
for i in range(idx + 1, len(split)):
if split[i] == "(":
paren_count += 1
elif split[i] == ")":
paren_count -= 1
if paren_count == 0:
end_paren_idx = i
break
if end_paren_idx == -1:
break
arg_tokens = split[idx + 2:end_paren_idx]
if DEBUG:
print_line(f"CALCULATING FUNCTION")
FormatCodes.print(f"[dim](function ID:) {f_id}")
FormatCodes.print(f"[dim](arg_tokens:) {arg_tokens}")
# HANDLE MULTI-ARGUMENT FUNCTIONS
if len(arg_tokens) == 1:
arg_value = split_sympy[idx + 2]
function_impl = FUNCTIONS.get(f_id)
if function_impl is None:
break
result = function_impl(arg_value)
else:
if "," in arg_tokens:
comma_idx = arg_tokens.index(",")
arg1_tokens = arg_tokens[:comma_idx]
arg2_tokens = arg_tokens[comma_idx + 1:]
if len(arg1_tokens) == 1:
arg1_sympy_idx = idx + 2
arg1_value = split_sympy[arg1_sympy_idx]
else:
arg1_str = self._convert_ids_to_symbols(arg1_tokens)
arg1_value = sanitize(arg1_str)
if len(arg2_tokens) == 1:
arg2_sympy_idx = idx + 2 + len(arg1_tokens) + 1
arg2_value = split_sympy[arg2_sympy_idx]
else:
arg2_str = self._convert_ids_to_symbols(arg2_tokens)
arg2_value = sanitize(arg2_str)
function_impl = FUNCTIONS.get(f_id)
if function_impl is None:
break
if DEBUG:
FormatCodes.print(f"[dim](two-argument function)")
FormatCodes.print(f"[dim](arg1:) {arg1_value}")
FormatCodes.print(f"[dim](arg2:) {arg2_value}")
result = function_impl(arg1_value, arg2_value)
# SINGLE COMPLEX ARGUMENT
else:
arg_str = self._convert_ids_to_symbols(arg_tokens)
if DEBUG:
FormatCodes.print(f"[dim](evaluating arg expression:) {arg_str}")
arg_value = sanitize(arg_str)
function_impl = FUNCTIONS.get(f_id)
if function_impl is None:
break
result = function_impl(arg_value)
if DEBUG:
FormatCodes.print(f"[dim](result:) {result}")
formatted_result = self.format_result(result)
new_split = split[:idx] + [formatted_result] + split[end_paren_idx + 1:]
split = new_split
split_sympy = sympify(split)
# NO PARENTHESES FOUND - NOT A FUNCTION CALL
else:
break
# ITERATE OVER OPERATORS BASED ON PRECEDENCE
while len(split) > 1:
operator_positions: list[tuple[int, str, int]] = []
for i, token in enumerate(split):
if isinstance(token, str) and token.startswith("o:"):
precedence = OPERATORS.get_precedence(token)
# GIVE PREFIX 'NOT' HIGHER PRECEDENCE THAN BINARY OPERATORS
if (token == OPERATORS.NOT[0] and (
i == 0
or isinstance(s := split[i - 1], str) and s.startswith("o:")
or s in ["("]
)):
precedence = 3 # HIGHER THAN BINARY ARITHMETIC OPERATORS
operator_positions.append((i, token, precedence))
if not operator_positions:
break
highest_precedence = max(op[2] for op in operator_positions)
highest_ops = [op for op in operator_positions if op[2] == highest_precedence]
idx, operator_id, _ = highest_ops[-1]
if DEBUG:
print_line(f"CALCULATING OPERATOR")
FormatCodes.print(f"[dim](operator ID:) {operator_id}")
operator_func = OPERATORS.get(operator_id)
if operator_func is None:
break
# POSTFIX FACTORIAL OPERATOR
if operator_id == OPERATORS.FACTORIAL[0]:
if idx == 0:
break
result = operator_func(split_sympy[idx - 1], None)
if DEBUG:
FormatCodes.print(f"[dim](argument:) {split_sympy[idx - 1]}")
FormatCodes.print(f"[dim](operator:) {operator_id} [dim]((postfix factorial))")
FormatCodes.print(f"[dim](result:) {result}")
new_split = split[:idx - 1] + [self.format_result(result)] + split[idx + 1:]
# UNARY MINUS
elif operator_id == OPERATORS.MINUS[0] and (
idx == 0
or isinstance(s := split[idx - 1], str) and s.startswith("o:")
):
if idx + 1 >= len(split):
break
result = operator_func(0, split_sympy[idx + 1])
if DEBUG:
FormatCodes.print(f"[dim](argument:) 0")
FormatCodes.print(f"[dim](operator:) {operator_id} [dim]((unary minus))")
FormatCodes.print(f"[dim](argument:) {split_sympy[idx + 1]}")
FormatCodes.print(f"[dim](result:) {result}")
new_split = split[:idx] + [self.format_result(result)] + split[idx + 2:]
# PREFIX NOT OPERATOR
elif operator_id == OPERATORS.NOT[0] and (
idx == 0
or isinstance(s := split[idx - 1], str) and s.startswith("o:")
or s in ["("]
):
if idx + 1 >= len(split):
break
result = operator_func(split_sympy[idx + 1], None)
if DEBUG:
FormatCodes.print(f"[dim](operator:) {operator_id} [dim]((prefix NOT))")
FormatCodes.print(f"[dim](argument:) {split_sympy[idx + 1]}")
FormatCodes.print(f"[dim](result:) {result}")
new_split = split[:idx] + [self.format_result(result)] + split[idx + 2:]
# BINARY OPERATOR
else:
if idx == 0 or idx + 1 >= len(split):
break
result = operator_func(split_sympy[idx - 1], split_sympy[idx + 1])
if DEBUG:
FormatCodes.print(f"[dim](argument:) {split_sympy[idx - 1]}")
FormatCodes.print(f"[dim](operator:) {operator_id}")
FormatCodes.print(f"[dim](argument:) {split_sympy[idx + 1]}")
FormatCodes.print(f"[dim](result:) {result}")
new_split = split[:idx - 1] + [self.format_result(result)] + split[idx + 2:]
split = new_split
split_sympy = sympify(split)
if len(split) == 1:
calc_str = str(split[0])
else:
calc_str = " ".join(str(s) for s in split)
try:
result = sanitize(calc_str)
calc_str = self.format_result(result)
except:
raise Exception(f"Could not perform calculation on [br:cyan]({SAVE_CALC_STR})")
if calc_str == SAVE_CALC_STR:
try:
sanitize(calc_str)
except:
raise Exception(f"Could not perform calculation on [br:cyan]({SAVE_CALC_STR})")
return calc_str
def main():
print()
if not ARGS.help.exists and len(calc_str_parts := list(ARGS.calculation.values)) > 0:
precision_value = int(ARGS.precision.values[0]) if ARGS.precision.values and ARGS.precision.values[0].lstrip("-").isdigit() else 100
if precision_value <= 0 and precision_value != -1:
Console.fail(f"[b](ValueError:) Precision must be positive or [br:cyan](-1) for infinite precision, got [br:cyan]({precision_value})", end="\n\n")