# Valency of each atom in a molecule with python

Consider ethanol, where each carbon atom has a valency of 4 and oxygen a valency of -2. I would like to determine these values for many compounds given their SMILES or InChI keys. (For the purpose of computing the degree of reduction, which in the case of ethanol is 6)

I would like to find a systematic method to calculate this for many molecules. I attempted to use Open Babel as described below, but I am open to any other programmatic solution.

## Attempt with Open Babel on Python:

I attempted the following with openbabel.pybel (after installing the appropriate packages):

from openbabel import pybel

ethanol = 'CCO'  # SMILES

for atom in mol.atoms:
print(f"type={atom.type} charge={atom.formalcharge} \
degree(valence)={atom.degree} implicitvalence={atom.implicitvalence}")


This returns the error:

AttributeError: 'OBAtom' object has no attribute 'GetImplicitValence'


If I understand correctly the implicit valence is the value I am after. I am stuck and the documentation is not helping (is not even consistent with the behavior of the module) openbabel.__version__ returns 3.0.0. This is based on a 6 year old question

• Possible duplicate of Counting valency of atoms, in a molecule with python – Mithoron Dec 5 '19 at 0:46
• This seems to be some programming issue and therefore off-topic. Closing as dupe is another option. – Mithoron Dec 5 '19 at 0:48
• I'm voting to close this question as off-topic because it's about using software and not about chemistry. – Todd Minehardt Dec 5 '19 at 1:20
• Since this question has been up for a few days but still has no answer, perhaps it might be something that a dedicated computational chemical physics community would be interested in. There is a proposal for a Stack Exchange for Materials Modeling for example: area51.stackexchange.com/proposals/122958/…. Perhaps you might consider supporting it if you think it would be useful for you! – user1271772 Dec 10 '19 at 3:12
• There's no 'implicit valence' in Open Babel 3 because we found it to be a very confusing concept. I think you should ask a more general question outside your specifics with Pybel. – Geoff Hutchison Dec 10 '19 at 16:37