I would like to do the following:
import StringIO, uu
my_data = StringIO.StringIO() # this is a file-like object
uu.encode(in_file, my_data)
# do stuff with my data (send over network)
uu.decode(my_data, out_file) # here I finally write to disk
The above code works. However, if I implement the previous step as a property in an object:
@property
def content(self):
out = StringIO.StringIO()
uu.decode(self._content, out)
return out.getvalue()
@content.setter
def content(self, value):
self._content = StringIO.StringIO()
with open('value', 'rb') as stream:
uu.encode(stream, self._content)
but when I do it like that, self._content is empty (None, to be precise). Any ideas?
self._contentis left with the “current point” at its end after thecontent.settermethod has written to it. You probably want to addself._content.seek(0)at the end of that method so you can next read that pseudo-file from the beginning (reading while starting from the end will return “nothing more”, quite correctly since it does start at the end, and that’s probably what’s leaving you with the impression that it’s “empty”;-).