SAS Viya. Kevin D. Smith

Читать онлайн книгу.

SAS Viya - Kevin D. Smith


Скачать книгу
[27]: out = conn.history(casout=dict(name='hist'))

      This is such a common idiom that the server enables you to specify dictionary values with only the first specified key given (for example, name), just using the value of that key. That is a mouthful, but it is easier than it sounds. It just means that rather than having to use the dict to create a nested dictionary, you could simply do the following:

      In [28]: out = conn.history(casout='hist')

      Of course, if you need to use any other keys in the casout parameter, you must use the dict form. This conversion of a scalar value to a dictionary value is common when specifying input tables and variable lists of tables, which we see later on.

      Now that we have spent some time on the input side of CAS actions, let’s look at the output side.

      Up to now, all of our examples have stored the result of the action calls in a variable, but we have not done anything with the results yet. Let’s start by using our example of all of the CAS parameter types.

      In [29]: out = conn.echo(

      ....: boolean_true = True,

      ....: boolean_false = False,

      ....: double = 3.14159,

      ....: int32 = 1776,

      ....: int64 = 2**60,

      ....: string = u'I like snowmen! \u2603',

      ....: list = [u'item1', u'item2', u'item3'],

      ....: dict = {'key1': 'value1',

      ....: 'key2': 'value2',

      ....: 'key3': 3}

      ....: )

      Displaying the contents of the out variable gives:

      In [30]: out

       Out[30]:

      [int32]

      1776

      [boolean_false]

      False

      [list]

      ['item1', 'item2', 'item3']

      [boolean_true]

      True

      [int64]

      1152921504606846976

      [double]

      3.14159

      [string]

      'I like snowmen! ☃'

      [dict]

      {'key1': 'value1', 'key2': 'value2', 'key3': 3}

      + Elapsed: 0.000494s, mem: 0.0546mb

      The object that is held in the out variable is an instance of a Python class called CASResults. The CASResults class is a subclass of collections.OrderedDict. This class is a dictionary-like object that preserves the order of the items in it. If you want only a plain Python dictionary, you can convert it as follows, but you lose the ordering of the items.

      In [31]: dict(out)

       Out[31]:

      {'boolean_false': False,

      'boolean_true': True,

      'dict': {'key1': 'value1', 'key2': 'value2', 'key3': 3},

      'double': 3.14159,

      'int32': 1776,

      'int64': 1152921504606846976,

      'list': ['item1', 'item2', 'item3'],

      'string': 'I like snowmen! ☃'}

      In either case, you can traverse and modify the result just as you could any other Python dictionary. For example, if you wanted to walk through the items and print each key and value explicitly, you could do the following:

      In [32]: for key, value in out.items():

      ....: print(key)

      ....: print(value)

      ....: print('')

      ....:

      int32

      1776

      boolean_false

      False

      list

      ['item1', 'item2', 'item3']

      boolean_true

      True

      int64

      1152921504606846976

      double

      3.14159

      string

      I like snowmen! ☃

      dict

      {'key1': 'value1', 'key3': 3, 'key2': 'value2'}

      Although the object that is returned by an action is always a CASResults object, the contents of that object depend completely on the purpose of that action. It could be as simple as key/value pairs of scalars and as complex as a complex nested structure of dictionaries such as our parameters in the previous section. Actions that perform analytics typically return one or more DataFrames that contain the results.

      Since the results objects are simply Python dictionaries, we assume that you are able to handle operations on them. But we will take a closer look at DataFrames in the next section.

      Using DataFrames

      The DataFrames that are returned by CAS actions are extensions of the DataFrames that are defined by the Pandas package. Largely, both work the same way. The only difference is that the DataFrames returned by CAS contain extra metadata that is found in typical SAS data sets. This metadata includes things such as SAS data format names, the SAS data type, and column and table labels.

      One of the builtins actions that returns a DataFrame is help. This action returns a DataFrame that is filled with the names and descriptions of all the actions that are installed on the server. Each action set gets its own key in the result. Let’s look at some output from help.

      The following code runs the help action, lists the keys in the CASResults object that is returned, verifies that it is a SASDataFrame object using Python’s type function, and displays the contents of the DataFrame (some output is reformatted slightly for readability):

      In [33]: out = conn.help()

      In [34]: list(out.keys())

       Out[34]:

      ['accessControl',

      'builtins',

      'loadStreams',

      'search',

      'session',

      'sessionProp',

      'table',

      'tutorial']

      In [35]: type(out['builtins'])

      Out[35]: swat.dataframe.SASDataFrame

      In [36]: out['builtins']

       Out[36]:

      name description


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