~narvenblog/weco/devel

« back to all changes in this revision

Viewing changes to main.py

  • Committer: Narven
  • Date: 2009-03-28 20:59:32 UTC
  • Revision ID: narven@bigone-20090328205932-hmr09z0amv25r1xk
create molecule, atom and bond classes

Show diffs side-by-side

added added

removed removed

Lines of Context:
16
16
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
17
17
"""
18
18
 
19
 
 
20
 
print "bla"
21
 
 
22
 
print "bla2"
 
 
b'\\ No newline at end of file'
 
19
class Atom(object):
 
20
    """class to store info about an atom"""
 
21
    def __init__(self, id=0, symbol="", pos=[0,0,0]):
 
22
        self.id = id
 
23
        self.symbol = symbol
 
24
        self.pos = pos
 
25
        
 
26
class Bond(object):     
 
27
    " class to store info about a bond """
 
28
    def __init__(self, id=0, atom_id_1=0, atom_id_2=0):
 
29
        self.id = id
 
30
        self.atom_id_1 = atom_id_1
 
31
        self.atom_id_2 = atom_id_2
 
32
        
 
33
class Molecule(object):
 
34
    """ class to store info about a molecule"""
 
35
    def __init__(self, id=0, name="", atoms={}, bonds={}):
 
36
        self.id = id
 
37
        self.name = name
 
38
        self.atoms = atoms
 
39
        self.bonds = bonds
 
40
        
 
41
if __name__ == '__main__':
 
42
    # code to test the atom, bond and molecule classes
 
43
    
 
44
    # create a molecule and give it some data
 
45
    m = Molecule(id=1, name="Water")
 
46
    
 
47
    # add 3 atoms to the molecule
 
48
    m.atoms[1] = Atom(id=1, symbol="O", pos=[1,2,3])
 
49
    m.atoms[2] = Atom(id=2, symbol="H", pos=[4,2,3])
 
50
    m.atoms[3] = Atom(id=3, symbol="H", pos=[1,5,6])
 
51
    
 
52
    # create 2 bonds
 
53
    m.bonds[1] = Bond(id=1, atom_id_1=1, atom_id_2=2)
 
54
    m.bonds[2] = Bond(id=2, atom_id_1=1, atom_id_2=3)
 
55
    
 
56
    # retrive the molecule data
 
57
    print "Molecule name is: %s" % m.name
 
58
    
 
59
    for key in m.atoms.keys():
 
60
        print "Atom #%s is %s" % (key, m.atoms[key].symbol)
 
61
        
 
62
    for key in m.bonds.keys():
 
63
        print "Bond #%s is bounded to atom %s and atom %s" % (key, m.bonds[key].atom_id_1, m.bonds[key].atom_id_2)
 
64
    
 
65
    
 
 
b'\\ No newline at end of file'