<ma***********@hotmail.comescribió:
I have a thread that I've created from a main program. I started thisReplace sys.stdout with an object that stores the lines printed. (Due to
thread and passed it a function to execute. Within this function are
'print' statements. While they are directly translated to the stdout, I
would love to return them back to the program itself and store them in
an object. How would I do this?
the way the print statement works, you should not inherit from file)
class PrintBuffer:
def __init__(self, stream):
self._stream = stream
self.output = []
def write(self, text):
self.output.append(text)
self._stream.write(text)
def __getattr__(self, name):
return getattr(self._stream, name)
pyimport sys
pysys.stdout = PrintBuffer(sys.stdout)
pyprint "Hello world!"
Hello world!
pyprint 2,"*",3,"=",2*3
2 * 3 = 6
pyprint >>sys.stderr, sys.stdout.output
['Hello world!', '\n', '2', ' ', '*', ' ', '3', ' ', '=', ' ', '6', '\n']
py>
--
Gabriel Genellina