Kevin D. Smith

SAS Viya


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

addition to the attributes previously described, the messages attribute contains any messages that were printed during the execution of the action. While you likely saw the messages as they were being printed by the action, it can sometimes be useful to have them accessible on the CASResults object for programmatic inspection. Let’s use the help action for help on an action that does exist and also an action that doesn’t exist to see the status information attributes in action.

      The first example, as follows, asks for help on an existing action. The returned status attributes are all zeros for numeric values and None for reason and status. The messages attribute contains a list of all messages that are printed by the server.

      In [42]: out = conn.help(action='help')

      NOTE: Information for action 'builtins.help':

      NOTE: The following parameters are accepted.

      Default values are shown.

      NOTE: string action=NULL,

      NOTE: specifies the name of the action for which you want help.

      The name can be in the form 'actionSetName.actionName'

      or just 'actionName'.

      NOTE: string actionSet=NULL,

      NOTE: specifies the name of the action set for which you

      want help. This parameter is ignored if the action

      parameter is specified.

      NOTE: boolean verbose=true

      NOTE: when set to True, provides more detail for each parameter.

      In [43]: print(out.status)

      None

      In [44]: out.status_code

      Out[44]: 0

      In [45]: print(out.reason)

      None

      In [46]: out.severity

      Out[46]: 0

      In [47]: out.messages

       Out[47]:

      ["NOTE: Information for action 'builtins.help':",

      'NOTE: The following parameters are accepted. Default values are shown.',

      'NOTE: string action=NULL,',

      "NOTE: specifies the name of the action for which you want help. The name can be in the form 'actionSetName.actionName' or just 'actionName.",

      'NOTE: string actionSet=NULL,',

      'NOTE: specifies the name of the action set for which you want help. This parameter is ignored if the action parameter is specified.',

      'NOTE: boolean verbose=true',

      'NOTE: when set to True, provides more detail for each parameter.']

      Now let’s ask for help on a nonexistent action.

      In [48]: out = conn.help(action='nonexistent')

      ERROR: Action 'nonexistent' was not found.

      ERROR: The action stopped due to errors.

      In [49]: out.status

      Out[49]: 'The specified action was not found.'

      In [50]: out.status_code

      Out[50]: 2720406

      In [51]: out.reason

      Out[51]: 'abort'

      In [52]: out.severity

      Out[52]: 2

      In [53]: out.messages

       Out[53]:

      ["ERROR: Action 'nonexistent' was not found.",

      'ERROR: The action stopped due to errors.']

      In this case, all of the attributes contain information about the error that was generated. You can use this information about the CASResults object to capture and handle errors more gracefully in your programs.

      If you prefer to use exceptions rather than status codes, you can set the cas.exception_on_severity option to 1 to raise exceptions on warnings, or you can set the option to 2 to raise exceptions on errors. The options system is covered in detail later in this chapter.

      In [54]: swat.set_option('cas.exception_on_severity', 2)

      In [55]: try:

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

      ....: except swat.SWATCASActionError as err:

      ....: print(err.response)

      ....: print('')

      ....: print(err.connection)

      ....: print('')

      ....: # Since this action call fails before producing

      ....: # results, this will be empty. In actions that

      ....: # fail partway through, this may contain results

      ....: # up to the point of failure.

      ....: print(err.results)

      ....:

      ERROR: Action 'nonexistent' was not found.

      ERROR: The action stopped due to errors.

      CASResponse(messages=[],

      disposition=CASDisposition(

      debug=0x88bfc196:TKCASA_GEN_ACTION_NOT_FOUND, reason=abort,

      severity=2, status=The specified action was not found.,

      status_code=2720406), performance=CASPerformance(cpu_system_time=0.0, cpu_user_time=0.0,

      data_movement_bytes=0, data_movement_time=0.0,

      elapsed_time=0.000279, memory=50080, memory_os=8441856,

      memory_quota=12111872, system_cores=32, system_nodes=1,

      system_total_memory=202931654656))

      CAS('server-name.mycompany.com', 5570, 'username',

      protocol='cas', name='py-session-1',

      session='292319d5-151f-f241-b27c-c3b6a93c1814')

      As you can see, working with results from CAS actions is the same as the workflow with any other Python framework. You connect to a CAS host, run a CAS action (either using keyword arguments, building the

      parameters ahead of time, or using a mixture of methods), check the return status, and process the dict-like CASResults object that is returned.

      Now that we understand the basics of the workflow, let’s look at how to add additional action sets and actions to your CAS session.

      In the previous sections, we have already seen that a CAS session has access to multiple action sets that each contain multiple actions. However, all of the action sets we have seen so far have been installed automatically when we connect to CAS. We haven’t shown how to load additional action sets in order to do additional operations such as advanced analytics, machine learning, streaming data analysis, and so on.

      In order to load new action sets, we must first see what action sets are available on our server. We can