Skip to content

Commit

Permalink
Put back methods to return plain strings for unit labels in Python
Browse files Browse the repository at this point in the history
The main Unit::label is deprecated and issues a warnings but scripts
won't break
Refs #9252
  • Loading branch information
martyngigg committed Apr 9, 2014
1 parent a8c8305 commit 531226a
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 6 deletions.
Expand Up @@ -8,6 +8,18 @@ using Mantid::Geometry::IMDDimension;
using Mantid::Geometry::IMDDimension_sptr;
using namespace boost::python;

namespace
{
/**
* @param self A reference to the calling object
* @return A plain-text string giving the units
*/
std::string getUnitsAsStr(IMDDimension & self)
{
return self.getUnits().ascii();
}
}

void export_IMDDimension()
{
register_ptr_to_python<boost::shared_ptr<IMDDimension>>();
Expand All @@ -21,7 +33,7 @@ void export_IMDDimension()
.def("getX", &IMDDimension::getX, "Return coordinate of the axis at the given index")
.def("getDimensionId", &IMDDimension::getDimensionId, "Return a short name which identify the dimension among other dimension."
"A dimension can be usually find by its ID and various ")
.def("getUnits", &IMDDimension::getUnits, "Return the units associated with this dimension.")
.def("getUnits", &getUnitsAsStr, "Return the units associated with this dimension.")
;
}

Expand Up @@ -19,6 +19,16 @@ namespace
return self.caption();
}

/**
* Returns the label of the unit as a std::string & raises a deprecation warning
* @param self A reference to calling object
*/
const std::string deprecatedLabel(Unit & self)
{
PyErr_Warn(PyExc_DeprecationWarning, "'unit.label()' is deprecated, use 'str(unit.symbol())' instead.");
return self.label().ascii();
}

}

void export_Unit()
Expand All @@ -28,7 +38,8 @@ void export_Unit()
class_<Unit,boost::noncopyable>("Unit", no_init)
.def("name", &deprecatedName, "Return the full name of the unit (deprecated, use caption)")
.def("caption", &Unit::caption, "Return the full name of the unit")
.def("label", &Unit::label, "Returns a label to be printed on the axis")
.def("label", &deprecatedLabel, "Returns a plain-text label to be used as the symbol for the unit (deprecated, use symbol())")
.def("symbol", &Unit::label, "Returns a UnitLabel object that holds information on the symbol to use for unit")
.def("unitID", &Unit::unitID, "Returns the string ID of the unit. This may/may not match its name")
;

Expand Down
Expand Up @@ -58,9 +58,9 @@ def test_axes(self):
self.assertEquals(xunit.unitID(), "TOF")

yunit = yaxis.getUnit()
self.assertEquals(yunit.caption(), "")
self.assertEquals(yunit.caption(), "Spectrum")
self.assertEquals(yunit.label(), "")
self.assertEquals(yunit.unitID(), "Empty")
self.assertEquals(yunit.unitID(), "Label")


def test_detector_retrieval(self):
Expand Down
Expand Up @@ -15,7 +15,7 @@ def test_UnitLabel_can_be_built_simple_string_and_unicode_object(self):

def test_utf8_is_converted_to_unicode_object(self):
tof = UnitFactory.Instance().create("TOF")
unit_lbl = tof.label()
unit_lbl = tof.symbol()
self.assertTrue(isinstance(unit_lbl.utf8(), types.UnicodeType))
self.assertEquals(u"\u03bcs", unit_lbl.utf8())

Expand Down
@@ -1,5 +1,5 @@
import unittest
from mantid.kernel import UnitFactory, Unit, Label
from mantid.kernel import UnitFactory, Unit, Label, UnitLabel
import types

class UnitsTest(unittest.TestCase):
Expand All @@ -11,6 +11,7 @@ def test_Label_is_returned_from_Factory(self):
label_unit.setLabel("Temperature", "K")
self.assertEquals("Temperature", label_unit.caption())
self.assertEquals("K", label_unit.label())
self.assertTrue(isinstance(label_unit.symbol(), UnitLabel))

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

0 comments on commit 531226a

Please sign in to comment.