Reputation: 3613
I'm using some library and I can't edit its source. There is a function in the library that I have to call, and when I call it, it makes this file that I want; however, at the same time, it prints this warning to the screen hundreds of times. The warning is always the same.
Warning during export : no corresponding GDSII layer found for process and purpose
This is kind of annoying and makes me printing anything to stdout/stderr useless, because it just gets flooded with this silly warning.
I know how to redirect stdout/stderr by simply assigning them a different file. Is it possible to simply check what will be written to stdout/stderr, discard it if it's that string, otherwise, print it?
Upvotes: 8
Views: 783
Reputation: 9599
I would use something like...
3.x
import sys
from _io import TextIOWrapper
class StdoutFilter(TextIOWrapper):
def __init__(self, stdout):
super().__init__(stdout)
self.stdout = stdout
def write(self, output):
if output != "don't write this":
self.stdout.write(output)
sys.stdout = StdoutFilter(sys.stdout)
print("hello, world!")
print("don't write this")
sys.stdout = sys.__stdout__
2.x
from StringIO import StringIO
class StdoutFilter(StringIO):
def __init__(self, stdout):
StringIO.__init__(self, stdout)
self.stdout = stdout
Hope it helps!
Upvotes: 8