PythonBasics: Difference between revisions

From CASA Guides
Jump to navigationJump to search
Line 33: Line 33:
=== Executing Scripts ===
=== 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.
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''
''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 ===
=== Functions ===

Revision as of 21:42, 9 October 2011

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.

Modules

Object Oriented Aspects

Tips and Tricks

Pasting Code

Getting Help, Exploring Objects

Things That Work Only in the Shell