def encode(iterator, method='xml', encoding=None, out=None):
    'Encode serializer output into a string.\n    \n    :param iterator: the iterator returned from serializing a stream (basically\n                     any iterator that yields unicode objects)\n    :param method: the serialization method; determines how characters not\n                   representable in the specified encoding are treated\n    :param encoding: how the output string should be encoded; if set to `None`,\n                     this method returns a `unicode` object\n    :param out: a file-like object that the output should be written to\n                instead of being returned as one big string; note that if\n                this is a file or socket (or similar), the `encoding` must\n                not be `None` (that is, the output must be encoded)\n    :return: a `str` or `unicode` object (depending on the `encoding`\n             parameter), or `None` if the `out` parameter is provided\n    \n    :since: version 0.4.1\n    :note: Changed in 0.5: added the `out` parameter\n    '
    if (encoding is not None):
        errors = 'replace'
        if ((method != 'text') and (not isinstance(method, TextSerializer))):
            errors = 'xmlcharrefreplace'
        _encode = (lambda string: string.encode(encoding, errors))
    else:
        _encode = (lambda string: string)
    if (out is None):
        return _encode(''.join(list(iterator)))
    for chunk in iterator:
        out.write(_encode(chunk))
Encode serializer output into a string.

:param iterator: the iterator returned from serializing a stream (basically
                 any iterator that yields unicode objects)
:param method: the serialization method; determines how characters not
               representable in the specified encoding are treated
:param encoding: how the output string should be encoded; if set to `None`,
                 this method returns a `unicode` object
:param out: a file-like object that the output should be written to
            instead of being returned as one big string; note that if
            this is a file or socket (or similar), the `encoding` must
            not be `None` (that is, the output must be encoded)
:return: a `str` or `unicode` object (depending on the `encoding`
         parameter), or `None` if the `out` parameter is provided

:since: version 0.4.1
:note: Changed in 0.5: added the `out` parameter
def encode(iterator, method='xml', encoding=None, out=None):
    'Encode serializer output into a string.\n    \n    :param iterator: the iterator returned from serializing a stream (basically\n                     any iterator that yields unicode objects)\n    :param method: the serialization method; determines how characters not\n                   representable in the specified encoding are treated\n    :param encoding: how the output string should be encoded; if set to `None`,\n                     this method returns a `unicode` object\n    :param out: a file-like object that the output should be written to\n                instead of being returned as one big string; note that if\n                this is a file or socket (or similar), the `encoding` must\n                not be `None` (that is, the output must be encoded)\n    :return: a `str` or `unicode` object (depending on the `encoding`\n             parameter), or `None` if the `out` parameter is provided\n    \n    :since: version 0.4.1\n    :note: Changed in 0.5: added the `out` parameter\n    '
    if (encoding is not None):
        errors = 'replace'
        if ((method != 'text') and (not isinstance(method, TextSerializer))):
            errors = 'xmlcharrefreplace'
        _encode = (lambda string: string.encode(encoding, errors))
    else:
        _encode = (lambda string: string)
    if (out is None):
        return _encode(.join(list(iterator)))
    for chunk in iterator:
        out.write(_encode(chunk))