The snippet shows the use of a list of class instances to form a sortable and searchable database for solvents, or for that matter other chemicals. The list can be expanded and all the needed data added at the same time.
A Simple Solvent Database
# a solvent database using a list of class instances
# a modification of: http://www.daniweb.com/code/snippet390.html
# EU tested with Python 2.4 10/30/2006
import operator # for attrgetter()
class Solvent(object):
"""a structure class for solvents, self refers to the instance"""
def __init__(self, name=9999, bp=9999, mp=9999, fp=9999, dy=9999, de=9999):
self.name = name
# 9999 --> not applicable
self.bp = bp # boiling point degC
self.mp = mp # melting point degC
self.fp = fp # flash point degC
self.dy = dy # density g/ml
self.de = de # dielectric constant
def table(solvent_list):
"""print a table of all solvent attributes"""
title_str = "%-20s %8s %8s %8s %8s %8s"
data_str = "%-20s %8.1f %8.1f %8.1f %8.3f %8.2f"
print "-"*72
print title_str % ("Name", "Bp", "Mp", "Fp", "Density", "Dielectric")
for solvent in solvent_list:
print data_str % (solvent.name, solvent.bp, solvent.mp, solvent.fp, solvent.dy, solvent.de)
print "-"*72
print "9999 --> not applicable"
print
def table_bp(solvent_list, bp_limit):
"""print a table of all solvent attributes, with bp restrictions"""
title_str = "%-20s %8s %8s %8s %8s %8s"
data_str = "%-20s %8.1f %8.1f %8.1f %8.3f %8.2f"
print "-"*72
print title_str % ("Name", "Bp", "Mp", "Fp", "Density", "Dielectric")
for solvent in solvent_list:
if solvent.bp > bp_limit:
print data_str % (solvent.name, solvent.bp, solvent.mp, solvent.fp, solvent.dy, solvent.de)
print "-"*72
print "9999 --> not applicable"
print
# make a list of class Solvent instances
# also adds all the data/information
# data order = name, boiling point, melting point, flash point, density, dielectric constant
# 9999 --> not applicable
solvent_list = []
solvent_list.append(Solvent("methanol", 64.7, -97.7, 11, 0.791, 32.7))
solvent_list.append(Solvent("ethanol", 78.3, -114.1, 8, 0.789, 24.55))
solvent_list.append(Solvent("propanol iso", 82.3, -88, 22, 0.785, 19.92))
solvent_list.append(Solvent("butanol normal", 117.7, -88.6, 35, 0.81, 17.51))
solvent_list.append(Solvent("butanol secondary", 88.8, -114.7, 26, 0.805, 16.56))
solvent_list.append(Solvent("butanol tertiary", 82.2, 25.5, 4, 0.786, 10.9))
solvent_list.append(Solvent("benzyl alcohol", 205.4, -15.3, 100, 1.045, 13.1))
solvent_list.append(Solvent("acetone", 56.3, -94.7, -17, 0.791, 20.7))
solvent_list.append(Solvent("toluene", 110.6, -94.9, 4, 0.867, 2.38))
solvent_list.append(Solvent("water", 100, 0, 9999, 1, 78.5))
# got the drift, add more solvents here ...
print "Sort the solvent_list by name ..."
solvent_list.sort(key=operator.attrgetter('name'))
# ... now show a table of the solvent_list
table(solvent_list)
print
print "Sort the solvent_list by melting point ..."
solvent_list.sort(key=operator.attrgetter('mp'))
# ... now show a table of the solvent_list
table(solvent_list)
print
print "Sort the solvent_list by boiling point ..."
solvent_list.sort(key=operator.attrgetter('bp'))
# ... now show a table of the solvent_list
table(solvent_list)
print
bp_limit = 75
print "Show only solvents boiling higher than %0.1f degC:" % bp_limit
# show a boiling point restricted table
table_bp(solvent_list, bp_limit)
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.