code: def countDirectlyConnected(port: LPort, result: dict) -> int:
      
      inEdges = port.incomingEdges
      outEdges = port.outgoingEdges
  
      if port.children:
          ch_cnt = 0
          # try:
          #    assert not inEdges, (port, port.children, inEdges)
          #    assert not outEdges, (port, port.children, outEdges)
          # except AssertionError:
          #    raise
          for ch in port.children:
              ch_cnt += countDirectlyConnected(ch, result)
  
          return ch_cnt
  
      elif not inEdges and not outEdges:
          # this port is not connected, just check if it expected state
          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 is connected to different port
          if e.srcs[0].name != e.dsts[0].name:
              return 0
  
          if e.srcs[0] is port:
              p = e.dsts[0].parent
          else:
              # (can be hyperedge and then this does not have to be)
              # assert e.dsts[0] is port, (e, port)
              p = e.srcs[0].parent
  
          # if is part of interface which can be reduced
          if not isinstance(p, LNode):
              connections = result.get(p, [])
              connections.append((port, e))
              result[p] = connections
  
          return 1
docs: Count how many ports are directly connected to other nodes
  
      :return: cumulative sum of port counts