Filtering tool messages by the level of severity

As I mentioned in the last recipe, all tools generate a number of messages that can be classified as information, warnings, or error messages. The GetMessages() method accepts a parameter that allows you to filter the messages that are returned. For example, you may not be interested in the informative or warning messages in your script. However, you will certainly be interested in error messages as they indicate a fatal error that will not allow a tool to successfully execute. Using GetMessages(), you can filter the returned message to include only error messages.

Getting ready

Messages are classified into one of three types, which are indicated by the level of severity. Informational messages provide descriptive information concerning things, such as a tools progress, start and end times of the tool, output data characteristics, and much more. The severity of an informational message is indicated by a value of 0. Warning messages are generated when a problem has occurred during execution that may affect the output. Warnings are indicated with a severity level of 1 and don't normally stop a tool from running. The last type of message is an error message, which is indicated by a numeric value of 2. These indicate fatal events that prevent a tool from running. Multiple messages may be generated during the execution of a tool, and these are stored in a list. More information about the severity of message is provided in the following image. In this recipe, you will learn how to filter the messages generated by the GetMessages() function:

Getting ready

How to do it…

Filtering the messages returned by a tool is really quite simple. You simply provide the severity level you'd like to return as a parameter for the GetMessages() function.

  1. If necessary, open the C:ArcpyBookCh11ErrorHandling.py file in IDLE.
  2. Alter the GetMessages() function so that you pass in a value of 2 as the only parameter:
    import arcpy
    try:
      arcpy.env.workspace = "c:/ArcpyBook/data"
      arcpy.Buffer_analysis("Streams.shp","Streams_Buff.shp")
    except:
      print(arcpy.GetMessages(2))
  3. You can check your work by examining the C:ArcpyBookcodeCh11ErrorHandling4.py solution file.
  4. Save and run the script to see the output:
    Failed to execute. Parameters are not valid.
    ERROR 000735: Distance [value or field]: Value is required
    Failed to execute (Buffer).
    

How it works…

As I mentioned earlier, the GetMessages() method can accept an integer argument of 0, 1, or 2. Passing a value of 0 indicates that all messages should be returned, while passing a value of 1 indicates that you wish to see warnings. In our case, we have passed a value of 2, which indicates that we only want to see error messages. Therefore, you won't see any of the other information messages, such as the start and end times of the script.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset