Skip to content

Commit

Permalink
Add a setCell function to ITableWorkspace export. Refs #3030
Browse files Browse the repository at this point in the history
  • Loading branch information
martyngigg committed Mar 19, 2012
1 parent ac18744 commit 96b2489
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 27 deletions.
Expand Up @@ -18,6 +18,7 @@ using Mantid::API::ITableWorkspace;
using Mantid::API::ITableWorkspace_sptr;
using Mantid::API::TableRow;
using Mantid::API::Column_sptr;
using Mantid::API::Column_const_sptr;
using Mantid::API::Workspace;
using Mantid::Kernel::DataItem_sptr;
using namespace boost::python;
Expand Down Expand Up @@ -96,38 +97,12 @@ namespace
}
}

/**
* Access a cell and return a corresponding Python type
* @param self A reference to the TableWorkspace python object that we were called on
* @param value A python object containing either a row index or a column name
* @param row_or_col An integer giving the row if value is a string or the column if value is an index
*/
PyObject * cell(ITableWorkspace &self, bpl::object value, int row_or_col)
{
// Find the column and row
Mantid::API::Column_const_sptr column;
int row(-1);
if( PyString_Check(value.ptr()) )
{
column = self.getColumn( extract<std::string>(value)());
row = row_or_col;
}
else
{
row = extract<int>(value)();
column = self.getColumn(row_or_col);
}

const std::type_info & typeID = column->get_type_info();
return getValue(column, typeID, row);
}

/**
* Access a cell and return a corresponding Python type
* @param self A reference to the TableWorkspace python object that we were called on
* @param value A python object containing a column name or index
*/
PyObject * column(ITableWorkspace &self, bpl::object value)
PyObject * column(ITableWorkspace &self, const bpl::object & value)
{
// Find the column and row
Mantid::API::Column_const_sptr column;
Expand Down Expand Up @@ -256,6 +231,58 @@ namespace
}
}

/**
* @param self A reference to the TableWorkspace python object that we were called on
* @param value A python object containing either a row index or a column name
* @param row_or_col An integer giving the row if value is a string or the column if value is an index
* @param column [Out]:: The column pointer will be stored here
* @param rowIndex [Out]:: The row index will be stored here
*/
void getCellLoc(ITableWorkspace &self, const bpl::object & col_or_row, const int row_or_col,
Column_sptr &column, int &rowIndex)
{
if( PyString_Check(col_or_row.ptr()) )
{
column = self.getColumn( extract<std::string>(col_or_row)());
rowIndex = row_or_col;
}
else
{
rowIndex = extract<int>(col_or_row)();
column = self.getColumn(row_or_col);
}
}

/**
* Returns an appropriate Python object for the value at the given cell
* @param self A reference to the TableWorkspace python object that we were called on
* @param value A python object containing either a row index or a column name
* @param row_or_col An integer giving the row if value is a string or the column if value is an index
*/
PyObject * cell(ITableWorkspace &self, const bpl::object & value, int row_or_col)
{
// Find the column and row
Mantid::API::Column_sptr column;
int row(-1);
getCellLoc(self, value, row_or_col, column, row);
const std::type_info & typeID = column->get_type_info();
return getValue(column, typeID, row);
}

/**
* Sets the value of the given cell
* @param self A reference to the TableWorkspace python object that we were called on
* @param value A python object containing either a row index or a column name
* @param row_or_col An integer giving the row if value is a string or the column if value is an index
*/
void setCell(ITableWorkspace &self, const bpl::object & col_or_row, const int row_or_col,
const bpl::object & value)
{
Mantid::API::Column_sptr column;
int row(-1);
getCellLoc(self, col_or_row, row_or_col, column, row);
setValue(column, row, value);
}
}

void export_ITableWorkspace()
Expand Down Expand Up @@ -303,6 +330,9 @@ void export_ITableWorkspace()

.def("cell", &cell, "Return the given cell. If the first argument is a "
"number then it is interpreted as a row otherwise it is interpreted as a column name")

.def("setCell", &setCell, "Sets the value of a given cell. If the first argument is a "
"number then it is interpreted as a row otherwise it is interpreted as a column name")
;

REGISTER_SINGLEVALUE_HANDLER(ITableWorkspace_sptr);
Expand Down
Expand Up @@ -54,6 +54,18 @@ def test_table_is_resized_correctly(self):
self.assertEquals(len(table), 5)
table.addColumn(type="int",name="index")
self.assertEquals(table.columnCount(), 1)

def test_setcell_sets_the_correct_cell(self):
test_table = self._create_test_table()
data = '11'
col = 1
row = 2
test_table.setCell(row, col, data)
self.assertEquals(test_table.cell(row,col), data)
data = '12'
col = 'name'
test_table.setCell(col, row, data)
self.assertEquals(test_table.cell(col,row), data)

def test_adding_table_data_using_dictionary(self):
table = WorkspaceFactory.createTable()
Expand Down

0 comments on commit 96b2489

Please sign in to comment.