Reputation: 31
This is the class I am using as my supressor
import sys
import traceback
import warnings
from rdkit import RDLogger
from rdkit import Chem
class Suppressor:
def __init__(self, log_file):
self.log_file = log_file
self.stdout = sys.stdout
self.stderr = sys.stderr
def __enter__(self):
sys.stdout = self
sys.stderr = self
def __exit__(self, exception_type, value, tb):
sys.stdout = self.stdout
sys.stderr = self.stderr
if exception_type is not None:
raise Exception(f"Got exception: {exception_type} {value} {traceback}")
def write(self, x):
with open(self.log_file, "a") as log:
log.write(x)
And this is how I'm using it.
with Suppressor("error.log"):
Chem.MolFromSmiles('CO(C)C')
I have noticed that it works fine in notebook block, it does suppress and logs the output of Chem.MolFromSmiles('CO(C)C')
.
But when I run a script that uses the suppressor in command line it does not suppress nor log the output of Chem.MolFromSmiles('CO(C)C')
. However, it still suppress the output prints.
I tried it with different functions that raises errors and it works fine
Upvotes: 1
Views: 48