data structure design question

Moshe Zadka moshez at math.huji.ac.il
Sat Feb 12 01:07:06 EST 2000


On Fri, 11 Feb 2000, Felix Thibault wrote:

> I'm thinking of trying to do some chemistry stuff in 
> Python, and one of the things I was thinking of was
> to define a Chemical (or Molecule) object. On paper
> chemicals are represented by stuctures like:
> 
> H
> |  
> C=O
> |
> H
> 
> so I thought I could initialize instances with some values
> that mimic what we draw. All I've come up with so far is either
> 
> #make instances of the constituent elements
> h1, h2, c, o = H(), H(), C(), O()

Hmmm...why not have a generic Atom:

h1 = Atom('H', 1)

(actually, the 1 is superfluous: you can have a table mapping name to
number of electrons on the outer level (is that it?))

> #initialize with a dictionary of who's connected to who
> formaldehyde = Chemical({h1:[c], h2:[c], c:[h1, h2, o, o], o:[c, c]})

I think this solution is great.

Just have a 

Chemical.add_atom(atom, connected_to)
Chemical.remove_atom(atom)

Raise a

ChemistryError (expolosion <wink>) if you need more connections 
then you have.

Have an is_stable() method, if all connections are full....

Seems like a great way!

(Note: anything connected to chemistry knowledge is 11 years old, so 
take it with a grain of salt)

> The dictionary method seems more condensed and readable, so I
> am preferring it right now, even though I would have to make a
> new instance for every atom of an element

That's a good thing. Every atom *is* a new instance of the atom class.

--
Moshe Zadka <mzadka at geocities.com>. 
INTERNET: Learn what you know.
Share what you don't.





More information about the Python-list mailing list