@defer.inlineCallbacks
    def write(self, data):
        """Write data to the file. There is no return value.

        `data` can be either a string of bytes or a file-like object
        (implementing :meth:`read`).

        Due to buffering, the data may not actually be written to the
        database until the :meth:`close` method is called. Raises
        :class:`ValueError` if this file is already closed. Raises
        :class:`TypeError` if `data` is not an instance of
        :class:`str` or a file-like object.

        :Parameters:
          - `data`: string of bytes or file-like object to be written
            to the file
        """
        if self._closed:
            raise ValueError("TxMongo: cannot write to a closed file.")

        try:
            # file-like
            read = data.read
        except AttributeError:
            # string
            if not isinstance(data, (bytes, unicode)):
                raise TypeError("TxMongo: can only write strings or file-like objects.")
            if isinstance(data, unicode):
                try:
                    data = data.encode(self.encoding)
                except __HOLE__:
                    raise TypeError("TxMongo: must specify an encoding for file in "
                                    "order to write {0}".format(data))
            read = StringIO(data).read

        if self._buffer.tell() > 0:
            # Make sure to flush only when _buffer is complete
            space = self.chunk_size - self._buffer.tell()
            if space:
                to_write = read(space)
                self._buffer.write(to_write)
                if len(to_write) < space:
                    return  # EOF or incomplete
            yield self.__flush_buffer()
        to_write = read(self.chunk_size)
        while to_write and len(to_write) == self.chunk_size:
            yield self.__flush_data(to_write)
            to_write = read(self.chunk_size)
        self._buffer.write(to_write)
dataset/ETHPy150Open twisted/txmongo/txmongo/_gridfs/grid_file.py/GridIn.write