lorentz-html.py

'''\
Generate Lorentz HTML page.

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

'''
####################################################################################################
from sys import stdin, stdout, stderr, argv
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.000001, 0.00001, 0.0001, 0.001, 0.01,
    0,
    0.10,
    0.20, 0.25,
    0.30, 0.333333,
    0.40,
    0.50,
    0.60, 0.666666,
    0.70, 0.75,
    0.80, 0.85,
    0.90,
    0,
    0.91, 0.92, 0.93, 0.94, 0.95, 0.96, 0.97, 0.98, 0.99,
    0,
    0.995,
    0.999, 0.9999, 0.99999, 0.999999, 0.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 (fpath, fname):
    fn = path.join(fpath, fname)
    fp = open(fn, 'w')
    print 'opened: %s [path: %s]' % (fname, fpath)
    try:
        # Head text...
        print >> fp, html_head
        # Generate list of velocities...
        vlist = map(lambda v: c1 * float(v), flist)
        # Generate table...
        for factor,velocity in zip(flist, vlist):
            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*factor, s1, s3, s2, g, 1.0/g)
                print >> fp, html_table_row % t
            else:
                print >> fp, html_table_row_divider
        # Tail text...
        print >> fp, html_tail

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

    finally:
        fp.close()


##================================================================================================##
def do_test (*args):
    print 'test/parameters: %d' % len(args)
    fname = args[0] if 0 < len(args) else FileName
    dname = args[1] if 1 < len(args) else BasePath
    return [fname, dname]
##================================================================================================##
def do_main (*args):
    print 'main/parameters: %d' % len(args)
    fname = args[0] if 0 < len(args) else FileName
    dname = args[1] if 1 < len(args) else BasePath
    create_html_file(dname, fname)
    return [fname, dname]
####################################################################################################
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 [None, cmd, args]
####################################################################################################
if __name__ == '__main__':
    print 'autorun: %s' % argv[0]
    cmd = argv[1] if 1 < len(argv) else ''
    etc = argv[2:]
    obj = dispatch(cmd, *etc)
    print ''
    print 'exit'
####################################################################################################
'''eof'''