Python

Python Commands

The Python "filter" function can be used in a similar way to idl "where" function.

See other Python functions like "map" and "reduce"

Useful Python links


The Python Pocket Reference Book:


Link to NCAS Python tutorial notes

https://drive.google.com/drive/folders/0B8zznD_2CvEJYk5KcUlLUG80WUk

Andy Heaps Python for IDL user guide: http://www.met.reading.ac.uk/~swsheaps/python.html

CF-Python: http://cfpython.bitbucket.org/

CF-Plot: http://ajheaps.github.io/cf-plot/


Setting up a virtual environment

If needed:

 $ pip install virtualenv

Create project folder

 $ mkdir project_folder
 $ cd project_folder

Setup a virtualenv with a specific version of python and inherit system site packages

 $ virtualenv -p /usr/bin/python2.7 --system-site-packages venv

Make a shortcut file

 $ echo ". venv/bin/activate" > setup_env.sh

Which contains the code to activate the virtual environment

To deactivate the environment simply type

 $ deactivate 

Python debugger

The python de-bugger (pdb) is imported as a package:

import pdb; 

Step through your code from a given point by setting:

pdb.set_trace()

Pdb_Commands.pdf

Some useful commands

To recompile a module without leaving an interactive session

reload(module)

with open options

List of the different modes of opening a file:

r

Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.

rb

Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.

r+

Opens a file for both reading and writing. The file pointer will be at the beginning of the file.

rb+

Opens a file for both reading and writing in binary format. The file pointer will be at the beginning of the file.

w

Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

wb

Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.

w+

Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

wb+

Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.

a

Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

ab

Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.

a+

Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.

ab+

Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.