code: def toposort(initialAtoms, initialBonds):
      atoms = []
      a_append = atoms.append
      bonds = []
      b_append = bonds.append
      ahash = {}
      bhash = {}
      for atom in initialAtoms[1:]:
          ahash[atom.handle] = 1
      for bond in initialBonds:
          bhash[bond.handle] = bond
      next = initialAtoms[0]
      a_append(next)
      while ahash:
          for atom in next.oatoms:
              if ahash.has_key(atom.handle):
                  bond = next.findbond(atom)
                  assert bond
                  if bhash.has_key(bond.handle):
                      a_append(atom)
                      b_append(bond)
                      del ahash[atom.handle]
                      next = atom
                      break
          else:
              raise RingException("Atoms are not in ring")
      assert len(initialAtoms) == len(atoms)
      assert len(bonds) == len(atoms) - 1
      lastBond = atoms[0].findbond(atoms[-1])
      assert lastBond
      b_append(lastBond)
      return atoms, bonds