\chapter{MX for Python}

The MX binding for the Python language is called MP.  The current
version of MP implements
methods for most of the MX area detector functions.  At present,
a complete manual
for MP has not yet been written.  However, most of the
Python methods are a one-to-one map to the underlying C functions,
so the C documentation in Chapters~\ref{ch:areadetectorapi} and 
\ref{ch:imageapi} can be used directly to explain the Python methods.

The MP methods themselves are defined in the Python module \textit{Mp}.
If MX and MP have been installed to the default location of
\myfilename{/opt/mx}, the source for the \textit{Mp} module can
be found in the file \myfilename{/opt/mx/lib/Mp.py}.  Alternately, you
can find the \textit{Mp} module file in the MP source distribution 
at the location \myfilename{mp/libMp/Mp.py}.

The main classes to look at in \myfilename{Mp.py} are the Mp.RecordList
class, the Mp.Record class, the Mp.AreaDetector class and the
Mp.ImageFrame class.  In addition, there are some examples in the
\myfilename{mp/examples} directory.  That directory contains three
subdirectories.  The \myfilename{mp/examples/simple} directory contain 
example scripts that are designed to work on Linux/Unix systems.

The scripts found in the \myfilename{mp/examples/mpscript} make use
of a front end Python script called \textit{mpscript} that is
normally installed at \myfilename{/opt/mx/bin/mpscript}.
\textit{mpscript} makes it easy to write scripts that will operate
unchanged on both Windows and Linux/Unix systems.  Here is
an example \textit{mpscript}-based script that reports the position
of an MX motor.
\begin{verbatim}
#! /usr/bin/env mpscript
# 
# This script reports the position of the requested motor.
#

def main( record_list, argv ):

	if ( len(argv) != 1 ):
		print ""
		print "Usage:  mp_get_position motorname"
		print ""
		sys.exit(0);

	motor_name = argv[0]

	motor = record_list.get_record( motor_name )

	position = motor.get_position()

	units = motor.get_field("units")

	print "Motor '%s' position = %g %s" % (motor_name, position, units)

\end{verbatim}

The primary thing to notice is that all you have to do is create a
\texttt{main} procedure that is passed two arguments.  They are
\texttt{record\_list} which is a object that encapsulates the MX database
and \texttt{argv} which contains the command line arguments that were
passed to the script.
Unfortunately, there are not yet any area detector-specific script
in the \textit{Mp} examples directory, but that should be rectified soon.

