File: units.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 # units.pyw
  27 #
  28 # This is a GUI which lets you run python expression to calculate numbers,
  29 # which are then used to convert from/to many common measurement units.
  30 #
  31 # Updates happen automatically, each time the main input changes.
  32 
  33 
  34 import math
  35 from math import \
  36     acos, acosh, asin, asinh, atan, atan2, atanh, ceil, comb, \
  37     copysign, cos, cosh, degrees, dist, e, erf, erfc, exp, expm1, \
  38     fabs, factorial, floor, fmod, frexp, fsum, gamma, gcd, hypot, inf, \
  39     isclose, isfinite, isinf, isnan, isqrt, lcm, ldexp, lgamma, log, \
  40     log10, log1p, log2, modf, nan, nextafter, perm, pi, pow, prod, \
  41     radians, remainder, sin, sinh, sqrt, tan, tanh, tau, trunc, ulp
  42 try:
  43     from math import cbrt, exp2
  44 except:
  45     pass
  46 
  47 from random import \
  48     betavariate, choice, choices, expovariate, gammavariate, gauss, \
  49     getrandbits, getstate, lognormvariate, normalvariate, paretovariate, \
  50     randbytes, randint, random, randrange, sample, seed, setstate, \
  51     shuffle, triangular, uniform, vonmisesvariate, weibullvariate
  52 
  53 from statistics import \
  54     bisect_left, bisect_right, fmean, \
  55     geometric_mean, harmonic_mean, mean, median, \
  56     median_grouped, median_high, median_low, mode, multimode, pstdev, \
  57     pvariance, quantiles, stdev, variance
  58 try:
  59     from statistics import \
  60         correlation, covariance, linear_regression, mul, reduce
  61 except:
  62     pass
  63 
  64 from tkinter import Tk, Entry, Label, TOP, BOTTOM, LEFT, Event
  65 from typing import List, Tuple, Union, Any
  66 
  67 
  68 def check_shortcuts(event: Any) -> None:
  69     char: str = event.char
  70     if char == '':
  71         return
  72     # copy formula and result to clipboard when enter is pressed
  73     if ord(char) == 13:
  74         copy(output['text'])
  75     # quit when esc key is pressed
  76     if ord(char) == 27:
  77         win.quit()
  78 
  79 
  80 def update_result(event: Event) -> None:
  81     try:
  82         expr = input.get()
  83         if expr == '':
  84             return
  85         # square brackets are easier to type and are valid math
  86         expr = expr.replace('[', '(').replace(']', ')')
  87         output['text'] = convert(eval(expr))
  88     except Exception as e:
  89         output['text'] = e
  90 
  91 
  92 def copy(s: str) -> None:
  93     # tkinter's clipboard functionality isn't working
  94     from subprocess import run
  95     for p in ['xclip -i', 'wclip -i', 'clip.exe', 'pbcopy']:
  96         try:
  97             run(p, input=s, text=True)
  98             # copy successful, so return
  99             return
 100         except Exception as _:
 101             pass
 102 
 103 
 104 def convert(x: float) -> str:
 105     t = []
 106     for e in table:
 107         if e is None:
 108             t.append('')
 109         else:
 110             y = e[1] * x + e[2]
 111             t.append('{:,} {:<3} = {:,.4f} {}'.format(x, e[0], y, e[3]))
 112     return '\n'.join(t)
 113 
 114 
 115 # unit conversion table
 116 table: List[Union[Tuple[str, float, float, str], None]] = [
 117     # from, multiplier, offset, to
 118     # none values mark where to put empty lines in the output
 119     ('ft', 0.3048, 0, 'm'),
 120     ('m', 3.28084, 0, 'ft'),
 121     ('in', 2.54, 0, 'cm'),
 122     ('cm', 0.393701, 0, 'in'),
 123     ('mi', 1.60934, 0, 'km'),
 124     ('km', 0.621371, 0, 'mi'),
 125     ('yd', 0.9144, 0, 'm'),
 126     ('m', 1.09361, 0, 'yd'),
 127     None,
 128     ('f', 5/9, -32*5/9, 'c'),
 129     ('c', 9/5, 32, 'f'),
 130     None,
 131     ('lb', 0.453592, 0, 'kg'),
 132     ('kg', 2.20462, 0, 'lb'),
 133     ('oz', 28.3495, 0, 'g'),
 134     ('g', 0.035274, 0, 'oz'),
 135     None,
 136     ('gal', 3.78541, 0, 'l'),
 137     ('l', 0.264172, 0, 'gal'),
 138     None,
 139     ('in2', 6.4516, 0, 'cm2'),
 140     ('cm2', 0.15500031, 0, 'in2'),
 141     ('ft2', 0.092903, 0, 'm2'),
 142     ('m2', 10.7639, 0, 'ft2'),
 143     ('mi2', 2.58999, 0, 'km2'),
 144     ('km2', 0.386102, 0, 'mi2'),
 145     ('ac', 0.00404686, 0, 'km2'),
 146     ('km2', 247.105, 0, 'ac'),
 147 ]
 148 
 149 win = Tk()
 150 win.title('Unit-conversion Calculator')
 151 input = Entry(font=20, width=35)
 152 input.pack(side=TOP, padx=5)
 153 input.bind('<KeyPress>', check_shortcuts)
 154 input.bind('<KeyRelease>', update_result)
 155 input.focus()
 156 
 157 font = ('Lucida Console', 12)
 158 output = Label(text='esc\tquits\nenter\tcopies to clipboard', font=font)
 159 output.config(justify=LEFT)
 160 output.pack(side=BOTTOM, padx=10, pady=5)
 161 win.mainloop()