Last active
August 29, 2015 14:15
-
-
Save cwebber314/0b09f870f62f96ddc57a to your computer and use it in GitHub Desktop.
Capture output from psspy commands
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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