Skip to content

Instantly share code, notes, and snippets.

@cwebber314
Last active August 29, 2015 14:15
Show Gist options
  • Save cwebber314/0b09f870f62f96ddc57a to your computer and use it in GitHub Desktop.
Save cwebber314/0b09f870f62f96ddc57a to your computer and use it in GitHub Desktop.
Capture output from psspy commands
import pssepath
pssepath.add_pssepath()
import psspy
import redirect
redirect.psse2py()
import sys
# This command prints results to console
psspy.psseinit(50000)
import contextlib
@contextlib.contextmanager
def redirect_stdout(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
import StringIO
f = StringIO.StringIO()
print "before redirect"
with redirect_stdout(f):
psspy.report('123xxx(report)')
psspy.alert('abcyyy(alert)')
psspy.progress('foobar(progress)')
f.seek(0)
var = f.read()
print "after redirect"
print "var: %s" % var
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment