Ask Your Question
2

Silencing PSSE STDOUT

asked 2012-03-16 16:24:38 -0600

chip gravatar image

PSSE is rather talkative and makes liberal use of STDOUT just to let you know its happy. Does anyone know how to silence PSSE and just make it report important things like errors?

For example just setting up psse with the command:

psspy.psseinit(80000)

Causes PSSE to dump a copyright notice and other info to STDOUT:

 PSS«E Version 32
 Copyright (c) 1976-2012
 Siemens Energy, Inc.,
 Power Technologies International                            (PTI)
 This program is a confidential  unpublished  work  created  and  first
 licensed in 1976.  It is a trade secret which is the property of  PTI

Other common operations like reading a raw file behave in the same way. Now I know I could redirect all STDOUT in Python, but that seems rather drastic. Any ideas?

edit retag flag offensive close merge delete

1 answer

Sort by » oldest newest most voted
2

answered 2012-03-16 19:47:09 -0600

JervisW gravatar image

updated 2012-04-07 04:08:39 -0600

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:

  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/contex...

edited Added reference to redirect.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

[hide preview]

Question Tools

1 follower

Stats

Asked: 2012-03-16 16:24:38 -0600

Seen: 7,090 times

Last updated: Apr 07 '12