def getConnectedElems(pyObj, alreadyVisited, models):
  # Return all table instances in which the current 'pyObj' is a foreign
  # key. These can normally be accessed by the table names in 
  #  lower_case <with> _set suffixed, eg pyObj.comment_set
  if not pyObj:
     return None

  objsets = [attr for attr in dir(pyObj) if setSuffixCompile.search(attr)]

  setElemsDict = dict()
  for objset in objsets:
    connElems = getattr(pyObj, objset, None)

    if not isCallableAttr(connElems, 'all'): # Or handle this miss as an err
       continue

    objList = list()
    for connElem in connElems.all():
      serializDict = getSerializableElems(connElem)
      if not serializDict:
        continue

      objHash = hash(pyObj)
      if objHash in alreadyVisited:
        # TODO: Show the cyclic dependency by
        # passing back a serializable sentinel
        continue

      alreadyVisited[objHash] = objHash

      connConnElems = getForeignKeyElems(connElem, alreadyVisited, models) 
      refObjs = getConnectedElems(connElem, alreadyVisited, models)

      if refObjs:
        serializDict.update(refObjs)

      if connConnElems:
        serializDict.update(connConnElems)

      objList.append(serializDict)

    setElemsDict[objset] = objList
    
  return setElemsDict
dataset/ETHPy150Open odeke-em/restAssured/api/crudAPI.py/getConnectedElems/original