this point, you should have Python and the SWAT package installed, and you should have a running CAS server. In the next chapter, we’ll give a brief summary of what it’s like to use CAS from Python. Then, we’ll dig into the chapters that go into the details of each aspect of SWAT.
Chapter 2: The Ten-Minute Guide to Using CAS from Python
Importing SWAT and Getting Connected
Executing Actions on CAS Tables
If you are already familiar with Python, have a running CAS server, and just can’t wait to get started, we’ve written this chapter just for you. This chapter is a very quick summary of what you can do with CAS from Python. We don’t provide a lot of explanation of the examples; that comes in the later chapters. This chapter is here for those who want to dive in and work through the details in the rest of the book as needed.
In all of the sample code in this chapter, we are using the IPython interface to Python.
Importing SWAT and Getting Connected
The only thing you need to know about the CAS server in order to get connected is the host name, the port number, your user name, and your password. The SWAT package contains the CAS class that is used to communicate with the server. The arguments to the CAS class are hostname, port, username, and password1, in that order. Note that you can use the REST interface by specifying the HTTP port that is used by the CAS server. The CAS class can autodetect the port type for the standard CAS port and HTTP. However, if you use HTTPS, you must specify protocol=’https’ as a keyword argument to the CAS constructor. You can also specify ‘cas’ or ‘http’ to explicitly override autodetection.
In [1]: import swat
In [2]: conn = swat.CAS('server-name.mycompany.com', 5570,
...: 'username', 'password')
When you connect to CAS, it creates a session on the server. By default, all resources (CAS actions, data tables, options, and so on) are available only to that session. Some resources can be promoted to a global scope, which we discuss later in the book.
To see what CAS actions are available, use the help method on the CAS connection object, which calls the help action on the CAS server.
In [3]: out = conn.help()
NOTE: Available Action Sets and Actions:
NOTE: accessControl
NOTE: assumeRole - Assumes a role
NOTE: dropRole - Relinquishes a role
NOTE: showRolesIn - Shows the currently active role
NOTE: showRolesAllowed - Shows the roles that a user
is a member of
NOTE: isInRole - Shows whether a role is assumed
NOTE: isAuthorized - Shows whether access is authorized
NOTE: isAuthorizedActions - Shows whether access is
authorized to actions
NOTE: isAuthorizedTables - Shows whether access is authorized
to tables
NOTE: isAuthorizedColumns - Shows whether access is authorized
to columns
NOTE: listAllPrincipals - Lists all principals that have
explicit access controls
NOTE: whatIsEffective - Lists effective access and
explanations (Origins)
NOTE: partition - Partitions a table
NOTE: recordCount - Shows the number of rows in a Cloud
Analytic Services table
NOTE: loadDataSource - Loads one or more data source interfaces
NOTE: update - Updates rows in a table
The printed notes describe all of the CAS action sets and the actions in those action sets. The help action also returns the action set and action information as a return value. The return values from all actions are in the form of CASResults objects, which are a subclass of the Python collections.OrderedDict class. To see a list of all of the keys, use the keys method just as you would with any Python dictionary. In this case, the keys correspond to the names of the CAS action sets.
In [4]: list(out.keys())
Out[4]:
['accessControl',
'builtins',
'configuration',
'dataPreprocess',
'dataStep',
'percentile',
'search',
'session',
'sessionProp',
'simple',
'table']
Printing the contents of the return value shows all of the top-level keys as sections. In the case of the help action, the information about each action set is returned in a table in each section. These tables are stored in the dictionary as Pandas DataFrames.
In [5]: out
Out[5]:
[accessControl]
name description
0 assumeRole Assumes a role
1 dropRole Relinquishes a role
2 showRolesIn Shows the currently active role
3 showRolesAllowed Shows the roles that a user is a mem...
4 isInRole Shows whether a role is assumed
5 isAuthorized Shows whether access is authorized
6 isAuthorizedActions Shows whether access is authorized t...
7 isAuthorizedTables Shows whether access is authorized t...
8 isAuthorizedColumns Shows whether access is authorized t...
9 listAllPrincipals Lists all principals that have expli...
10 whatIsEffective Lists effective access and explanati...
11 listAcsData Lists access controls for caslibs, t...
12 listAcsActionSet Lists access controls for an action ...
13 repAllAcsCaslib Replaces all access controls for a c...
14 repAllAcsTable Replaces all access controls for