Tuesday, February 1, 2011

Creating a Unit Conversion Page

We had a unit conversion page before but I felt the initial code was too complicated to maintain. A rewrite was necessary and though the page is still at version 0.0.1, we feel that the rewrite is justified. Here is the code, using Python and QP. I need a web artist or graphic designer to add color and spice to the unit conversion page.

from qp.sites.extreme.lib.config import image_dir, homeurl



__version__ = "0.0.1 2011.01.28"
__date__    = "2011.01.28"
__catalog__ = "UNITS-CONV-001"
__author__  = "ernesto.adorio@gmail.com"
__url__     = homeurl + "solvers/units/"
__title__   = "Fundamental Units Conversion Page"


from qp.pub.common      import get_session
from qp.fill.css        import BASIC_FORM_CSS
from qp.fill.directory  import Directory
from quixote.errors     import PublishError
from quixote.util       import dump_request
from qp.fill.form       import Form
from qp.fill.widget    import CheckboxWidget,StringWidget,SingleSelectWidget, StringWidget, TextWidget
from qp.pub.common      import header, footer, page, redirect
from   qp.sites.extreme.lib.uicommon import renderheader, renderfooter, processheader, processfooter
import time
import urllib

from unitslib import Length, Mass, Time, convert


import urllib, urllib2, os

from  qp.sites.extreme.lib.webutils  import before, after
from  qp.sites.extreme.lib import config


def getstatusoutput(command):
     """
     Fredrick Lundh 
     http://mail.python.org/pipermail/python-list/2006-October/406444.html 
     """
     from subprocess import Popen, PIPE, STDOUT
     p = Popen(command, stdout=PIPE, stderr=STDOUT, shell=True)
     s = p.stdout.read().split("\n")
     return p.wait(), s


class UnitsDirectory(Directory):
    def get_exports(self):
        yield '', 'index', 'NetTools server', None



    def index (self):
        form  = Form(enctype = "multipart/form-data")  # enctype for file upload

 form.add(StringWidget, name = "qty_length", value = "1.0", size = 30)
        length_options = []
        for key in Length:
            length_options.append(key)
      
 form.add(SingleSelectWidget, name = "unit_length",
           value = "meter",  
           options=length_options
        )   


 form.add(StringWidget, name = "qty_mass", value = "1.0", size = 30)
        mass_options = []
        for key in Mass:
            mass_options.append(key)
      
 form.add(SingleSelectWidget, name = "unit_mass",
           value = "kilogram",  
           options=mass_options
        )   

 form.add(StringWidget, name = "qty_time", value = "1.0", size = 30)
        time_options = []
        for key in Time:
            time_options.append(key)
      
 form.add(SingleSelectWidget, name = "unit_time",
           value  = "second",  
           options=time_options
        )   

 form.add_hidden("time",   value = time.time()) 
        form.add_submit("submit", "submit")

 def render [html] ():
            renderheader(__title__)
            if 1:
              """
              

Length Unit

%s%s
""" % (form.get_widget("qty_length").render(), form.get_widget("unit_length").render()) """

Mass Unit

%s%s
""" % (form.get_widget("qty_mass").render(), form.get_widget("unit_mass").render()) """

Time Unit

%s%s
""" % (form.get_widget("qty_time").render(), form.get_widget("unit_time").render()) "The quantified unit will be converted in other equivalent units." renderfooter(form, __version__, __catalog__, __author__) if not form.is_submitted(): return page('UnitsDirectory', render(), style= BASIC_FORM_CSS) def process [html] (): processheader("Unit Utility") calctime_start = time.time() "" "" "" "" "
"
qty = float(form.get("qty_length"))
unit = form.get("unit_length")

"

Length Conversion

"


'' % (qty, unit) for u in Length.keys(): " " % (convert(Length, qty, unit, u),u) "
Input [%s %s]
%s%s
"
"
"
qty = float(form.get("qty_mass"))
unit = form.get("unit_mass")

"

Mass Conversion

"
'' % (qty, unit) for u in Mass.keys(): " " % (convert(Mass, qty, unit, u),u) "
Input [%s %s]
%s%s
"
"
"
qty = float(form.get("qty_time"))
unit = form.get("unit_time")
"

Time Conversion

"

'' % (qty, unit) for u in Time.keys(): " " % (convert(Time, qty, unit, u),u) "
Input [%s %s]
%s%s
"
"
" processfooter(form, calctime_start, homeurl, __url__) return process()

Yuck! the html output code is being translated by a browser! Use your browswerĊ› View Source facility to recover the original contents.

Codes are provided for educational use. Please make the proper attribution.


Only the fundamental units for Length, Mass and Time are considered here, we will add unit conversions for other quantities such as Area, Volume/Capacity, Work/Energy, Pressure and others.

The solver page may be accessed at http://extreme.adorio-research.org/solvers/units/.

The Python code units.py which is called by the above QP interface code above is at http://adorio-research.org/wordpress/?p=10377.

Our reference to conversion values is http://physics.nist.gov/Pubs/SP811/appenB9.html#LENGTH
for length units. The Mass and Time units are similarly obtained from NIST.

1 comment: