code: def countDirectlyConnected(port: LPort, result: dict) -> int:
      inEdges = port.incomingEdges
      outEdges = port.outgoingEdges
      if port.children:
          ch_cnt = 0
          for ch in port.children:
              ch_cnt += countDirectlyConnected(ch, result)
          return ch_cnt
      elif not inEdges and not outEdges:
          if port.direction == PortType.INPUT:
              if port.originObj is not None:
                  assert not port.originObj.src.drivers, port.originObj
              else:
                  print("Warning", port, "not connected")
          return 0
      else:
          connectedElemCnt = 0
          for e in inEdges:
              connectedElemCnt += len(e.srcs)
              if connectedElemCnt > 1:
                  return 0
          for e in outEdges:
              connectedElemCnt += len(e.dsts)
              if connectedElemCnt > 1:
                  return 0
          if connectedElemCnt != 1:
              return 0
          if inEdges:
              e = inEdges[0]
          else:
              e = outEdges[0]
          if e.srcs[0].name != e.dsts[0].name:
              return 0
          if e.srcs[0] is port:
              p = e.dsts[0].parent
          else:
              p = e.srcs[0].parent
          if not isinstance(p, LNode):
              connections = result.get(p, [])
              connections.append((port, e))
              result[p] = connections
          return 1