| Lee Brown 
    administrator at leebrown.org Mon Apr 17 13:04:45 EDT 2006 
 That's a good question.  I don't know if the filter object is iterable or
not.
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.  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.
Best Regards,
Lee E. Brown
(administrator at leebrown.org)
-----Original Message-----
From: mod_python-bounces at modpython.org
[mailto:mod_python-bounces at modpython.org] On Behalf Of Nick
Sent: Monday, April 17, 2006 12:43 PM
To: Lee Brown
Cc: mod_python at modpython.org
Subject: Re: [mod_python] Output Filters Redeaux
Great example!  Thanks.
On a devel note, it looks like the filter object would be very handy if it
could be treated as in iterator, such as (using the sample code's
nomenclature):
for streamlet in filter:
  [do something with streamlet here]
It would definitely turn filter writing into something more Pythonic.  Does
that seem like something that would be useful?
Thanks,
Nick
Lee Brown wrote:
> 
> 
> Greetings!
> 
> Just for fun, here is a handy template for constructing Mod Python 
> output filters:
> 
>     from mod_python import apache
>     from cStringIO import StringIO
> 
>     def outputfilter (filter):
>            
>         try:
>             streambuffer = filter.req.streambuffer
>         except AttributeError:
>             filter.req.streambuffer = StringIO() # See Note 1
>             streambuffer = filter.req.streambuffer
>                 # See Note 2
>        
>         streamlet = filter.read()
>         while streamlet:
>                 # See Note 3
>             streambuffer.write(streamlet)
>             streamlet = filter.read()
> 
>         if streamlet is None:
>                 # See Note 4
>             filter.write(streambuffer)
>             filter.close()
> 
> This is as stripped-down, bare-bones as a filter can get, IMHO.  Of 
> course, this is just an echo filter until you add your own code to it.
> 
> Note 1:
> 
> When a filter is invoked once or only a few times, there is no real 
> advantage in using a cStringIO buffer over a Python List buffer.
> 
> As the number of re-invocations goes up, cStringIO starts to show a 
> speed advantage.
> 
> Note 2:
> 
> Anything else that you may want to initialize at the start of a 
> request should go here, ex:
>      filter.req.some_variable = some_value
> 
> Note 3:
> 
> Depending on your application, you may want to hack at the raw stream 
> before putting it in the buffer, ex:
>     streambuffer.write(streamlet.replace('\r\n', '\n'))
> 
> Note 4:
> 
> At this point, streambuffer contains the entire request response and 
> you may go ahead and do whatever it is you wanted to accomplish, ex:
> 
>     filter.write(my_tranform_function(streambuffer))
> 
> 
> 
> Best Regards,
> Lee E. Brown
> (leebrown at leebrown.org)
> 
> 
> ----------------------------------------------------------------------
> --
> 
> _______________________________________________
> Mod_python mailing list
> Mod_python at modpython.org
> http://mailman.modpython.org/mailman/listinfo/mod_python
_______________________________________________
Mod_python mailing list
Mod_python at modpython.org
http://mailman.modpython.org/mailman/listinfo/mod_python
 |