From: richard a. <ric...@ya...> - 2006-12-12 15:29:28
|
--- 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. |