From: Noel O'B. <bao...@gm...> - 2006-12-13 09:44:59
|
I spoke too soon - after playing around with the OBMolAtomIter class for a while I found that it *is* in working order (from Python's point of view). It does require some wrapping to make it into a real Python iterator though. If you look at the interface files (.i), you can see that Geoff has renamed some methods to good(), deref() and inc(). Using these, it's possible to create a class that behaves as an iterator. (Note that .next() and __iter__() are magic methods that Python uses to create an iterator. Presumably other languages have their own ways of creating iterators.) ================ import openbabel as ob import pybel class Testiter(object): def __init__(self, mol): self.iter = ob.OBMolAtomIter(mol) self.finished = False def __iter__(self): return self def next(self): obAtom = self.iter.deref() self.iter.inc() if not self.finished: if not self.iter.good(): self.finished = True return obAtom else: raise StopIteration mol = pybel.readstring("smi", "CNC").OBMol for atom in Testiter(mol): print atom.GetAtomicMass() ================ I'll move this into the SWIG interface file somehow; I think it's possible to replace the existing OBMolAtomIter class with a Python class of the same name. Hopefully it's also possible to create these classes at runtime (or have a Factory method) as there are now about 10 iterators of similar structure in obiter.h. Regards, Noel On 12/12/06, richard apodaca <ric...@ya...> wrote: > > --- Noel O'Boyle <bao...@gm...> wrote: > > > obiter.h exposes a number of C++ convenience > > functions for iterating > > over atoms, bonds, etc. e.g. OBMolAtomIter iterates > > over the atoms in > > a molecule. > > > > However, these iterator methods don't work very well > > from scripting > > languages (I'm not even sure if it's possible to use > > them as they were > > designed to be used). It's much easier to use the > > built-in iterator > > methods present in the various languages. In Python, > > the easiest way > > to do this is to create a list of the atoms of a > > molecule and then > > iterate over these: > > > > obAtoms = [obMol.GetAtom(i+1) for i in > > range(obMol.NumAtoms())] > > for obAtom in obAtoms: > > # Do something with obAtom > > > > It would be very useful to collect such OpenBabel > > idioms from > > different scripting languages and put them on the > > scripting pages (or > > even include them in the interface files as > > convenience functions). > > Could anyone suggest similar code for > > Perl/Ruby/Java? > > Interesting idea. In Ruby, I'd like to just do > something like this: > > mol.each_atom do |atom| > # do something with atom > end > > And similar methods for other iterable collections. > > There are a few ways to get there. One would be > throught the SWIG interface file or C bridge it > generates. Another would be to just define a Ruby > class with the iterators built-in. > > Another would be through Ruby metaprogramming. This > has the advantage over the other methods that it can > be very concise. It comes with a potential decrease in > speed. > > cheers, > Rich > > > > ____________________________ > Richard Apodaca > Blog: http://depth-first.com > > > > ____________________________________________________________________________________ > Any questions? Get answers on any topic at www.Answers.yahoo.com. Try it now. > > ------------------------------------------------------------------------- > Take Surveys. Earn Cash. Influence the Future of IT > Join SourceForge.net's Techsay panel and you'll get the chance to share your > opinions on IT & business topics through brief surveys - and earn cash > http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV > _______________________________________________ > OpenBabel-scripting mailing list > Ope...@li... > https://lists.sourceforge.net/lists/listinfo/openbabel-scripting > |