PythonBasics: Difference between revisions
Line 18: | Line 18: | ||
=== Pasting Code === | === Pasting Code === | ||
We will fairly rapidly reach the point where we want to cut and paste somewhat complex blocks of code. Python's use of indentation to implement code structure can make for awkward interactions with the shell at times. Even when it goes well after cutting and pasting a 'for' loop you often have to press return twice to execute. In very complex cases, you may end up needing to paste a line at a time. | |||
Fortunately, the iPython shell offers a very nice way around these challenges. You can type | |||
<source lang="python"> | |||
cpaste | |||
</source> | |||
to initiate a 'code paste'. You will then be able to paste code directly into the shell and the code will appear exactly as you copied it. When you have pasted all of your code got to a new line and type "--". This ends the code paste and the pasted code should then execute. | |||
=== Getting Help, Exploring Objects === | === Getting Help, Exploring Objects === |
Revision as of 14:41, 10 October 2011
We are porting, updating, and editing this [1]
Tips and Tricks
Environment
We could write a lot on setting up python, a nice shell, installing key packages, etc. Because this is a CASA guide, we can short circuit some of this. CASA installs with it's own nice iPython shell and a core set of third-party packages (numpy, scipy, pylab, matplotlib). We will assume that you have downloaded and installed CASA from here and return to the issue of additional third-party software down the road.
Once you have CASA installed you can start a new session from your shell prompt by just typing
casapy
Depending on your computing environment you likely also have some version of python and perhaps iPython installed, but the versions of these (especially the pre-installed version on the Mac) can vary widely. If you are interested in a non-CASA distribution that folds in a similar (in fact more extensive) suite of packages you might look at the free academic version here (no promises). In the interests of version control, we focus our discussion on the version of python that comes with CASA (2.6).
Pasting Code
We will fairly rapidly reach the point where we want to cut and paste somewhat complex blocks of code. Python's use of indentation to implement code structure can make for awkward interactions with the shell at times. Even when it goes well after cutting and pasting a 'for' loop you often have to press return twice to execute. In very complex cases, you may end up needing to paste a line at a time.
Fortunately, the iPython shell offers a very nice way around these challenges. You can type
cpaste
to initiate a 'code paste'. You will then be able to paste code directly into the shell and the code will appear exactly as you copied it. When you have pasted all of your code got to a new line and type "--". This ends the code paste and the pasted code should then execute.
Getting Help, Exploring Objects
Things That Work Only in the Shell
Simple Variables
Basic Math
Ints and Floats
Booleans
Deleting Variables
Checking Whether a Variable Exists
Data Collections: Lists
Lists are one of the fundamental data collections in python (we will also discuss dictionaries and you may want to read about tuples and sets).
Data Collections: Dictionaries
Dictionaries are another basic python data collection. Dictionaries store data in pairs, with a set of unique keys each mapped to some value (the values do not have to be unique). Dictionaries are extremely useful in scripting. For example, imagine that we are reducing a complex set of observations and want to be able to look up a the calibrator associated with a given source.
Control Flow
If
While
For
Break/Continue
More Complex Programs
Executing Scripts
The most basic way to execute a set of python commands (aside from just copying and pasting to the shell) is to use the execfile command. Calling execfile('myscript.py') from inside a python shell will execute 'myscript.py' one line at a time. You can use this to run a series of reduction commands or other simple scripts. In fact, calling execfile on one or more scripts will almost certainly be sufficient to script most basic CASA data reductions.
A simple example
You can combine the control flow that we learned above with execfile to refine your scripts. For example, you might have a sophisticated reduction path that requires a few user inputs, which could be collected at the top of the script as variables. The reduction might have several discrete parts, which you could turn on or off using booleans and if statements. As an example, try creating a file that holds the following:
An example with a bit of control flow.
As you edit the variables and booleans, various parts of the script will run tuned by the variables you set. This simple but powerful approach can (if you desire) form the infrastructure for a lot of your CASA reduction scripting.
Functions
Python allows you to define functions either from the command line (or an execfile call) or as part of modules.