Your First CAS Connection from Python

Let's start with a gentle introduction to the Python CAS client by doing some basic operations like creating a CAS connection and running a simple action. You'll need to have Python installed as well as the SWAT Python package from SAS, and you'll need a running CAS server.

We will be using Python 3 for our example. Specifically, we will be using the IPython interactive prompt (type 'ipython' rather than 'python' at your command prompt). The first thing we need to do is import SWAT and create a CAS session. We will use the name 'mycas1' for our CAS hostname and 12345 as our CAS port name. In this case, we will use username/password authentication, but other authentication mechanisms are also possible depending on your configuration.


In [1]:
# Import the SWAT package which contains the CAS interface
import swat

# Create a CAS session on mycas1 port 12345
conn = swat.CAS('mycas1', 12345, 'username', 'password')


Out[1]:
CAS('mycas1', 12345, 'username', protocol='cas', name='py-session-1', session='fc767e9f-4620-1e45-bd75-61518bdc9d8c')

As you can see above, we have a session on the server. It has been assigned a unique session ID and more user-friendly name. In this case, we are using the binary CAS protocol as opposed to the REST interface. We can now run CAS actions in the session. Let's begin with a simple one: listnodes.


In [2]:
# Run the builtins.listnodes action
nodes = conn.listnodes()
nodes


NOTE: Information is available on 6 nodes.
Out[2]:
§ nodelist
Node List
name role connected IP Address
0 mycas1 controller Yes 10.37.10.172
1 mycas2 controller Yes 10.37.10.174
2 mycas3 worker Yes 10.37.10.175
3 mycas4 worker Yes 10.37.10.176
4 mycas5 worker Yes 10.37.10.191
5 mycas6 worker Yes 10.37.10.193

elapsed 0.00862s · user 0.012s · sys 0.018s · mem 0.406MB

The listnodes action returns a CASResults object (which is just a subclass of Python's ordered dictionary). It contains one key ('nodelist') which holds a Pandas DataFrame. We can now grab that DataFrame to do further operations on it.


In [3]:
# Grab the nodelist DataFrame
df = nodes['nodelist']
df


Out[3]:
Node List
name role connected IP Address
0 mycas1 controller Yes 10.37.10.172
1 mycas2 controller Yes 10.37.10.174
2 mycas3 worker Yes 10.37.10.175
3 mycas4 worker Yes 10.37.10.176
4 mycas5 worker Yes 10.37.10.191
5 mycas6 worker Yes 10.37.10.193

Use DataFrame selection to subset the columns.


In [4]:
roles = df[['name', 'role']]
roles


Out[4]:
Node List
name role
0 mycas1 controller
1 mycas2 controller
2 mycas3 worker
3 mycas4 worker
4 mycas5 worker
5 mycas6 worker

In [5]:
# Extract the worker nodes using a DataFrame mask
roles[roles.role == 'worker']


Out[5]:
Node List
name role
2 mycas3 worker
3 mycas4 worker
4 mycas5 worker
5 mycas6 worker

In [6]:
# Extract the controllers using a DataFrame mask
roles[roles.role == 'controller']


Out[6]:
Node List
name role
0 mycas1 controller
1 mycas2 controller

In the code above, we are doing some standard DataFrame operations using expressions to filter the DataFrame to include only worker nodes or controller nodes. Pandas DataFrames support lots of ways of slicing and dicing your data. If you aren't familiar with them, you'll want to get acquainted on the Pandas web site.

When you are finished with a CAS session, it's always a good idea to clean up.


In [7]:
conn.close()

Those are the very basics of connecting to CAS, running an action, and manipulating the results on the client side. You should now be able to jump to other topics on the Python CAS client to do some more interesting work.


In [ ]: