2 cents on file objects... WAS: RE: [Web-SIG] Bill's comments on WSGI draft 1.4

Ian Bicking ianb at colorstudy.com
Thu Sep 2 17:58:47 CEST 2004


Michael C. Neel wrote:
> Well, I've seen alot of back and forth on file objects, write(), etc.  I
> think it's of little issue myself, not that hard to return an interface
> that will support both methods.   Let the programming working on the
> middlware/application decide against the tradeoffs from one method to
> another.
> 
> In the framework I use, I've actually altered it to allow it's context
> object (which is connected to the output stream, among other things) to
> be used as a file object.  The first need for this was to allow me to
> pass the object off to a cvs.writer object, when I then called with the
> result of a DB-API 2.0 fetchall(); and made a "Download as CSV" button
> work in no more than 4 lines of code.  I could also see doing this with
> XML classes for a WSDL/SOAP system.  Really off the wall, you could do
> this with the logging module, and send your logging statments to another
> server.

FWIW, using WSGI I've handled like:

class FakeFile: pass

write = start_response(status, headers)
f = FakeFile()
f.write = write
# now f is my file-like object...


Or, it was suggested:

start_response(status, headers)
lst = []
f = FakeFile()
f.write = lst.append
# use f...
return lst


This way you are missing a couple methods that files typically have 
(writelines I guess); then again, you could add those to FakeFile easily 
enough.  I find it feels a little hackish, but I think it should be 
reliable enough.

-- 
Ian Bicking  /  ianb at colorstudy.com  /  http://blog.ianbicking.org


More information about the Web-SIG mailing list