def write(self, chunk, serialize=False, format=None):
<EOL>self.require_not_closed()<EOL>if chunk is None:<EOL><INDENT>return<EOL><DEDENT>if serialize or format is not None:<EOL><INDENT>self.serialize(chunk, format=format)<EOL>return  <EOL><DEDENT>if type(chunk) is six.binary_type:<EOL><INDENT>self._length += len(chunk)<EOL>self._stream.write(chunk)<EOL><DEDENT>elif isinstance(chunk, six.string_types):<EOL><INDENT>encoding = self.encoding<EOL>if encoding is not None:<EOL><INDENT>chunk = chunk.encode(encoding)<EOL><DEDENT>else:<EOL><INDENT>raise exceptions.InvalidOperation(<EOL>'<STR_LIT>')<EOL><DEDENT>self._length += len(chunk)<EOL>self._stream.write(chunk)<EOL><DEDENT>elif isinstance(chunk, collections.Iterable):<EOL><INDENT>for section in chunk:<EOL><INDENT>self.write(section)<EOL><DEDENT><DEDENT>else:<EOL><INDENT>raise exceptions.InvalidOperation(<EOL>'<STR_LIT>')<EOL><DEDENT>
Writes the given chunk to the output buffer.

        @param[in] chunk
            Either a byte array, a unicode string, or a generator. If `chunk`
            is a generator then calling `self.write(<generator>)` is
            equivalent to:

            @code
                for x in <generator>:
                    self.write(x)
                    self.flush()
            @endcode

        @param[in] serialize
            True to serialize the lines in a determined serializer.

        @param[in] format
            A specific format to serialize in; if provided, no detection is
            done. If not provided, the accept header (as well as the URL
            extension) is looked at to determine an appropriate serializer.