Skip to content

Instantly share code, notes, and snippets.

@mrogaski
Created January 3, 2014 16:02
Show Gist options
  • Save mrogaski/8240401 to your computer and use it in GitHub Desktop.
Save mrogaski/8240401 to your computer and use it in GitHub Desktop.
Animal Power Converter
#!/opt/bin/python
#
# $Id: power.py,v 1.1 2002/02/15 07:42:30 wendigo Exp $
#
import sys
import os
import cgi
import types
#
# dictionary of animal energy usage
#
adata = {
'mouse' : 10.0,
'hamster' : 40.0,
'rat' : 45.0,
'cat' : 200.0,
'rabbit' : 560.0,
'dog' : 1500.0,
'goat' : 1350.0,
'human' : 2250.0,
'gorilla' : 3000.0,
'lion' : 6000.0,
'cow' : 15000.0,
'horse' : 22500.0,
'giraffe' : 39000.0,
'elephant' : 107000.0,
'dolphin' : 13125.0
}
thisurl = os.environ['SCRIPT_NAME']
#
# conversion function
#
def convert(val, cur, new, db=adata):
"Convert a power value from one animal to another"
try:
fval = float(val)
except ValueError, x:
return -1
return fval * (adata[cur] / adata[new])
#
# display functions
#
def print_header():
print "Content-type: text/html"
print
def print_top():
print """
<HTML>
<HEAD>
<TITLE>Wacky Animal Power Converter</TITLE>
<LINK HREF="http://www.pobox.com/~wendigo/style/default.css"
REL="stylesheet" TYPE="text/css" MEDIA="screen">
</HEAD>
<BODY>
<H1>Animal Power Converter</H1>
<HR>
"""
def print_bottom():
print """
<HR>
<P CLASS="footer">
&copy; 2002 Mark Rogaski
<A HREF="mailto:wendigo@pobox.com">wendigo@pobox.com</A> <BR>
Most recently updated: $Date: 2002/02/15 07:42:30 $
</P>
</BODY>
"""
def print_question(db=adata):
animals = db.keys()
animals.sort()
print '<FORM ACTION="' + thisurl + '" METHOD=POST>'
print """
<P>When we describe the power that is available from a gasoline or
electric engine, we usually describe it in terms of <EM>horsepower</EM>.
Have you ever wondered why? Well, I'm going to tell you anyway. The man
who came up with the term, James Watt, was working with ponies that were
used to lift coal from coal mines. Well that's fine if you happen to be
familiar with ponies.</P>
<P>However, if you're not living on a farm, a fan of
OTB, or a member of London.pm; you probably don't have a good feel for
exactly how much power 1 horsepower is. But, I bet you have a cat ... or
a dog ... or a hamster ... or a gorilla! If you could specify the amount
of power available in terms of another animal, such a measure would be
much more meaningful.</P>
<P>That's where this tool comes in handy. Just fill in the power rating
of you favorite motor, the animal that the rating is based upon, and the
animal you want to state the rating in terms of, then click
"Submit"</P>
<HR>
<INPUT TYPE=HIDDEN NAME="convert" VALUE="1">
<TABLE CELLSPACING=5 CELLPADDING=10>
<TR>
<TD>Power Rating:<BR><INPUT TYPE=TEXT NAME="power"></TD>
<TD>From:<BR>
<SELECT NAME="from">
"""
for ndx in animals:
if ndx == "horse":
print " " * 24 + "<OPTION SELECTED>" + ndx + "</OPTION>"
else:
print " " * 24 + "<OPTION>" + ndx + "</OPTION>"
print """
</SELECT>
</TD>
<TD>To:<BR>
<SELECT NAME="to">
"""
for ndx in animals:
if ndx == "cat":
print " " * 24 + "<OPTION SELECTED>" + ndx + "</OPTION>"
else:
print " " * 24 + "<OPTION>" + ndx + "</OPTION>"
print """
</SELECT>
</TD>
<TD><INPUT TYPE=SUBMIT></TD>
<TD><INPUT TYPE=RESET></TD>
</TR>
</TABLE>
</FORM>
"""
def print_result(val0, val1, tag0="foo", tag1="bar", url=thisurl):
print '<P>Your answer is:</P>'
print '<TABLE ALIGN=CENTER CELLPADING=10 CELLSPACING=30>'
print '<TR><TD ALIGN=CENTER><FONT SIZE="+3">'
print round(float(val0), 2), tag0 + "power", "is equivalent to"
print round(float(val1), 2), tag1 + "power."
print '</FONT></TD></TR>'
print '<TR><TD ALIGN=CENTER><FORM ACTION="' + url + '" METHOD=GET>'
print '<INPUT TYPE=SUBMIT VALUE="New Query"></FORM></TD></TR></TABLE>'
def print_error(errstr, url=thisurl):
print '<P>An error has occurred:</P>'
print '<TABLE ALIGN=CENTER CELLPADING=10 CELLSPACING=30>'
print '<TR><TD ALIGN=CENTER>', errstr, '</TD></TR>'
print '<TR><TD ALIGN=CENTER><FORM ACTION="' + url + '" METHOD=GET>'
print '<INPUT TYPE=SUBMIT VALUE="Try Again"></FORM></TD></TR></TABLE>'
#
# main line code
#
print_header()
print_top()
form = cgi.FieldStorage()
if not form.has_key('convert'):
print_question()
else:
newpower = convert(form['power'].value, form['from'].value,
form['to'].value)
if newpower < 0:
print_error("unable to convert power: " + form['power'].value)
else:
print_result(form['power'].value, newpower,
form['from'].value, form['to'].value)
print_bottom()
@mrogaski
Copy link
Author

mrogaski commented Jan 3, 2014

This was my first Python program.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment