8

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?

Jean-Luc
  • 3,563
  • 8
  • 40
  • 78

1 Answers1

8

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!

cdonts
  • 9,304
  • 4
  • 46
  • 72
  • Thanks for that, certainly does help! There seems to be an odd problem getting it to work for python 2.7. I get the error: `attribute error: readable` at `super().__init__(stdout)`. Any ideas why? A Google search didn't seem to reveal much. – Jean-Luc Oct 09 '15 at 06:20
  • 1
    @Jean-Luc You're welcome. See the update for the 2.x version! – cdonts Oct 10 '15 at 00:09