Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

I prefer to redirect STDOUT using a contextmanager so that I am sure that STDOUT goes back to normal after I am done capturing/ignoring it. The contextmanger will do the right thing if there is an exception, and plays nicely with pdb.set_trace(). If I don't use a contextmanger it still works, but invariably my code throws an exception while STDOUT is being captured, and then everything is a mess because I can't see STDOUT.

Usually if redirecting STDOUT is failing it is because I forget the line redirect.psse2py(). Here is a simple example with psseinit()

# silence.py
import sys
import os

import pssepath
pssepath.add_pssepath()    
import psspy

import redirect
redirect.psse2py()

import contextlib
@contextlib.contextmanager
def silence(new_target=None):
    # sometimes you don't care about messages.
    if new_target is None:
        new_target = open(os.devnull, 'w')
    old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout
    try:
        yield new_target # run some code with the replaced stdout
    finally:
        sys.stdout = old_target # restore to the previous value

if sys.argv[1] == 'silent':
    with silence():
        psspy.psseinit(50000)
else:
    psspy.psseinit(50000)

And the results from running it in the CLI:

C:\projects\foo
>python silence.py silent

C:\projects\foo
>python silence.py verbose

 PSS(R)E Version 33
 <... snip ...>
 treaties.  All  Rights  Reserved  Under  The  Copyright  Laws.


           SIEMENS POWER TECHNOLOGIES INTERNATIONAL

      50000 BUS POWER SYSTEM SIMULATOR--PSS(R)E-33.5.2

             INITIATED ON FRI, MAR 06 2015   9:57

I prefer to redirect STDOUT using a contextmanager so that I am sure that STDOUT goes back to normal after I am done capturing/ignoring it. The contextmanger will do the right thing if there is an exception, and plays nicely with pdb.set_trace(). If I don't use a contextmanger it still works, but invariably my code throws an exception while STDOUT is being captured, and then everything is a mess because I can't see STDOUT.

Usually if redirecting STDOUT is failing it is because I forget the line redirect.psse2py(). Here is a simple example with psseinit()

# silence.py
import sys
import os

import pssepath
pssepath.add_pssepath()    
import psspy

import redirect
redirect.psse2py()

import contextlib
@contextlib.contextmanager
def silence(new_target=None):
    # sometimes you don't care about messages.
    if new_target is None:
        new_target = open(os.devnull, 'w')
    old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout
    try:
        yield new_target # run some code with the replaced stdout
    finally:
        sys.stdout = old_target # restore to the previous value

if sys.argv[1] == 'silent':
    with silence():
        psspy.psseinit(50000)
else:
    psspy.psseinit(50000)

And the results from running it in the CLI:

C:\projects\foo
>python silence.py silent

C:\projects\foo
>python silence.py verbose

 PSS(R)E Version 33
 <... snip ...>
 treaties.  All  Rights  Reserved  Under  The  Copyright  Laws.


           SIEMENS POWER TECHNOLOGIES INTERNATIONAL

      50000 BUS POWER SYSTEM SIMULATOR--PSS(R)E-33.5.2

             INITIATED ON FRI, MAR 06 2015   9:57

Without redirect.psse2py()

If you do not run the redirect.psse2py() function, STDOUT will be captured for everything except PSSE. (I do not understand how PSSE is making text appear on the screen without STDOUT).

If we use our example code above we see that normal python is silenced:

In [1]: print "Checkpoint 0"
Checkpoint 0

In [2]: with silence():
   ...:     print "Checkpoint 1"
   ...:
In [3]:

But PSSE is still noisy:

In [3]: with silence():
   ...:     psspy.psseinit(10)
   ...:

 PSS(R)E Version 33
 Copyright (c) 1976-2015
 <.......... snip ..............>

I prefer to redirect STDOUT using a contextmanager so that I am sure that STDOUT goes back to normal after I am done capturing/ignoring it. The contextmanger will do the right thing if there is an exception, and plays nicely with pdb.set_trace(). If I don't use a contextmanger it still works, but invariably my code throws an exception while STDOUT is being captured, and then everything is a mess because I can't see STDOUT.

Usually if redirecting STDOUT is failing it is because I forget the line redirect.psse2py(). Here is a simple example with psseinit()

# silence.py
import sys
import os

import pssepath
pssepath.add_pssepath()    
import psspy

import redirect
redirect.psse2py()

import contextlib
@contextlib.contextmanager
def silence(new_target=None):
    # sometimes you don't care about messages.
    if new_target is None:
        new_target = open(os.devnull, 'w')
    old_target, sys.stdout = sys.stdout, new_target # replace sys.stdout
    try:
        yield new_target # run some code with the replaced stdout
    finally:
        sys.stdout = old_target # restore to the previous value

if sys.argv[1] == 'silent':
    with silence():
        psspy.psseinit(50000)
else:
    psspy.psseinit(50000)

And the results from running it in the CLI:

C:\projects\foo
>python silence.py silent

C:\projects\foo
>python silence.py verbose

 PSS(R)E Version 33
 <... snip ...>
 treaties.  All  Rights  Reserved  Under  The  Copyright  Laws.


           SIEMENS POWER TECHNOLOGIES INTERNATIONAL

      50000 BUS POWER SYSTEM SIMULATOR--PSS(R)E-33.5.2

             INITIATED ON FRI, MAR 06 2015   9:57

Without redirect.psse2py()

If you do not run the redirect.psse2py() function, STDOUT will be captured for everything except PSSE. (I do not understand how PSSE is making text appear on the screen without STDOUT).

If we use our example code above and leave out the redirect.psse2py(), we see that normal python is silenced:

In [1]: print "Checkpoint 0"
Checkpoint 0

In [2]: with silence():
   ...:     print "Checkpoint 1"
   ...:
In [3]:

But PSSE is still noisy:

In [3]: with silence():
   ...:     psspy.psseinit(10)
   ...:

 PSS(R)E Version 33
 Copyright (c) 1976-2015
 <.......... snip ..............>