SAS Viya. Kevin D. Smith

Чтение книги онлайн.

Читать онлайн книгу SAS Viya - Kevin D. Smith страница 22

Автор:
Жанр:
Серия:
Издательство:
SAS Viya - Kevin D. Smith

Скачать книгу

for getting help from CAS, it is an important topic to recap. Every object in the SWAT package uses the standard Python method of surfacing documentation. This includes the help function in Python (for example, help(swat.CAS)), the ? suffix operator in IPython and Jupyter (for example, swat.CAS?), and any other tool that uses Python’s docstrings.

      In addition, action sets and actions that are loaded dynamically also support the same Python, IPython, and Jupyter Help system hooks (for example, conn.summary?).

      Keep in mind that tab completion on the CAS objects and other objects in the SWAT package can be a quick reminder of the attributes and methods of that object.

      These Help system hooks should be sufficient to help you get information about any objects in the SWAT package, CAS action sets, and CAS actions and their parameters. If more detailed information is needed, it is available in the official SAS Viya documentation on the SAS website.

      The issue of CAS action errors was discussed to some extent previously in the chapter. There are two methods for dealing with CAS action errors: return codes and exceptions. The default behavior is to surface return codes, but SWAT can be configured to raise exceptions. In the case of return codes, they are

      available in the severity attribute of the CASResults object that is returned by CAS action methods. The possible values are shown in the following table:

Severity Description
0 An action was executed with no warnings or errors.
1 Warnings were generated.
2 An error occurred.

      In addition to the severity attribute, the CASResults object has an attribute named reason, which is a string that contains the general reason for the warning or error. The possible reasons are shown in the following table:

Reason Description
ok The action was executed with no warnings or errors.
abort The action was aborted.
authentication The action could not authenticate user credentials.
authorization The action was unable to access a resource due to permissions settings.
exception An exception occurred during the execution of an action.
expired-token An authentication token expired.
io An input/output error occurred.
memory Out of memory.
network Networking failure.
session-retry An action restarted and results already returned should be ignored.
unknown The reason is unknown.

      The last two attributes to note are status and status_code. The status attribute of CASResults contains a human-readable formatted message that describes the action result. The status_code is a numeric code that can be supplied to Technical Support if further assistance is required. The code contains information that might be useful to Technical Support for determining the source of the problem.

      Using CAS Action Exceptions

      As mentioned previously, it is possible for SWAT to raise an exception when an error or warning occurs in a CAS action. This is enabled by setting an option in the SWAT package. We haven’t covered SWAT options yet. It is covered in the next section in this chapter. However, the simplest way to enable exceptions is to submit the following code:

      In [62]: swat.options.cas.exception_on_severity = 2

      This causes SWAT to throw a swat.SWATCASActionError exception when the severity of the response is 2. For exceptions to be raised on warnings and errors, you set the value of the option to 1. Setting it to None disables this feature.

      The swat.SWATCASActionError exception contains several attributes that contain information about the context of the exception when it was raised. The attributes are described in the following table:

Attribute Name Description
message The formatted status message from the CAS action.
response The CASResponse object that contains the final response from the CAS host.
connection The CAS connection object that the action was running in.
results The compiled result up to the point of the exception.

      The message attribute is simply the same value as the status attribute of the response. The response attribute is an object that we haven’t discussed yet: CASResponse. This object isn’t seen when using the CAS action methods on a CAS object. It is used behind the scenes when compiling the results of a CAS action. Then it discarded. It is possible to use more advanced methods of traversing the responses from CAS where you deal with CASResponse objects directly, but that is not discussed until much later in this book. For now, it is sufficient to know that the CASResponse object has several attributes, including one named disposition, which contains the same result code fields that the CASResults object also contains.

      The connection attribute of swat.SWATCASActionError contains the CAS connection object that executed the action. And finally, the results attribute contains the results that have been compiled up to that point. Normally, this is a CASResults object, but there are options on the CAS action methods that we haven’t yet discussed that cause other data values to be inserted into that attribute.

      Catching a swat.SWATCASActionError is just like catching any other Python exception. You use a try/except block, where the except statement specifies swat.SWATCASActionError (or any of its parent classes). In the following code, we try to get help on a nonexistent action. The action call is wrapped in a try/except block in which the except statement captures the exception as the variable err. In the except section, the message attribute of the exception is printed. Of course, you can use any of the other fields to handle the exception in any way you prefer.

      In [63]: try:

      ...: out = conn.help(action='nonexistent')

      ...: except swat.SWATCASActionError

Скачать книгу