I like to create a structure in Python, something like:

solvent_name
boiling_point
melting_point
flash_point

I need to search and sort this structure. How can I best do that?

Recommended Answers

All 5 Replies

This is a perfect application for a class! I took the Python snippet at:
http://www.daniweb.com/code/snippet390.html
and modified it slightly for the solvent database application:

# example of a small solvent database using a list of class instances

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):
        self.name = name
        self.bp = bp     # boiling point degC
        self.mp = mp     # melting point degC
        self.fp = fp     # flash point degC


def table(solvent_list):
    """print a table of all solvent attributes"""
    print "-"*50
    print "%-20s  %8s  %8s  %8s" % ("Name", "Bp", "Mp", "Fp")
    for solvent in solvent_list:
        print "%-20s  %8.1f  %8.1f  %8.1f" % (solvent.name, solvent.bp, solvent.mp, solvent.fp)
    print "-"*50
    print "9999 --> not applicable"
    print

def table_bp(solvent_list, bp_limit):
    """print a table of all solvent attributes, with bp restrictions"""
    print "-"*50
    print "%-20s  %8s  %8s  %8s" % ("Name", "Bp", "Mp", "Fp")
    for solvent in solvent_list:
        if solvent.bp > bp_limit:
            print "%-20s  %8.1f  %8.1f  %8.1f" % (solvent.name, solvent.bp, solvent.mp, solvent.fp)
    print "-"*50
    print "9999 --> not applicable"
    print

# make a list of class Solvent instances
# data order = name, boiling point, melting point, flash point
# 9999 --> not applicable
solvent_list = []
solvent_list.append(Solvent("methanol", 65, -98, 11))
solvent_list.append(Solvent("ethanol", 78, -114, 8))
solvent_list.append(Solvent("butanol", 118, -89, 35))
solvent_list.append(Solvent("acetone", 56, -95, -17))
solvent_list.append(Solvent("toluene", 111, -95, 4))
solvent_list.append(Solvent("water", 100, 0, 9999))
# got the drift, add more solvents here ...


print "Show one particular item:"
print solvent_list[1].name         # ethanol

print

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)

Nice job Ene! I figured the snippet would be useful to somebody!

Yeah, thanks a lot Ene! I can really use this in my Chemistry class. I also found your more elaborate example in the Code Snippets. Are you taking chemistry classes too?

Now I can see why people might want to use OOP with Python.

Member Avatar for Mouche

Oh thanks. That gave me a good example of creating a table. I can apply that to one of my current projects.

im doing AS chemistry at 6th form college , its fun

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.