SAS Viya. Kevin D. Smith

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

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

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

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

      Scalar Parameter to Dictionary Conversion

      Many times when using an action parameter that requires a dictionary as an argument, you use only the first key in the dictionary to specify the parameter. For example, the history action takes a parameter called casout. This parameter specifies an output table to put the history information into. The specification for this parameter follows: (You can use conn.history? in IPython to see the parameter definition.)

      casout : dict or CASTable, optional

      specifies the settings for saving the action history to an

      output table.

      casout.name : string or CASTable, optional

      specifies the name to associate with the table.

      casout.caslib : string, optional

      specifies the name of the caslib to use.

      casout.timestamp : string, optional

      specifies the timestamp to apply to the table. Specify

      the value in the form that is appropriate for your

      session locale.

      casout.compress : boolean, optional

      when set to True, data compression is applied to the table.

      Default: False

      casout.replace : boolean, optional

      specifies whether to overwrite an existing table with the same

      name.

      Default: False

      ... truncated ...

      The first key in the casout parameter is name and indicates the name of the CAS table to create. The complete way of specifying this parameter with only the name key follows:

      In [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)

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