Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Yes, PSS/E is very chatty sometimes I find that reassuring 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.

# 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()

Yes, PSS/E is very chatty sometimes I find that reassuring 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.

# 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:

  1. save the original sys.stdout
  2. set sys.stdout to a new value (a file or null device)
  3. yield so the function halts at this point and the tasks in the with statement are executed
  4. The with statement is over so a finally block re assigns the original sys.stdout value.

[1] [http://docs.python.org/library/contextlib.html](http://docs.python.org/library/contextlib.html)

click to hide/show revision 3
intro easier to read?

Yes, PSS/E PSS®E is very chatty sometimes I find that 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.

# 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:

  1. save the original sys.stdout
  2. set sys.stdout to a new value (a file or null device)
  3. yield so the function halts at this point and the tasks in the with statement are executed
  4. The with statement is over so a finally block re assigns the original sys.stdout value.

[1] [http://docs.python.org/library/contextlib.html](http://docs.python.org/library/contextlib.html)

click to hide/show revision 4
added reference to redirect module.

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.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:

  1. save the original sys.stdout
  2. set sys.stdout to a new value (a file or null device)
  3. yield so the function halts at this point and the tasks in the with statement are executed
  4. The with statement is over so a finally block re assigns the original sys.stdout value.

[1] [http://docs.python.org/library/contextlib.html](http://docs.python.org/library/contextlib.html)

edited Added reference to redirect.