[Tutor] StringIO

Kent Johnson kent37 at tds.net
Tue May 23 11:59:11 CEST 2006


kakada wrote:
> Actually, what I want is creating a kind of file that is just located in
> memory not file system.
> I want to have a filename for my convert(inputFileName, outputFileName)
> How can I get filename?
> doing:
> fin.name() is impossible!

Rewrite convert so it takes file-like objects as arguments rather than 
file names. Then you can pass StringIO objects for input and output in 
your unittests.

Alternately you might want to write convert so it can take *either* a 
file name or an open file as an argument. Here is a snippet from a 
function that does that (ElementTree.parse()):
     def parse(self, source, parser=None):
         if not hasattr(source, "read"):
             source = open(source, "rb")

> All above, I want to test it in unittest.

Good idea!

Kent



More information about the Tutor mailing list