Ask Your Question

What techniques do you use to track down bugs in your PSS/E scripts?

asked 2012-01-03 03:46:33 -0500

jtrain gravatar image

Sometimes I have difficult to track down bugs in my Python for PSS/E scripts. The solutions don't match what I expect is physically happening. How do you quickly track down bugs in your code?

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted

answered 2012-01-04 01:14:35 -0500

Daniel_Hillier gravatar image

updated 2012-01-04 05:04:04 -0500

There are a few ways of debugging Python for PSS/E scripts efficiently.

The first and easiest method is to:

Make psspy Throw Exceptions

By default, all calls to psspy return an error code which needs to be checked to see if the call succeeded. If the error codes are not checked, errors can propagate undetected through the code, resulting in erroneous solutions.

Changing this default behaviour, so Python will stop execution and raise an exception as soon as a call to psspy encounters an error, is incredibly easy, removes the need to do manual error code checking and only requires one line of code after the psspy module has been imported:

psspy.throwPsseExceptions = True

As you should at least be aware that there is an error (some may be safe to work around), this mode of operation is far safer than psspy's default mode of operation and it doesn't hurt to have it enabled all the time.

Here is an example of an exception that will be raised:

>>> ierr = psspy.cong()
>>> ierr = psspy.fnsl()
Traceback (most recent call last):
    File "", line 17, in <module>
    File "", line 13, in main
    File ".\", line 5378, in fnsl
psspy.FnslError: fnsl Error: ierr=2

The exception raised will have information about which call failed, its location in the code and an error code for why the call failed. In particular, the last line indicates the exception raised: in this case, a call to the function fnsl raised the error code 2. Unfortunately, for an explanation of what went wrong, you will need to dig into the PSSE API documentation and find what the error code corresponds to for the particular function that failed.

This will make any failing calls to psspy immediately obvious, greatly reducing the time required to debug these calls.

Debugging Python Code

If enabling psspy.throwPsseExceptions doesn't resolve the bug, you will have to debug the Python code the usual ways; check your code is doing what you think it is doing. My goto method is using the Python debugger, pdb. It allows you to step through your code and check the values of variables, dynamically interact with your code and execute various commands at any point through out your code. It's like debugging your code with infinite print statements scattered through your code.

edit flag offensive delete link more


Nice one, thanks Dan. I'll definitely try psspy.throwPsseExceptions

jtrain gravatar imagejtrain ( 2012-01-04 01:46:43 -0500 )edit

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



Asked: 2012-01-03 03:46:33 -0500

Seen: 660 times

Last updated: Jan 04 '12