I have created an Atom object as follows:
class Atom(object):
def __init__(self, symbol, x, y, z)
self.symbol = symbol
self.position = (x, y, z)
and a Selection
class that contains atoms selected by some criteria:
class Selection(object):
def __init__(self, a_system, atom_list=[]):
for atom in a_system:
atom_list.append(atom)
self.atom_list = atom_list
def by_symbol(self, symbol):
r_list = []
for atom in self.atom_list:
if atom.symbol is symbol:
r_list.append(atom)
self.atom_list = r_list
def by_zrange(self, zmin, zmax):
r_list = []
for atom in self.atom_list:
pos = atom.position[2]
if pos > zmin and pos < zmax:
r_list.append(atom)
self.atom_list = r_list
So as you can see I can say for example:
# my_system is a list of atoms objects
group = Selection(my_system)
and then say:
group.by_symbol('H')
and I'll have in object group
all hydrogen atoms. Then if I do:
group.by_zrange(1, 2)
and I'll have in object group
all hydrogen atoms which have z-coordinate between 1 and 2.
I have other selections criteria but in general they have all the same structure, to know:
r_list = []
for atom in self.atom_list:
# Some criteria here
r_list.append(atom)
self.atom_list = r_list
So the question is: is there something I can do in order to avoid writing the above structure for each selection criteria?
If you know there is a simpler way to accomplish my purpose I'll be glad to hear it.