Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 "converted_loads.py", line 17, in <module>
    File "converted_loads.py", line 13, in main
    File ".\psspy.py", 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 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.

click to hide/show revision 2
Clarified why people should look into the documentation.

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 "converted_loads.py", line 17, in <module>
    File "converted_loads.py", line 13, in main
    File ".\psspy.py", 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 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.