0

I want to suppress auto generated output from this line of code:

cp = client.fetch_market_depth(a)["Data"][0]['LastTradedPrice']

Here client.fetch_market_depth() is part of coding API developed by 5paisa.
It's using log_response to output debug messages.

It outputs "current time | Success"

After googling and using code from this link of Stack Overflow, I am using following code. Still it is unable to suppress the output. What option I have now? I use Python 3.

After I receive the suggestion in comment, I edited the code and redirected output from stderr too. Still it's printing same output.

class HiddenPrints:
    def __enter__(self):
        self._original_stdout = sys.stdout
        self._original_stderr = sys.stderr
        sys.stdout = open(os.devnull, 'w')
        sys.stderr = open(os.devnull, "w")

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout = self._original_stdout
        sys.stderr = self._original_stderr

SC1 = 20374
a=[{"Exchange":"N","ExchangeType":"C","ScripCode":SC1},]
with HiddenPrints():
    cp = client.fetch_market_depth(a)["Data"][0]['LastTradedPrice']
gre_gor
  • 6,669
  • 9
  • 47
  • 52
narayanpatra
  • 5,627
  • 13
  • 51
  • 60
  • Maybe overload the print method (as a guess...)? – PrinsEdje80 May 23 '22 at 09:27
  • Have you tried the IPython magic `%%capture`? Or check if the library you are using has some flags you can set. – Edward Ji May 23 '22 at 09:30
  • 2
    Is any of the output being written to stderr? If so, then you may need to redirect that as well. – Tom Karzes May 23 '22 at 09:34
  • @TomKarzes : Tried, did not work. Kindly check the edit. – narayanpatra May 23 '22 at 09:54
  • 1
    We have no idea what `fetch_market_depth` does or how it generates its output. – tripleee May 23 '22 at 10:13
  • @triplee : It is part of trading api developed by 5paisa. here is the link : https://github.com/5paisa/py5paisa – narayanpatra May 23 '22 at 11:49
  • The code in https://github.com/5paisa/py5paisa/blob/2b3403b305062fd98a27fde8b67336778929b08e/py5paisa/py5paisa.py#L257 ends up calling https://github.com/5paisa/py5paisa/blob/3bb8af85d4015d1d2620fbfc443127998912bc54/py5paisa/logging.py which uses the Python `logging` module, but then we'd need to see how you have configured that. – tripleee May 23 '22 at 11:52
  • @tripleee It's using the `loguru` module not `logging`. – gre_gor May 23 '22 at 12:05
  • Looks like that the problem is that the py5paisa's logging module gets the reference to the real `sys.stdout` before `HiddenPrints` replaces it. – gre_gor May 23 '22 at 12:09

1 Answers1

0

Looks like that the problem is that the py5paisa's logging module gets the reference to the real sys.stdout before HiddenPrints replaces it.

You need to disable the logger by importing it and calling logger.disable(""). And after exiting the context, enable it with logger.enable("").

If you also want to ignore any exceptions, you need to return True on exit.

class HiddenPrints:
    def __enter__(self):
        self._original_stdout = sys.stdout
        self._original_stderr = sys.stderr
        sys.stdout = open(os.devnull, 'w')
        sys.stderr = open(os.devnull, "w")
        from loguru import logger
        logger.disable("") #disable the logger

    def __exit__(self, exc_type, exc_val, exc_tb):
        sys.stdout = self._original_stdout
        sys.stderr = self._original_stderr
        from loguru import logger
        logger.enable("") #enable the logger
        return True #optionally also ignore exceptions
gre_gor
  • 6,669
  • 9
  • 47
  • 52