5.2 The Python Shell Window

To open the Python shell window you select the Tools->Run Python Shell from the main menu system.


[Picture]
Figure 5.1: Selecting the Python Shell.

This can be done with an open project or without an open project. The Python Shell Window has a tool bar with a couple of useful buttons listed below from left to right:
Clear Shell - Erase the command history and clear the text on the shell.
Open a Script - Run a script from which each line will be executed in the shell.
Save as Script - Save the command history (not the output of the commands) as a script.

It may be useful when developing a script to experiment interactively in the ChemTree shell. Once a solution has been reached, the Save as Script button can be used to save the current history into a text file which can later be edited to make finishing touches.

5.2.1 Using Shell Objects

While describing the Python Shell there are some terms that will be used repeatedly. Object is one of them. Object refers to a named shell item that represents some data and has methods associated with it. Different scripting commands will create objects for you. When the Python Shell is started one object is always created and present for your use. It is called ghi. Every object will have methods you can use to perform some task. Methods are accessed using the object name followed by a period then the method name and any parameters in parentheses. Like this objectName.methodName(parameters). Sometimes a method will change the state of an object’s data, and other times a method may create a new object. Most objects provided by ChemTree will display information about their data if you type just the object name in the shell and press return. For example, if you have a spreadsheet object called ss and you typed “ss” at the shell prompt you would see a list of the spreadsheet’s attributes such as the number of rows, the number of columns, what dataset it is based on, and its Navigator Node display name and ID.

5.2.2 Using the Dir Command

At any time you can use the dir() command to see all the objects currently available. Shown below is the dir() command with no parameters. It produces a listing all the current objects.


[Picture]
Figure 5.2: Using the dir command.

As you can see the ghi object is listed as one of the current objects in the shell. To see the methods available for the ghi object you can use the dir command again–only this time put ghi in the parenthesis like this dir(ghi). When you put an object name as a parameter to the dir command you will get a listing of all the methods that the object contains.


[Picture]
Figure 5.3: Using the dir command on an object.

5.2.3 Using Python for Getting Help

This chapter of the manual has detailed description of the scripting commands provided by ChemTree. You can also get a brief form of help from within the Python Shell by using the help() command. You can type help(objectName) with the name of an object in the parenthesis and you will get a help message for the object. You can also get help on any method of an object by putting the object name followed by a period, followed by the method name in the parenthesis of the help command. Looking at the above picture you see that the ghi object has several methods like openProject, newProject and importCSV. To get help on importCSV you would enter help(ghi.importCSV) at the shell prompt.


[Picture]
Figure 5.4: Using the help command.

There are two ways to begin developing customized script routines. You could open a text editor and save your script commands in a .py file. Alternatively you could first open the Python shell window using the Tools->Run Python Shell menu option, enter script commands interactively and later copy them to a .py script file or use the Save button to save the shell’s command history to a .py file.