-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmolecule.py
More file actions
executable file
·62 lines (56 loc) · 1.82 KB
/
molecule.py
File metadata and controls
executable file
·62 lines (56 loc) · 1.82 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
from atom import Atom
#START:init
class Molecule(object):
'''A molecule with a name and a list of Atoms.'''
def __init__(self, name):
'''Create a Compound named name with no Atoms.'''
self.name = name
self.atoms = []
def add(self, a):
'''Add Atom a to my list of Atoms.'''
self.atoms.append(a)
#END:init
#START:translate
def translate(self, x, y, z):
'''Move this Compound, including all Atoms, by (x, y, z).'''
for atom in self.atoms:
atom.translate(x, y, z)
#END:translate
#START:str
def __repr__(self):
'''Return a string representation of this Molecule in this format:
Molecule("NAME", (ATOM1, ATOM2, ...))
'''
res = ''
for atom in self.atoms:
res = res + repr(atom) + ', '
# Strip off the last comma.
res = res[:-2]
return 'Molecule("%s", (%s))' % (self.name, res)
def __str__(self):
'''Return a string representation of this Molecule in this format:
(NAME, (ATOM1, ATOM2, ...))
'''
res = ''
for atom in self.atoms:
res = res + str(atom) + ', '
# Strip off the last comma.
res = res[:-2]
return '(%s, (%s))' % (self.name, res)
#END:str
if __name__ == '__main__':
#START:ammonia
ammonia = Molecule("AMMONIA")
ammonia.add(Atom(1, "N", 0.257, -0.363, 0.0))
ammonia.add(Atom(2, "H", 0.257, 0.727, 0.0))
ammonia.add(Atom(3, "H", 0.771, -0.727, 0.890))
ammonia.add(Atom(4, "H", 0.771, -0.727, -0.890))
ammonia.translate(0, 0, 0.2)
#END:ammonia
#START:test
assert ammonia.atoms[0].center[0] == 0.257
assert ammonia.atoms[0].center[1] == -0.363
assert ammonia.atoms[0].center[2] == 0.2
#END:test
print repr(ammonia)
print ammonia