1.1.1.1. Sequential input

 1import sys
 2import os
 3
 4import CDPL.Chem as Chem
 5
 6
 7# function called for each read molecule
 8def procMolecule(mol: Chem.Molecule) -> None: 
 9    print('Processing molecule with {!s} atoms and {!s} bonds'.format(mol.numAtoms, mol.numBonds))
10    
11def main() -> None:
12    if len(sys.argv) < 2:
13        sys.exit('Usage: %s <input mol. file>' % sys.argv[0])
14
15    # create reader for input molecules (format specified by file extension)
16    reader = Chem.MoleculeReader(sys.argv[1]) 
17    
18    # create an instance of the default implementation of the Chem.Molecule interface
19    mol = Chem.BasicMolecule()
20    
21    # read and process molecules one after the other until the end of input has been reached
22    try:
23        while reader.read(mol): 
24            try:
25                procMolecule(mol)
26            except Exception as e:
27                sys.exit('Error: processing of molecule failed: ' + str(e))
28                
29    except Exception as e: # handle exception raised in case of severe read errors
30        sys.exit('Error: reading molecule failed: ' + str(e))
31
32    sys.exit(0)
33        
34if __name__ == '__main__':
35    main()

Download source file