Yes, PSS®E is very chatty, which is reassuring some times and other times it is extremely annoying. You could redirect all STDOUT in Python, but here is a recipe to redirect for only a short period of time.
from __future__ import with_statement
from contextlib import contextmanager
import sys
import os
@contextmanager
def silence(file_object=None):
"""
Discard stdout (i.e. write to null device) or
optionally write to given file-like object.
"""
if file_object is None:
file_object = open(os.devnull, 'w')
old_stdout = sys.stdout
try:
sys.stdout = file_object
yield
finally:
sys.stdout = old_stdout
Here is how you would use the silence
function. Make sure you import redirect
and redirect psse output to python.
import redirect
redirect.psse2py()
# discarding stdout.
with silence():
psspy.psseinit(8000)
# writing to file.
stdout = open('psse-chatter.log', 'w')
with silence(stdout):
psspy.case('2012-system-normal.sav')
# writing to file-like object.
stdout = StringIO.StringIO()
with silence(stdout):
psspy.save('2013-system-normal.sav')
print "This isn't redirected"
# retrieving the output from the StringIO object.
print stdout.getvalue()
This works by using Python's context managers [1]. A context manager provides a way to run some 'setup' code, following by a task or series of tasks you want to do, then some 'teardown' code that is always executed after the tasks are complete.
An example of a context manager is the open
function. Which will automatically close the file when you exit its scope.
For our stdout redirecting context manager we:
- save the original
sys.stdout
- set
sys.stdout
to a new value (a file or null device) yield
so the function halts at this point and the tasks in the with
statement are executed - The
with
statement is over so a finally
block re assigns the original sys.stdout
value.
[1] [http://docs.python.org/library/contex...
edited Added reference to redirect
.