<SYSTEM_TASK:>
Solve the following problem using Python, implementing the functions described below, one line at a time
<END_TASK>
<USER_TASK:>
Description:
 def write_index(self, fh, to_str_func=str, generate=True, verbose=False):
 """ Write this index to a file. Only the index dictionary itself is stored, no informatiom about the indexed file, or the open filehandle is retained. The Output format is just a tab-separated file, one record per line. The last column is the file location for the record and all columns before that are collectively considered to be the hash key for that record (which is probably only 1 column, but this allows us to permit tabs in hash keys). :param fh: either a string filename or a stream-like object to write to. :param to_str_func: a function to convert hash values to strings. We'll just use str() if this isn't provided. :param generate: build the full index from the indexed file if it hasn't already been built. This is the default, and almost certainly what you want, otherwise just the part of the index already constructed is written :param verbose: if True, output progress messages to stderr. """

    try:
      handle = open(fh, "w")
    except TypeError:
      # okay, not a filename, try to treat it as a stream to write to.
      handle = fh
    if generate:
      self.__build_index(verbose=verbose)
    for key in self._index:
      handle.write(to_str_func(key) + "\t" + str(self._index[key]) + "\n")