//  [python]
def serialize(self, treewalker, encoding=None):
    self.encoding = encoding
    in_cdata = False
    self.errors = []

    if encoding and self.inject_meta_charset:
        from ..filters.inject_meta_charset import Filter
        treewalker = Filter(treewalker, encoding)
    # WhitespaceFilter should be used before OptionalTagFilter
    # for maximum efficiently of this latter filter
    if self.strip_whitespace:
        from ..filters.whitespace import Filter
        treewalker = Filter(treewalker)
    if self.sanitize:
        from ..filters.sanitizer import Filter
        treewalker = Filter(treewalker)
    if self.omit_optional_tags:
        from ..filters.optionaltags import Filter
        treewalker = Filter(treewalker)
    # Alphabetical attributes must be last, as other filters
    # could add attributes and alter the order
    if self.alphabetical_attributes:
        from ..filters.alphabeticalattributes import Filter
        treewalker = Filter(treewalker)

    for token in treewalker:
        type = token["type"]
        if type == "Doctype":
            doctype = "<!DOCTYPE %s" % token["name"]

            if token["publicId"]:
                doctype += ' PUBLIC "%s"' % token["publicId"]
            elif token["systemId"]:
                doctype += " SYSTEM"
            if token["systemId"]:
                if token["systemId"].find('"') >= 0:
                    if token["systemId"].find("'") >= 0:
                        self.serializeError("System identifer contains both single and double quote characters")
                    quote_char = "'"
                else:
                    quote_char = '"'
                doctype += " %s%s%s" % (quote_char, token["systemId"], quote_char)

            doctype += ">"
            yield self.encodeStrict(doctype)

        elif type in ("Characters", "SpaceCharacters"):
            if type == "SpaceCharacters" or in_cdata:
                if in_cdata and token["data"].find("</") >= 0:
                    self.serializeError("Unexpected </ in CDATA")
                yield self.encode(token["data"])
            else:
                yield self.encode(escape(token["data"]))

        elif type in ("StartTag", "EmptyTag"):
            name = token["name"]
            yield self.encodeStrict("<%s" % name)
            if name in rcdataElements and not self.escape_rcdata:
                in_cdata = True
            elif in_cdata:
                self.serializeError("Unexpected child element of a CDATA element")
            for (attr_namespace, attr_name), attr_value in token["data"].items():
                # TODO: Add namespace support here
                k = attr_name
                v = attr_value
                yield self.encodeStrict(' ')

                yield self.encodeStrict(k)
                if not self.minimize_boolean_attributes or \
                    (k not in booleanAttributes.get(name, tuple()) and
                     k not in booleanAttributes.get("", tuple())):
                    yield self.encodeStrict("=")
                    if self.quote_attr_values == "always" or len(v) == 0:
                        quote_attr = True
                    elif self.quote_attr_values == "spec":
                        quote_attr = quoteAttributeSpec.search(v) is not None
                    elif self.quote_attr_values == "legacy":
                        quote_attr = quoteAttributeLegacy.search(v) is not None
                    else:
                        raise ValueError("quote_attr_values must be one of: "
                                         "'always', 'spec', or 'legacy'")
                    v = v.replace("&", "&amp;")
                    if self.escape_lt_in_attrs:
                        v = v.replace("<", "&lt;")
                    if quote_attr:
                        quote_char = self.quote_char
                        if self.use_best_quote_char:
                            if "'" in v and '"' not in v:
                                quote_char = '"'
                            elif '"' in v and "'" not in v:
                                quote_char = "'"
                        if quote_char == "'":
                            v = v.replace("'", "&#39;")
                        else:
                            v = v.replace('"', "&quot;")
                        yield self.encodeStrict(quote_char)
                        yield self.encode(v)
                        yield self.encodeStrict(quote_char)
                    else:
                        yield self.encode(v)
            if name in voidElements and self.use_trailing_solidus:
                if self.space_before_trailing_solidus:
                    yield self.encodeStrict(" /")
                else:
                    yield self.encodeStrict("/")
            yield self.encode(">")

        elif type == "EndTag":
            name = token["name"]
            if name in rcdataElements:
                in_cdata = False
            elif in_cdata:
                self.serializeError("Unexpected child element of a CDATA element")
            yield self.encodeStrict("</%s>" % name)

        elif type == "Comment":
            data = token["data"]
            if data.find("--") >= 0:
                self.serializeError("Comment contains --")
            yield self.encodeStrict("<!--%s-->" % token["data"])

        elif type == "Entity":
            name = token["name"]
            key = name + ";"
            if key not in entities:
                self.serializeError("Entity %s not recognized" % name)
            if self.resolve_entities and key not in xmlEntities:
                data = entities[key]
            else:
                data = "&%s;" % name
            yield self.encodeStrict(data)

        else:
            self.serializeError(token["data"])