data structure design question

Felix Thibault felixt at dicksonstreet.com
Fri Feb 11 21:18:26 EST 2000


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()
#initialize with a dictionary of who's connected to who
formaldehyde = Chemical({h1:[c], h2:[c], c:[h1, h2, o, o], o:[c, c]})

or else use lists of lists to mimic a connection matrix/table/... :

   HHCO  
H  -010
H  0-10
C  11-2
O  002-

like:

formaldehyde = Chemical([H, H, C, O], [[None, 0, 1, 0],
					     [0, None, 1, 0],
					     [1, 1, None, 2], 
					     [0, 0, 2, None]])

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, but I was wondering
if there was a better way to do it altogether, and if anyone 
had advice or could recommend some books/websites that would help me think
about this kind of problem.

Thanks!

Felix



 





More information about the Python-list mailing list