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?

4
  • 1
    Is there a reason you can't edit the library? Commented Oct 9, 2015 at 1:04
  • Did you try -W flag? Commented Oct 9, 2015 at 1:09
  • Yes, it's not my library, and it's running from the owners server. It's also in the process of being commericialised. Commented Oct 9, 2015 at 1:11
  • I recommend letting the owners know that their library has some extraneous print statements/calls, and no one likes a chatty library. Commented Oct 9, 2015 at 1:17

1 Answer 1

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!

Sign up to request clarification or add additional context in comments.

2 Comments

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 You're welcome. See the update for the 2.x version!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.