Skip to content

Commit

Permalink
Simple attribute handling Refs #970
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Apr 15, 2013
1 parent c7307e9 commit d97227e
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -60,15 +60,6 @@ namespace Mantid
/// The PyObject must be supplied to construct the object
DISABLE_DEFAULT_CONSTRUCT(IFunction1DAdapter);
DISABLE_COPY_AND_ASSIGN(IFunction1DAdapter);

/**
* Returns the PyObject that owns this wrapper, i.e. self
* @returns A pointer to self
*/
inline PyObject * getSelf() const { return m_self; }

/// The Python portion of the object
PyObject *m_self;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,19 +45,30 @@ namespace Mantid

/// Returns the name of the function
std::string name() const;
/// Declare all attributes & parameters
void init();

/// Declare an attribute with an initial value
void declareAttribute(const std::string &name, const boost::python::object &defaultValue);
/// Get a named attribute value
PyObject * getAttributeValue(const std::string & name);

protected:
/**
* Returns the PyObject that owns this wrapper, i.e. self
* @returns A pointer to self
*/
inline PyObject * getSelf() const { return m_self; }

private:
/// The PyObject must be supplied to construct the object
DISABLE_DEFAULT_CONSTRUCT(IFunctionAdapter);
DISABLE_COPY_AND_ASSIGN(IFunctionAdapter);

/// The name of the function
std::string m_name;
/// The Python portion of the object
PyObject *m_self;
};
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,12 @@ void export_IFunction()
class_<IFunction, IFunctionAdapter, boost::noncopyable>("IFunction", "Base class for all functions", no_init)
.def("name", &IFunction::name, "Return the name of the function")

.def("initialize", &IFunction::initialize, "Declares any parameters and attributes on the function")

.def("getCategories", &getCategories, "Returns a list of the categories for an algorithm")

.def("nAttributes", &IFunction::nAttributes, "Return the number of attributes (non-fitting arguments)")

.def("nParams", &IFunction::nParams, "Return the number of parameters")

.def("parameterName", &IFunction::parameterName, "Return the name of the ith parameter")
Expand All @@ -61,7 +65,7 @@ void export_IFunction()

.def("declareAttribute", &IFunctionAdapter::declareAttribute, "Declare an attribute with an initial value")

.def("getAttributeValue", &IFunctionAdapter::getAttribute, "Return the value of the ith attribute")
.def("getAttributeValue", &IFunctionAdapter::getAttributeValue, "Return the value of the named attribute")


//-- Deprecated functions that have the wrong names --
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ namespace Mantid
* * @param self A reference to the calling Python object
*/
IFunction1DAdapter::IFunction1DAdapter(PyObject* self)
: API::ParamFunction(), API::IFunction1D(), IFunctionAdapter(self), m_self(self)
: API::ParamFunction(), API::IFunction1D(), IFunctionAdapter(self)
{
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,39 @@
#include "MantidPythonInterface/api/FitFunctions/IFunctionAdapter.h"
#include "MantidPythonInterface/kernel/Environment/CallMethod.h"

#include <boost/python/class.hpp>

namespace Mantid
{
namespace PythonInterface
{
using Mantid::PythonInterface::Environment::CallMethod0;
using namespace boost::python;

/**
* Construct the wrapper and stores the reference to the PyObject
* * @param self A reference to the calling Python object
*/
IFunctionAdapter::IFunctionAdapter(PyObject* self)
: IFunction(), m_name(self->ob_type->tp_name)
: IFunction(), m_name(self->ob_type->tp_name), m_self(self)
{
}

/**
* Returns the class name of the function. This cannot be overridden in Python.
* @returns The class name of the function. This cannot be overridden in Python.
*/
std::string IFunctionAdapter::name() const
{
return m_name;
}

/**
*/
void IFunctionAdapter::init()
{
CallMethod0<void>::dispatchWithException(getSelf(),"init");
}

/**
* Declare an attribute on the given function from a python object
* @param name :: The name of the new attribute
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,12 @@

class PyLinear(IFunction1D):

def init(self):
self.declareAttribute("IntAtt", 1)
self.declareAttribute("DoubleAtt", 3.4)
self.declareAttribute("StringAtt", "filename")
self.declareAttribute("BoolAtt", True)

def function1D(self, xvals, out):
pass

Expand All @@ -21,13 +27,19 @@ def test_instance_can_be_created_from_factory(self):

def test_declareAttribute_only_accepts_known_types(self):
func = PyLinear()
func.declareAttribute("IntAtt", 1)
func.declareAttribute("DoubleAtt", 3.4)
func.declareAttribute("StringAtt", "filename")
func.declareAttribute("BoolAtt", True)

func.initialize() # Contains known types
self.assertEquals(4, func.nAttributes()) # Make sure initialize ran
self.assertRaises(ValueError, func.declareAttribute, "ListAtt", [1,2,3])

def test_correct_attribute_values_are_returned_when_asked(self):
func = PyLinear()
func.initialize() # Contains known types

# By name
self.assertEquals(1, func.getAttributeValue("IntAtt"))
self.assertEquals(3.4, func.getAttributeValue("DoubleAtt"))
self.assertEquals("filename", func.getAttributeValue("StringAtt"))
self.assertEquals(True, func.getAttributeValue("BoolAtt"))

if __name__ == '__main__':
unittest.main()

0 comments on commit d97227e

Please sign in to comment.