Fast Evaluation
Stephen Lukacs (2) iquanta.org/instruct/python
| .. .
Calculate/Evaluate:
this is a test of the American Broadcast System. This is only a test.. .
"""
reference: https://iquanta.org/instruct/python ::: Math 0: Calculator, Fast Evaluation ::: Stephen Lukacs, Ph.D. ©2021-11-17
"""
#bos...initialization but from under the server-side python...
from sys import version
server_python_version = version[:version.find(' ')]
import math
slst = set([ f for f in sorted(math.__dir__()) if (f[:2] != '__') ]) #server-side list of python math functions
from yatl.helpers import XML #XML needed under script to maintain the slst list as a list.
#eos.
<page_scripts>
<style>
h3 { margin: 0px; }
p { margin: 10px 0px; }
input[type="text"][id="input"] { width: 70%; border-radius: 5px; }
input[type="submit"] { height: 40px; padding: 6px 30px; border-radius: 10px; }
</style>
<script type="text/python">
# -*- coding: utf-8 -*-
"""
the following is brython code and thus client-side python.
mostly python style and syntax except when interfacing with the html document,
and when you need a package, i.e., numpy, scipy, etc., outside the standard packages, i.e., math, datetime, etc.
"""
import browser.timer
from browser import document
from browser.html import B, U, BR, DIV, P, SPAN, H2, H3
from datetime import datetime
from math import * #import all math functions and usage is direct, or, without the math.pi in front.
from math import log as ln, log10 as log, floor #i prefer ln for natural log, and log for base10.
def float_to_mantissa_exponential(f, sig_figs=None):
exponential = int(floor(log(abs(f)))) if (f != 0.) else 0
mantissa = f/(10**exponential)
if sig_figs:
return (mantissa, exponential, ('{:.'+str(sig_figs-1)+'f}x10<sup>{}</sup>').format(mantissa, exponential))
else:
return (mantissa, exponential)
round_to = 5
def show_result(event):
btn = event.target
itime = datetime.now()
input = document['input'].value
print(f"input: {input}")
try:
e = eval(input)
if (-1e3 < e < +1e3):
calc = f"{input} = <b><u>{round(e, round_to)}</u></b>"
else:
calc = f"{input} = <b><u>{e:.{round_to}e}</u></b> = <b><u>{float_to_mantissa_exponential(e, round_to+1)[2]}</u></b>"
except Exception as e:
calc = f"exception raised: {e}"
document['body'].clear()
document['body'] <= P(calc, style="font-size: 20pt;")
ftime = datetime.now()
document['time_span'].text = f"{(ftime - itime).total_seconds()}s"
return
def update_time():
now = datetime.now().strftime("%A, %B %d, %Y @ <b>%H:%M:%S</b>")
document['clock'].innerHTML = now
return now
#bos...initialization but from under the client-side bython...
itime = datetime.now()
client_brython_version = '.'.join([str(i) for i in __BRYTHON__.implementation[:3]])
print(f'brython version: {client_brython_version} ___ Math 0: Calculator, Fast Evaluation ___ iquanta.org/instruct/python ___ client-side sjlukacs implementation')
browser.timer.set_interval(update_time, 200)
document['return'].bind('click', show_result)
#bos...math.__dir__()
import math
clst = set([ f for f in sorted(math.__dir__()) if (f[:2] != '__') ]) #client-side list of brython math functions
document['body'].clear()
document['body'] <= P(H3(f'...client-side brython ({client_brython_version}) math functions...') + ', '.join(clst))
del math
slst = {{=XML(slst)}} #slst has to be escaped with XML to keep it a python/brython list.
server_python_version = "{{=server_python_version}}" #but since a string, doesn't need XML escaping.
#print(type(slst), slst)
document['body'] <= P(H3(f'...server-side python ({server_python_version}) math functions...') + ', '.join(slst))
x = clst.symmetric_difference(slst)
if (len(x) == 0):
document['body'] <= P(H3('...and there are no differences between the two math packages!...'))
else:
document['body'] <= P(H3('...so there are some differences between the two math packages! namely...') + ', '.join(x))
#eos...math.__dir__()
print(eval('-5-8'), 'should be -13 ___', eval('-5 - (-5)'), 'should be 0')
ftime = datetime.now()
document['time_span'].text = f"{(ftime - itime).total_seconds()}s"
#eos.
</script>
</page_scripts>
<header>
<h3 style="text-align:left;"><span id="clock"></span> | <span id="time_span"></span> .. .</h3>
Calculate/Evaluate: <input type="text" id="input" value="6*(4 + sin(pi))*69999" />
<input type="submit" id="return" name="return" value="Refresh"/> <span id="msg"></span>
</header>
<body>
this is a test of the American Broadcast System. This is only a test.. .
</body>