File: inca.pyw
   1 #!/usr/bin/python3
   2 
   3 # The MIT License (MIT)
   4 #
   5 # Copyright © 2024 pacman64
   6 #
   7 # Permission is hereby granted, free of charge, to any person obtaining a copy
   8 # of this software and associated documentation files (the “Software”), to deal
   9 # in the Software without restriction, including without limitation the rights
  10 # to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  11 # copies of the Software, and to permit persons to whom the Software is
  12 # furnished to do so, subject to the following conditions:
  13 #
  14 # The above copyright notice and this permission notice shall be included in
  15 # all copies or substantial portions of the Software.
  16 #
  17 # THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  18 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  19 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  20 # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  21 # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  22 # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  23 # SOFTWARE.
  24 
  25 
  26 # inca.pyw
  27 #
  28 # INteger-input CAlculator is a GUI app which live-evaluates the python
  29 # expression currently in its single-line input, re-running it as it changes
  30 # for each value in the integer range defined by 2 more expressions.
  31 
  32 
  33 from json import dumps
  34 
  35 import math
  36 from math import \
  37     acos, acosh, asin, asinh, atan, atan2, atanh, ceil, comb, \
  38     copysign, cos, cosh, degrees, dist, e, erf, erfc, exp, expm1, \
  39     fabs, factorial, floor, fmod, frexp, fsum, gamma, gcd, hypot, inf, \
  40     isclose, isfinite, isinf, isnan, isqrt, lcm, ldexp, lgamma, log, \
  41     log10, log1p, log2, modf, nan, nextafter, perm, pi, pow, prod, \
  42     radians, remainder, sin, sinh, sqrt, tan, tanh, tau, trunc, ulp
  43 try:
  44     from math import cbrt, exp2
  45 except:
  46     pass
  47 
  48 from random import \
  49     betavariate, choice, choices, expovariate, gammavariate, gauss, \
  50     getrandbits, getstate, lognormvariate, normalvariate, paretovariate, \
  51     randbytes, randint, random, randrange, sample, seed, setstate, \
  52     shuffle, triangular, uniform, vonmisesvariate, weibullvariate
  53 
  54 from statistics import \
  55     bisect_left, bisect_right, fmean, \
  56     geometric_mean, harmonic_mean, mean, median, \
  57     median_grouped, median_high, median_low, mode, multimode, pstdev, \
  58     pvariance, quantiles, stdev, variance
  59 try:
  60     from statistics import \
  61         correlation, covariance, linear_regression, mul, reduce
  62 except:
  63     pass
  64 
  65 from tkinter.ttk import Treeview
  66 from tkinter.messagebox import showerror
  67 from tkinter import Entry, Frame, Label, StringVar, Tk, END, LEFT
  68 
  69 
  70 # some convenience aliases to various funcs from the python stdlib
  71 geomean = geometric_mean
  72 harmean = harmonic_mean
  73 sd = stdev
  74 popsd = pstdev
  75 var = variance
  76 popvar = pvariance
  77 randbeta = betavariate
  78 randexp = expovariate
  79 randgamma = gammavariate
  80 randlognorm = lognormvariate
  81 randnorm = normalvariate
  82 randweibull = weibullvariate
  83 
  84 # some occasionally-useful values
  85 kb = 1024
  86 mb = 1024 * kb
  87 gb = 1024 * mb
  88 tb = 1024 * gb
  89 pb = 1024 * tb
  90 mole = 602214076000000000000000
  91 mol = mole
  92 
  93 # quick guide for the treeview-specific api
  94 # https://riptutorial.com/tkinter/example/31880/treeview--basic-example
  95 
  96 
  97 def update() -> None:
  98     '''Evaluates the current formula, then updates the bottom table'''
  99 
 100     # try to get the range of input values
 101     try:
 102         start = int(eval(fromnum.get()))
 103         stop = int(eval(tonum.get()))
 104     except:
 105         return
 106 
 107     # clear the output
 108     for c in out.get_children():
 109         out.delete(c)
 110 
 111     # update output rows
 112     j = 0  # the actual row-insertion index
 113     src = inp.get()
 114 
 115     for i, n in enumerate(range(start, stop + 1)):
 116         if i >= 1000:
 117             # avoid hanging the GUI, so quit after calculating 1000 rows
 118             break
 119 
 120         # visually-separate groups of 5 rows by adding an empty one
 121         if i > 0 and i % 5 == 0:
 122             out.insert('', j, text='')
 123             j += 1
 124 
 125         # calculate formula-result for the current value
 126         try:
 127             x = n  # allow x in formulas, as an alias for n
 128             y = eval(src)
 129         except Exception as e:
 130             val = [f'{n}', f'{e}']
 131             out.insert('', j, text=f'{i + 1}', values=val)
 132 
 133             # make it obvious an error just occurred and return
 134             inp.config(bg='darkred', fg='white')
 135             out.heading('result', text='f(n)')
 136             return
 137 
 138         # insert row with the current result
 139         if isinstance(y, int) or isinstance(y, float):
 140             val = [f'{n}', f'{y:,.6f}']
 141         else:
 142             val = [f'{n}', str(y)]
 143         out.insert('', j, text=f'{i + 1}', values=val)
 144         j += 1
 145 
 146     out.heading('result', text=src)
 147     # reset style to indicate success
 148     inp.config(bg='white', fg='black')
 149 
 150 
 151 try:
 152     win = Tk()
 153     win.title('INteger-input CAlculator')
 154     win.bind('<Escape>', lambda _: win.quit())
 155 
 156     # top of window: formula and integer-range inputs
 157     top = Frame(win)
 158     Label(top, text='f(n) = ', font=30).grid(row=0, column=0, padx=0)
 159     inp = Entry(top, text=StringVar(value='n'), width=32, borderwidth=1)
 160     inp.select_range(0, END)
 161     inp.grid(row=0, column=1, padx=0, sticky='e')
 162     inp.bind('<KeyRelease>', lambda _: update())
 163     inp.focus_set()
 164     Label(top, text='n = ', font=20).grid(row=0, column=2, padx=10)
 165     fromnum = Entry(top, text=StringVar(value=1), width=6, borderwidth=1)
 166     fromnum.grid(row=0, column=3, padx=2, sticky='e')
 167     fromnum.bind('<KeyRelease>', lambda _: update())
 168     Label(top, text=' .. ', font=20).grid(row=0, column=4, padx=0)
 169     tonum = Entry(top, text=StringVar(value=40), width=6, borderwidth=1)
 170     tonum.grid(row=0, column=5, padx=2, sticky='e')
 171     tonum.bind('<KeyRelease>', lambda _: update())
 172     top.pack(padx=1, pady=1, anchor='e')
 173 
 174     # bottom of window only has results table
 175     bottom = Frame(win)
 176     out = Treeview(bottom, column=['n', 'result'], height=48)
 177     out.heading('#0', text='#')
 178     out.column('#0',  width=80)
 179     out.heading('n', text='n')
 180     out.column('n', width=80, anchor='e')
 181     out.heading('result', text='f(n)')
 182     out.column('result', width=360, anchor='e')
 183     out.pack()
 184     bottom.pack(side=LEFT, padx=1, pady=1)
 185 
 186     # show results and run gui
 187     update()
 188     win.mainloop()
 189 except Exception as e:
 190     showerror('Error', str(e))
 191     win.quit()