[mod_python] Output Filters Redeaux

Deron Meranda deron.meranda at gmail.com
Mon Apr 17 14:44:16 EDT 2006

On 4/17/06, Lee Brown <administrator at leebrown.org> wrote:
> What's warping my noodle is handling the two different exit conditions in a
> Pythonic way; if the filter returns '' you just want to hibernate but if the
> filter returns None you want to wrap up and get out.

This kind of pattern is seen a lot in all kinds of "streams processors".
It's great for cases where you don't want to impose any data
size limits (such as needing to hold the whole document in-memory).

> Kinda like this (given an iterable filter object):
> for streamlet in filter:
>     if streamlet:
>         streambuffer.write(streamlet)
>     else:
>         break
> If streamlet is None:
>     [proceed with your filter thing]
> To me, this does not seem to be much more Pythonic that the previous version
> - you still have to noodle on it a while to see the flow structure.

For an iterator approach to seem better, you first must use it like an
iterator.  What you're doing is essentially collecting all the data in a big
heap and then only doing any processing at the end.  So yes, an iterator
isn't much cleaner.

If instead, you used something like a SAX parser, then you could be
processing the filter content on-the-fly as you read each bit, rather
than waiting until the end of the data.  And that would be more
"pythonic" (I guess).

Now, since you're using the heavyweight XSLT as your XML
transformer, then that may dictate the choices open to you.
Deron Meranda

More information about the Mod_python mailing list