6.2 Looping over the Bonds of an Atom

The exact same idiom is used for iterating over the bonds attached to an atom. The OEAtomBase method GetBonds is a generator method that allows looping over the bonds connected to the atom. The example below shows how to use this method to determine the explicit degree of an atom, i.e. the number of bonds to it, other than implicit hydrogen atoms.

#!/usr/bin/env python
# ch6-2.py
from openeye.oechem import *

def MyGetExplicitDegree(atom):
    result = 0
    for bond in atom.GetBonds():
        result += 1
    return result

mol = OEGraphMol()
OEParseSmiles(mol, "c1ccccc1")

for atom in mol.GetAtoms():
	print MyGetExplicitDegree(atom)