StdoutWrapper
ClassPrivateContextSource
with syncstream.webtools.StdoutWrapper():
...
A wrapper for ensuring that the stdout is always directed to the same position.
danger
This class was designed from scratch. However, its functionality has been fully covered
by contextlib.redirect_stdout(...)
and
contextlib.redirect_stdout(...)
. Therefore,
users should not use this context in any case.
This document is only used for archiving what was done in this implementation.
Arguments
No argument is needed.
Example
Redirect stdout to buffer
- Codes
- Better codes
- Results
use_stdout_context.py
import sys
import syncstream
from syncstream.webtools import StdoutWrapper
buffer = syncstream.LineBuffer(10)
with StdoutWrapper():
sys.stdout = buffer
for i in range(20):
print(f'Message "{i:02d}".')
print('No line break.', end='')
messages = buffer.read()
for mitem in messages:
print(mitem)
use_contextlib.py
from contextlib import redirect_stdout
import syncstream
buffer = syncstream.LineBuffer(10)
with redirect_stdout(buffer):
for i in range(20):
print(f'Message "{i:02d}".')
print('No line break.', end='')
messages = buffer.read()
for mitem in messages:
print(mitem)
Message "11".
Message "12".
Message "13".
Message "14".
Message "15".
Message "16".
Message "17".
Message "18".
Message "19".
No line break.