Spaces:
Sleeping
Sleeping
File size: 996 Bytes
83d190a |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
import sys
import tempfile
from typing import TextIO
class StdoutWrapper(TextIO):
"""
`sys.stdout` wrapper for both Google Colab and local environment.
"""
def __init__(self) -> None:
self.temp_file = tempfile.NamedTemporaryFile(
mode="w+", delete=False, encoding="utf-8"
)
self.original_stdout = sys.stdout
def write(self, message: str) -> int:
result = self.temp_file.write(message)
self.temp_file.flush()
print(message, end="", file=self.original_stdout)
return result
def flush(self) -> None:
self.temp_file.flush()
def read(self, n: int = -1) -> str:
self.temp_file.seek(0)
return self.temp_file.read(n)
def close(self) -> None:
self.temp_file.close()
def fileno(self) -> int:
return self.temp_file.fileno()
try:
import google.colab # type: ignore
SAFE_STDOUT = StdoutWrapper()
except ImportError:
SAFE_STDOUT = sys.stdout
|