Skip to content

Commit

Permalink
Inactivate HiddenPrints
Browse files Browse the repository at this point in the history
Hiding prints makes debugging harder, and it can create new errors. To make it active again, just change hide_prints to True
  • Loading branch information
surisdi committed May 1, 2023
1 parent 03ba31e commit bde4c63
Showing 1 changed file with 23 additions and 19 deletions.
42 changes: 23 additions & 19 deletions utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -168,34 +168,38 @@ def code_to_paste(code):


class HiddenPrints:
hide_prints = False

def __init__(self, model_name=None, console=None, use_newline=True):
self.model_name = model_name
self.console = console
self.use_newline = use_newline
self.tqdm_aux = None

def __enter__(self):
import tqdm # We need to do an extra step to hide tqdm outputs. Does not work in Jupyter Notebooks.
if self.hide_prints:
import tqdm # We need to do an extra step to hide tqdm outputs. Does not work in Jupyter Notebooks.

def nop(it, *a, **k):
return it
def nop(it, *a, **k):
return it

self.tqdm_aux = tqdm.tqdm
tqdm.tqdm = nop
self.tqdm_aux = tqdm.tqdm
tqdm.tqdm = nop

if self.model_name is not None:
self.console.print(f'Loading {self.model_name}...')
self._original_stdout = sys.stdout
self._original_stderr = sys.stderr
sys.stdout = open(os.devnull, 'w')
# May not be what we always want, but some annoying warnings end up to stderr
sys.stderr = open(os.devnull, 'w')
if self.model_name is not None:
self.console.print(f'Loading {self.model_name}...')
self._original_stdout = sys.stdout
self._original_stderr = sys.stderr
sys.stdout = open(os.devnull, 'w')
# May not be what we always want, but some annoying warnings end up to stderr
sys.stderr = open(os.devnull, 'w')

def __exit__(self, exc_type, exc_val, exc_tb):
sys.stdout.close()
sys.stdout = self._original_stdout
sys.stdout = self._original_stderr
if self.model_name is not None:
self.console.print(f'{self.model_name} loaded ')
import tqdm
tqdm.tqdm = self.tqdm_aux
if self.hide_prints:
sys.stdout.close()
sys.stdout = self._original_stdout
sys.stdout = self._original_stderr
if self.model_name is not None:
self.console.print(f'{self.model_name} loaded ')
import tqdm
tqdm.tqdm = self.tqdm_aux

0 comments on commit bde4c63

Please sign in to comment.