-
-
Save jtrain/6519794 to your computer and use it in GitHub Desktop.
This file contains 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 psspy | |
psspy.throwPsseExceptions = True | |
def ierr_doc(psspyFunc, ierr): | |
if not ierr: | |
return '' | |
# Find all the errors documented in the doc string | |
errDoc = psspyFunc.__doc__[psspyFunc.__doc__.find('IERR = 0'):] | |
errs = {} | |
for errStr in errDoc.split('\n'): | |
try: | |
errNum = errStr.split('=')[1].strip()[0] | |
errDesc = errStr.split('=')[1].strip()[2:] | |
errs[int(errNum)] = errDesc | |
except: | |
pass | |
return errs[ierr] | |
def raisePsspyError(psspyFunc): | |
"""This decorator changes the default Exception message raised by PSSE to include the | |
ierr description given in the function help documentation. """ | |
def callPsspyFunc(*args, **kwargs): | |
try: | |
return psspyFunc(*args, **kwargs) | |
except psspy.PsseException, e: | |
e.apiname = ierr_doc(psspyFunc, e.ierr) | |
raise e | |
return callPsspyFunc | |
# Apply our decorator to all psspy function which return an ierr | |
import types | |
for k,v in vars(psspy).items(): | |
if isinstance(v, types.FunctionType): | |
if 'IERR = 0' in v.__doc__: | |
vars(psspy)[k] = raisePsspyError(v) | |
if __name__ == '__main__': | |
psspy.case(r"nonexistantFile.sav") # Will raise a python exception instead | |
# of just going mairly on its way |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment