code: def set_element(self, index, e):
          r
          if index > len(self._chain):
              raise IndexError("tried to access element %i, but chain has only %i"
                               " elements" % (index, len(self._chain)))
  
          if type(index) is not int:
              raise ValueError(
                  "index is not a integer but '%s'" % str(type(index)))
          # if e is already in chain, we're finished
          if self._chain[index] is e:
              return
  
          # remove current index and its data producer
          replaced = self._chain.pop(index)
          if not replaced.is_reader:
              replaced.data_producer = None
  
          self._chain.insert(index, e)
  
          if index == 0:
              e.data_producer = e
          else:
              # rewire data_producers
              e.data_producer = self._chain[index - 1]
  
          # if e has a successive element, need to set data_producer
          try:
              successor = self._chain[index + 1]
              successor.data_producer = e
          except IndexError:
              pass
  
          # set data_producer for predecessor of e
          # self._chain[max(0, index - 1)].data_producer = self._chain[index]
  
          # since data producer of element after insertion changed, reset its status
          # TODO: make parameterized a property?
          self._chain[index]._estimated = False
  
          return replaced
docs: r""" Replaces a pipeline stage.
  
          Replace an element in chain and return replaced element.