954,549 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Create a Structure

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?

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

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:
[php]# 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)
[/php]

Ene Uran
Posting Virtuoso
1,723 posts since Aug 2005
Reputation Points: 625
Solved Threads: 213
 

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

vegaseat
DaniWeb's Hypocrite
Moderator
5,989 posts since Oct 2004
Reputation Points: 1,345
Solved Threads: 1,417
 

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.

sneekula
Nearly a Posting Maven
2,427 posts since Oct 2006
Reputation Points: 961
Solved Threads: 212
 

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

LaMouche
Posting Whiz in Training
269 posts since Oct 2006
Reputation Points: 83
Solved Threads: 39
 

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

jbennet
Moderator
Moderator
18,523 posts since Apr 2005
Reputation Points: 1,826
Solved Threads: 601
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You