Follow

Debugging EnSight Python command script tips

Tips on debugging EnSight Python command scripts

Currently EnSight does not have a built-in debugging parser to debug command language by way of python scripts through the EnSight Python Interpreter.  But here are a few considerations that will help.

Using ensight.sendmesgoptions() options

In an effort to create error free scripts, start them (just after the import statements) with:

ensight.sendmesgoptions(display=1,exception=1)

This does two things. First, it displays all of the generated command language (under the Python tab) to the command dialog (so you can see what is actually being executed). Second, if any of the command language functions fail, the script will stop right there and in many cases will display the error message from command language.  (See under EnSight on-line Help > Interface Manual... > 6 EnSight Python Interpreter > 6.1 Python EnSight module interface > ensight.sendmesgoptions for details.)

Using methods that query for action

In addition, you could add a method that querys for action.  An example that pops up a dialog and waits for you to click ok would be to add this to the top of your file:

import ensight
from cei.qtgenericdlg import *

def stop_here():
  if (ensight.batch()):
     return
  items = []
  items.append(['p',ITEM_TEXT,"Click Ok to continue",
                "Click Ok to continue"])

  dlg = CeiQtGenericDialog(items,None,"Click Ok")
  if (dlg.doit() == 0):
     raise Exception("User abort!")

Then if you click 'ok', your code will continue and if you click 'cancel', it will generate a Python error and it will stop executing the code in your module. It will also print out the line number where 'stop_here()' was called in your script.

Then anywhere you want to stop, you add the line:

stop_here()

It will put up a dialog and wait for you to click ok (or cancel).

Using 'import ensight'

Another thing to consider when debugging Python is how you run your scripts. If you are editing them in the built-in editor and selecting "Run Script" from the file menu; consider adding 'import ensight' to the top of your file and running it with 'import script as module'. The 'Run' option executes your lines as if they were typed at the console. It makes it difficult to see where things went wrong as EnSight does not know the input came from a file. The 'Import' option pre-compiles your script as a module and then executes it. This gives python good line numbers in the file.
In conjunction with the 'exception=1' sendmesgoptions() flag, when you get an error, you will know exactly at what line the problem happened.

Putting it all together...

So, the full suggestion is to start your file with:

import ensight
from cei.qtgenericdlg import *

def stop_here():
  if (ensight.batch()):
    return
  items = []
  items.append(['p',ITEM_TEXT,"Click Ok to continue",
                "Click Ok to continue"])

  dlg = CeiQtGenericDialog(items,None,"Click Ok")
  if (dlg.doit() == 0):
    raise Exception("User abort!")

ensight.sendmesgoptions(exception=1,display=1)

and run using the 'import' option in the editor. You can then use:

stop_here()

as needed.


Using "try:/except:" constructs

Remember you can also enhance your debugging capability with appropirate use of try:/except: constructs.  For example, executing the following construct

try:
    print "hello"
    print A
    print "bye"
except Exception,e:
    print "There was an error:",e

via the EnSight Python interpreter will print

hello

There was an error: name 'A' is not defined


because the variable A is not defined.  Effective use of  try:/except: constructs can not only help debug your scripts, but help create error-free scripts.

... check back periodically for more tips ....

Was this article helpful?
0 out of 0 found this helpful
Have more questions? Submit a request

Comments