SAS Viya. Kevin D. Smith
Чтение книги онлайн.
Читать онлайн книгу SAS Viya - Kevin D. Smith страница 6
![SAS Viya - Kevin D. Smith SAS Viya - Kevin D. Smith](/cover_pre687260.jpg)
To install SWAT, you use the standard Python installation tool pip. On Linux and Macintosh, the pip command is in the bin directory of your Anaconda installation. On Windows, it is in the Scripts directory of the Anaconda distribution. The SWAT installers are located at GitHub in the python-swat project of the sassoftware account. The available releases are listed at the following link:
https://github.com/sassoftware/python-swat/releases
You can install SWAT directly from the download link using pip as follows.
pip install https://github.com/sassoftware/python-
swat/releases/download/vX.X.X/python-swat-X.X.X-platform.tar.gz
Where X.X.X is the version number, and platform is the platform that you are installing on. If your platform isn’t available, you can install using the source code URL on the releases page instead, but you are restricted to using the REST interface over HTTP or HTTPS. The source code release is pure Python, so it will run wherever Python and the prerequisite packages are supported.
Note that if you have both Python 2 and Python 3 installed on your system (or even multiple installations of a particular Python version), you need to be careful to run the pip command from the installation where SWAT is installed. In any case, the same SWAT package works for both Python 2 and Python 3.
After SWAT is installed, you should be able to run the following command in Python in order to load the SWAT package:
>>> import swat
With Anaconda, you can submit the preceding code in several ways. You can use the python command at the command line. However, if you are going to use the command line, we’d recommend that you at least use the ipython command, which is preferred for interactive use. You also have the option of using the Spyder IDE that comes bundled with Anaconda. The Spyder IDE is useful for debugging as well as for development and interactive use. You can also use the popular Jupyter notebook, which was previously known as the IPython notebook. Jupyter is most commonly used within a web browser. It can be launched with the jupyter notebook command at the command line, or you can launch it from the Anaconda Launcher application.
In this book, we primarily show plain text output using the IPython interpreter. However, all of the code from this book is also available in the form of Jupyter notebooks here,
https://github.com/sassoftware/sas-viya-the-python-perspective
Now that we have installed Python and SWAT, the last thing we need is a CAS server.
Installing CAS
The installation of CAS is beyond the scope of this book. Installation on your own server requires a CAS software license and system administrator privileges. You need to contact your system administrator about installing, configuring, and running CAS.
Making Your First Connection
With all of the pieces in place, we can make a test connection just to verify that everything is working. From Python, you should be able to run the following commands:
>>> import swat
>>> conn = swat.CAS('server-name.mycompany.com', port-number,
'userid', 'password')
>>> conn.serverstatus()
>>> conn.close()
Where server-name.mycompany.com is the name or IP address of your CAS server, port-number is the port number that CAS is listening to, userid is your CAS user ID, and password is your CAS password. The serverstatus method should return information about the CAS grid that you are connected to, and the close method closes the connection. If the commands run successfully, then you are ready to move on. If not, you’ll have to do some troubleshooting before you continue.
Conclusion
At 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