Читать книгу SAS Viya - Kevin D. Smith - Страница 23

CAS Action Results

Оглавление

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.

SAS Viya

Подняться наверх