Skip to content

Commit

Permalink
Refs #4333. Tidy up self usage in python exports
Browse files Browse the repository at this point in the history
Use a reference to the class rather than a python object, that way the
compiler won't let the function be called on an incorrect type rather
than it being checked at runtime
  • Loading branch information
martyngigg committed Dec 14, 2011
1 parent bc5fb03 commit bbde603
Show file tree
Hide file tree
Showing 6 changed files with 51 additions and 50 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,28 +26,33 @@

namespace Mantid
{
namespace API
{
class MatrixWorkspace;
}

namespace PythonInterface
{
namespace Numpy
{
//** @name Numpy read-only wrappers */
///@{
/// Create a numpy wrapper around the original X values at the given index
PyObject *wrapX(PyObject *self, const size_t index);
PyObject *wrapX(API::MatrixWorkspace &self, const size_t index);
/// Create a numpy wrapper around the original Y values at the given index
PyObject *wrapY(PyObject *self, const size_t index);
PyObject *wrapY(API::MatrixWorkspace &self, const size_t index);
/// Create a numpy wrapper around the original X values at the given index
PyObject *wrapE(PyObject *self, const size_t index);
PyObject *wrapE(API::MatrixWorkspace &self, const size_t index);
///@}

//** @name Numpy clones of data*/
///{
/// Create a numpy array from the X values of the given workspace reference
PyObject *cloneX(PyObject * self);
PyObject *cloneX(API::MatrixWorkspace &self);
/// Create a numpy array from the Y values of the given workspace reference
PyObject *cloneY(PyObject * self);
PyObject *cloneY(API::MatrixWorkspace &self);
/// Create a numpy array from the E values of the given workspace reference
PyObject *cloneE(PyObject * self);
PyObject *cloneE(API::MatrixWorkspace &self);
///@}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,19 @@
Code Documentation is available at: <http://doxygen.mantidproject.org>
*/
#include "MantidKernel/System.h"

#include <boost/python/object.hpp>

#include <string>

namespace Mantid
{
//---------------------------------------------------------------------------
// Forward declaration
//---------------------------------------------------------------------------
namespace Kernel
{
class IPropertyManager;
}

namespace PythonInterface
{

Expand Down Expand Up @@ -70,7 +76,7 @@ namespace Mantid
/// Insert a new property handler
DLLExport void registerHandler(PyTypeObject* typeObject, PropertyHandler* handler);
/// This static function allows a call to a method on an IPropertyManager object
DLLExport void setProperty(boost::python::object self, const std::string & name,
DLLExport void setProperty(Kernel::IPropertyManager &self, const std::string & name,
boost::python::object value);
/// Upcast an item from a DataItem to the most exported known type
DLLExport void upcastFromDataItem(boost::python::object value);
Expand Down
18 changes: 8 additions & 10 deletions Code/Mantid/Framework/PythonInterface/mantid/api/src/Algorithm.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,10 +51,9 @@ namespace
* @return A Python list of strings
*/

PyObject * getInputPropertiesWithMandatoryFirst(boost::python::object self)
PyObject * getInputPropertiesWithMandatoryFirst(IAlgorithm & self)
{
IAlgorithm_sptr algm = boost::python::extract<IAlgorithm_sptr>(self);
PropVector properties(algm->getProperties()); // Makes a copy so that it can be sorted
PropVector properties(self.getProperties()); // Makes a copy so that it can be sorted
std::sort(properties.begin(), properties.end(), MandatoryFirst());
PropVector::const_iterator iend = properties.end();
// Build a python list
Expand All @@ -75,10 +74,9 @@ namespace
* @param self :: A pointer to the python object wrapping and Algorithm.
* @return A Python list of strings
*/
PyObject * getOutputProperties(boost::python::object self)
PyObject * getOutputProperties(IAlgorithm & self)
{
IAlgorithm_sptr algm = boost::python::extract<IAlgorithm_sptr>(self);
const PropVector & properties(algm->getProperties()); // No copy
const PropVector & properties(self.getProperties()); // No copy
PropVector::const_iterator iend = properties.end();
// Build the list
PyObject *names = PyList_New(0);
Expand All @@ -101,19 +99,19 @@ namespace
* @param self :: A pointer to the python object wrapping and Algorithm
* @return A string that documents an algorithm
*/
std::string createDocString(boost::python::object self)
std::string createDocString(IAlgorithm & self)
{
IAlgorithm_sptr algm = boost::python::extract<IAlgorithm_sptr>(self);
//IAlgorithm_sptr algm = boost::python::extract<IAlgorithm_sptr>(self);
const std::string EOL="\n";

// Put in the quick overview message
std::stringstream buffer;
std::string temp = algm->getOptionalMessage();
std::string temp = self.getOptionalMessage();
if (temp.size() > 0)
buffer << temp << EOL << EOL;

// get a sorted copy of the properties
std::vector<Property*> properties(algm->getProperties());
std::vector<Property*> properties(self.getProperties());
std::sort(properties.begin(), properties.end(), MandatoryFirst());
const size_t numProps(properties.size());

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,15 @@ namespace
* @param self :: Enables it to be called as a member function on the AlgorithmFactory class
* @param includeHidden :: If true hidden algorithms are included
*/
PyObject * getRegisteredAlgorithms(PyObject * self, bool includeHidden)
PyObject * getRegisteredAlgorithms(AlgorithmFactoryImpl & self, bool includeHidden)
{
UNUSED_ARG(self);
AlgorithmFactoryImpl & algFactory = AlgorithmFactory::Instance();
// A list of strings AlgorithmName|version
std::vector<std::string> keys = algFactory.getKeys(includeHidden);
std::vector<std::string> keys = self.getKeys(includeHidden);
const size_t nkeys = keys.size();
PyObject *registered = PyDict_New();
for( size_t i = 0; i < nkeys; ++i )
{
std::pair<std::string, int> algInfo = algFactory.decodeName(keys[i]);
std::pair<std::string, int> algInfo = self.decodeName(keys[i]);
PyObject *name = PyString_FromString(algInfo.first.c_str());
PyObject *vers = PyInt_FromLong(algInfo.second);
if( PyDict_Contains(registered, name) == 1 )
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ namespace Mantid
* @param endp1 :: One past the end index in the workspace to finish at when reading the data (similar to .end() for STL)
*
*/
PyArrayObject *cloneArray(MatrixWorkspace_sptr workspace,
PyArrayObject *cloneArray(MatrixWorkspace & workspace,
DataField field, const size_t start, const size_t endp1)
{
const size_t numHist = endp1 - start;
Expand All @@ -47,12 +47,12 @@ namespace Mantid
ArrayAccessFn dataAccesor;
if( field == XValues )
{
stride = workspace->readX(0).size();
stride = workspace.readX(0).size();
dataAccesor = &MatrixWorkspace::readX;
}
else
{
stride = workspace->blocksize();
stride = workspace.blocksize();
if(field == YValues) dataAccesor = &MatrixWorkspace::readY;
else dataAccesor = &MatrixWorkspace::readE;
}
Expand All @@ -66,7 +66,7 @@ namespace Mantid
double *dest = (double*)PyArray_DATA(nparray); // HEAD of the contiguous numpy data array
for(size_t i = start; i < endp1; ++i)
{
const MantidVec & src = ((*workspace).*(dataAccesor))(i);
const MantidVec & src = (workspace.*(dataAccesor))(i);
std::copy(src.begin(), src.end(), dest);
dest += stride; // Move the ptr to the start of the next 1D array
}
Expand All @@ -78,24 +78,22 @@ namespace Mantid
// -------------------------------------- Read-only arrays---------------------------------------------------
/*
* Create a numpy wrapper around the original X values at the given index
* @param self :: A pointer to a PyObject representing the calling object
* @param self :: A reference to the calling object
* @param index :: The index into the workspace
*/
PyObject *wrapX(PyObject *self, const size_t index)
PyObject *wrapX(MatrixWorkspace &self, const size_t index)
{
MatrixWorkspace_sptr workspace = bpl::extract<MatrixWorkspace_sptr>(self);
PyArrayObject *nparray = (PyArrayObject*)wrapWithReadOnlyNumpy(workspace->readX(index));
PyArrayObject *nparray = (PyArrayObject*)wrapWithReadOnlyNumpy(self.readX(index));
return (PyObject*)nparray;
}
/*
* Create a numpy wrapper around the original Y values at the given index
* @param self :: A pointer to a PyObject representing the calling object
* @param self :: A reference to the calling object
* @param index :: The index into the workspace
*/
PyObject *wrapY(PyObject *self, const size_t index)
PyObject *wrapY(MatrixWorkspace & self, const size_t index)
{
MatrixWorkspace_sptr workspace = bpl::extract<MatrixWorkspace_sptr>(self);
PyArrayObject *nparray = (PyArrayObject*)wrapWithReadOnlyNumpy(workspace->readY(index));
PyArrayObject *nparray = (PyArrayObject*)wrapWithReadOnlyNumpy(self.readY(index));
return (PyObject*)nparray;
}

Expand All @@ -104,10 +102,9 @@ namespace Mantid
* @param self :: A pointer to a PyObject representing the calling object
* @param index :: The index into the workspace
*/
PyObject *wrapE(PyObject *self, const size_t index)
PyObject *wrapE(MatrixWorkspace & self, const size_t index)
{
MatrixWorkspace_sptr workspace = bpl::extract<MatrixWorkspace_sptr>(self);
PyArrayObject *nparray = (PyArrayObject*)wrapWithReadOnlyNumpy(workspace->readE(index));
PyArrayObject *nparray = (PyArrayObject*)wrapWithReadOnlyNumpy(self.readE(index));
return (PyObject*)nparray;
}

Expand All @@ -118,31 +115,28 @@ namespace Mantid
* @param self :: A pointer to a PyObject representing the calling object
* @return A 2D numpy array created from the X values
*/
PyObject * cloneX(PyObject * self)
PyObject * cloneX(MatrixWorkspace &self)
{
MatrixWorkspace_sptr workspace = bpl::extract<MatrixWorkspace_sptr>(self);
return (PyObject*)cloneArray(workspace, XValues, 0, workspace->getNumberHistograms());
return (PyObject*)cloneArray(self, XValues, 0, self.getNumberHistograms());
}
/* Create a numpy array from the Y values of the given workspace reference
* This acts like a python method on a Matrixworkspace object
* @param self :: A pointer to a PyObject representing the calling object
* @return A 2D numpy array created from the Y values
*/
PyObject * cloneY(PyObject * self)
PyObject * cloneY(MatrixWorkspace &self)
{
MatrixWorkspace_sptr workspace = bpl::extract<MatrixWorkspace_sptr>(self);
return (PyObject*)cloneArray(workspace, YValues, 0, workspace->getNumberHistograms());
return (PyObject*)cloneArray(self, YValues, 0, self.getNumberHistograms());
}

/* Create a numpy array from the E values of the given workspace reference
* This acts like a python method on a Matrixworkspace object
* @param self :: A pointer to a PyObject representing the calling object
* @return A 2D numpy array created from the E values
*/
PyObject * cloneE(PyObject * self)
PyObject * cloneE(MatrixWorkspace &self)
{
MatrixWorkspace_sptr workspace = bpl::extract<MatrixWorkspace_sptr>(self);
return (PyObject*)cloneArray(workspace, EValues, 0, workspace->getNumberHistograms());
return (PyObject*)cloneArray(self, EValues, 0, self.getNumberHistograms());
}

}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ namespace Mantid
* @param name :: The name of the property
* @param value :: The value of the property as a bpl object
*/
void setProperty(bpl::object self, const std::string & name, bpl::object value)
void setProperty(IPropertyManager &self, const std::string & name, bpl::object value)
{
// Find type handler
PyTypeObject *pytype = value.ptr()->ob_type;
PyTypeLookup::const_iterator itr = typeHandlers.find(pytype);
if( itr != typeHandlers.end() )
{
itr->second->set(bpl::extract<IPropertyManager*>(self), name, value);
itr->second->set(&self, name, value);
}
else
{
Expand Down

0 comments on commit bbde603

Please sign in to comment.