lorentz-html.py

'''\
Generate Lorentz HTML page.

gamma = 1 / square-root(1 - (sqr(v) / sqr(c1)))

'''
####################################################################################################
from sys import stdinstdoutstderrargv
from os import path
from math import sqrt
from zoo import add_commas
####################################################################################################

BasePath = r'C:\CJS\prj\Python\app'
FileName = r'lorentz.html'

##------------------------------------------------------------------------------------------------##
flist = [0,
    0.0000010.000010.00010.0010.01,
    0,
    0.10,
    0.200.25,
    0.300.333333,
    0.40,
    0.50,
    0.600.666666,
    0.700.75,
    0.800.85,
    0.90,
    0,
    0.910.920.930.940.950.960.970.980.99,
    0,
    0.995,
    0.9990.99990.999990.9999990.99999999
]

KPM = 0.621371
c1  = float(299792458)
c2  = float(c1 * c1)

Lorentz = lambda v: 1/sqrt(1.0 - ((float(v) * float(v)) / c2))

##------------------------------------------------------------------------------------------------##
html_head = '''\
<style type="text/css">
#Lorentz {
    font-family: sans-serif;
    font-size: 11pt;
    border-spacing: 4pt 2pt;
    padding: 2pt 0;
    border: 1px solid black;
    margin: 9pt 12pt;
}
#Lorentz th {
    color: white;
    background-color: navy;
    font-family: serif;
    font-size: 12pt;
    font-weight: bold;
    font-style: italic;
    padding: 4px 6pt 6px 6pt;
}
#Lorentz td {
    text-align: right;
    padding: 2px 4pt 2px 12pt;
    border: 1px solid black;
}
#Lorentz .c_factor {
    color: white;
    background-color: maroon;
    font-weight: bold;
    padding-left: 9pt;
    padding-right: 4pt;
}
#Lorentz .c_v {
    color: red;
}
#Lorentz .c_g ,
#Lorentz .c_ig {
    color: navy;
}
#Lorentz .c_divider {
    height: 10px;
    border: 0;
}
</style>
<table id="Lorentz">
<caption>Lorentz Effect</caption>
<thead>
<tr><th>%c</th> <th>m/s</th> <th>KPH</th> <th>MPH</th> <th>&gamma;</th> <th>1/&gamma;</th></tr>
</thead>
<tfoot>
<tr><th>%c</th> <th>m/s</th> <th>KPH</th> <th>MPH</th> <th>&gamma;</th> <th>1/&gamma;</th></tr>
</tfoot>
<tbody>'''

html_table_row = '''<tr>\
<td class="c_factor">%.6f</td>\
<td class="c_v">%s</td>\
<td class="c_kph">%s</td>\
<td class="c_mph">%s</td>\
<td class="c_g">%.15f</td>\
<td class="c_ig">%.15f</td>\
</tr>'''

html_table_row_divider = '''\
<tr><td colspan="6" class="c_divider"></td></tr>'''

html_tail = '''\
</tbody>
</table>
'''


##================================================================================================##
def create_html_file (fpathfname):
    fn = path.join(fpathfname)
    fp = open(fn'w')
    print 'opened: %s [path: %s]' % (fnamefpath)
    try:
        # Head text...
        print >> fphtml_head
        # Generate list of velocities...
        vlist = map(lambda v: c1 * float(v), flist)
        # Generate table...
        for factor,velocity in zip(flistvlist):
            if factor:
                g = Lorentz(velocity)
                kph = (velocity / 1000) * 3600
                mph = kph * KPM
                s1 = add_commas("%.0f" % velocity)
                s2 = add_commas("%.0f" % mph)
                s3 = add_commas("%.0f" % kph)
                t = (100*factors1s3s2g1.0/g)
                print >> fphtml_table_row % t
            else:
                print >> fphtml_table_row_divider
        # Tail text...
        print >> fphtml_tail

        print 'wrote: %s [%d rows]' % (fnamelen(flist))
    except Exception as e:
        print e
        raise

    finally:
        fp.close()


##================================================================================================##
def do_test (*args):
    print 'test/parameters: %d' % len(args)
    fname = args[0if 0 < len(argselse FileName
    dname = args[1if 1 < len(argselse BasePath
    return [fnamedname]
##================================================================================================##
def do_main (*args):
    print 'main/parameters: %d' % len(args)
    fname = args[0if 0 < len(argselse FileName
    dname = args[1if 1 < len(argselse BasePath
    create_html_file(dnamefname)
    return [fnamedname]
####################################################################################################
def dispatch (cmd, *args):
    print 'command: %s' % cmd
    print 'arguments: %d' % len(args)
    if cmd == 'main': return do_main(*args)
    if cmd == 'test': return do_test(*args)
    return [Nonecmdargs]
####################################################################################################
if __name__ == '__main__':
    print 'autorun: %s' % argv[0]
    cmd = argv[1if 1 < len(argvelse ''
    etc = argv[2:]
    obj = dispatch(cmd, *etc)
    print ''
    print 'exit'
####################################################################################################
'''eof'''