durusmail: quixote-users: htmltext as a string subtype
htmltext as a string subtype
2003-10-31
2003-11-04
Threaded SCGI server
2003-11-04
htmltext as a string subtype
Neil Schemenauer
2003-10-31
On Fri, Oct 31, 2003 at 07:51:55AM -0700, Jonathan Corbet wrote:
> > One way to buy back performance would be to make templates return
> > TemplateIO objects rather than strings or htmltext objects.
>
> I'm not sure I understand how this would work.  Is the idea that literal
> strings would become strings again, and the TemplateIO object, somehow,
> tracks whether each component string is "clean" or not?

No.  The current TemplateIO is a mutable character buffer.  htmltext
data gets copied into it.  At the end of the template a new htmltext
object gets created from the data.  So, for each template, data is
copied in and out of the TemplateIO buffer (at least for the C
implementation).

I'm proposing that the TemplateIO object reference htmltext objects
rather than coping the data.  Templates would return TemplateIO
objects rather than htmltext.  I don't know if I'm explaining it
very well. Perhaps some code would be clearer:

    class TemplateIO:
        def __init__(self):
            self.data = []

        def __iadd__(self, value):
            if value is None:
                pass
            elif isinstance(value, htmltext):
                self.data.append(value)
            elif isinstance(value, TemplateIO):
                self.data.extend(value.data)
            else:
                self.data.append(htmlescape(value))

        def getvalue(self):
            return htmltext('').join(self.data)

The important optimization is the part that handles other TemplateIO
objects using extend().  For the C implementation it might be more
efficient to build a tree rather than always flattening it.

  Neil

reply