[Tutor] file-like object

Chad Crabtree flaxeater at yahoo.com
Fri Jan 14 18:08:21 CET 2005


Thank you KentBot.  That was what I wanted.

Kent Johnson wrote:

> Best: use the StringIO or cStringIO module instead, this is exactly

> what it is for. If you really need len() you could maybe subclass 
> StringIO to do what you want.
>
> Next best: Use an iterator. Something like this (Warning! not
tested!):
> class _macroString(object):
>     def __init__(self,s):
>         self.macro=s
>         self.list=[ line+'\n' for line in self.macro.split("\n") ]
>     self._iter = iter(self.list)
>     def readline(self):
>         try:
>             return self._iter.next()
>         except StopIteration:
>             return ''
>     def __str__(self):
>         return str(self.list)
>     def __len__(self):
>         return len(self.list)
>
> Note that your implementation of readline will raise IndexError
when 
> there are no more lines which is not correct behaviour.
>
> Kent
>
>



		
__________________________________ 
Do you Yahoo!? 
Take Yahoo! Mail with you! Get it on your mobile phone. 
http://mobile.yahoo.com/maildemo 


More information about the Tutor mailing list