Last active
June 22, 2020 17:03
-
-
Save alvaro-diaz-valenzuela/758bba4c099c8f98b1b47acf0bdcfe2a to your computer and use it in GitHub Desktop.
Permite capturar el output de consola a un str.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Taken from a stackoverflow answer ... not written by me. | |
from io import StringIO | |
import sys | |
class Capturing(list): | |
def __enter__(self): | |
self._stdout = sys.stdout | |
sys.stdout = self._stringio = StringIO() | |
return self | |
def __exit__(self, *args): | |
self.extend(self._stringio.getvalue().splitlines()) | |
del self._stringio # free up some memory | |
sys.stdout = self._stdout |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment