Re: [Rdkit-discuss] Can't stifle warnings / logs
Hi Cameron, While you are waiting for an answer about the proper way to silence errors, I can give you a work-around which will help with the metaphorical reams of teletype paper you are printing out. However, it is a very crude solution. Basically, close the C/C++ stderr file descriptor, and tell Python to route its error messages to a new stderr. # Copy the stderr descriptor new_fd = os.dup(2) # Close the original stderr os.close(2) # Tell Python about the new stderr sys.__stderr__ = sys.stderr = os.fdopen(new_fd, "w", 0) Here it is in context: import os import sys from rdkit import Chem print("Error?") Chem.MolFromSmiles("Q") # error message # Copy the stderr descriptor new_fd = os.dup(2) # Close the original stderr os.close(2) # Tell Python about the new stderr sys.__stderr__ = sys.stderr = os.fdopen(new_fd, "w", 0) print("Error again?") Chem.MolFromSmiles("Q") # No warning or error message. This assumes that neither RDKit nor some other code at the C level write directly to file descriptor 2 assuming it's stderr. Otherwise there's a problem if you ever open up a new file and it's assigned fd 2. If you *really* want to be sure that C/C++ code doesn't write to stderr, and the above doesn't work, and you are on a Unix-like system, then add the following after the "sys.__stderr__ = .." line: # Reroute the original descriptor number 2 to /dev/null fd2 = os.open("/dev/null", os.O_WRONLY) if fd2 != 2: raise SystemExit("Unable to reroute file descriptor 2: %d\n" % (fd2,)) Andrew da...@dalkescientific.com -- Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot ___ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss
[Rdkit-discuss] Can't stifle warnings / logs
Hi There, I'm a longtime rdkit user but a first time mailing list user. One thing that has always haunted me is the warnings and errors that get thrown whilst reading in a dirty sd file or smiles list. I've tried to no avail to silence the warnings and errors many times and normally just live with the endless scroll on my terminal but today I've finally had enough. Any help would be appreciated!! I've tried wrapping a ForwardSDMolSupplier with the following : from io import StringIO import sys Chem.WrapLogs() def silent_supplier(supplier): sio = sys.stderr = StringIO() for m in supplier: sio = sys.stderr = StringIO() if m is None: continue else: yield m but still get a stream of these sorts of errors: [11:39:24] S group DAT ignored on line 143364 [11:39:24] Can't kekulize mol. Unkekulized atoms: 5 6 7 8 9 10 11 13 14 [11:39:24] ERROR: Could not sanitize molecule ending on line 143370 [11:39:24] ERROR: Can't kekulize mol. Unkekulized atoms: 5 6 7 8 9 10 11 13 14 [11:39:24] S group DAT ignored on line 145111 [11:39:24] Can't kekulize mol. Unkekulized atoms: 3 4 5 6 26 [11:39:24] ERROR: Could not sanitize molecule ending on line 145117 [11:39:24] ERROR: Can't kekulize mol. Unkekulized atoms: 3 4 5 6 26 [11:39:24] S group DAT ignored on line 159380 [11:39:24] Can't kekulize mol. Unkekulized atoms: 5 6 7 8 9 10 11 12 13 [11:39:24] ERROR: Could not sanitize molecule ending on line 159386 [11:39:24] ERROR: Can't kekulize mol. Unkekulized atoms: 5 6 7 8 9 10 11 12 13 -- Check out the vibrant tech community on one of the world's most engaging tech sites, Slashdot.org! http://sdm.link/slashdot___ Rdkit-discuss mailing list Rdkit-discuss@lists.sourceforge.net https://lists.sourceforge.net/lists/listinfo/rdkit-discuss