Skip to content

Commit

Permalink
Export Unit::utf8Label to Python.
Browse files Browse the repository at this point in the history
It returns a standard Python unicode string.
Refs #9252
  • Loading branch information
martyngigg committed Apr 3, 2014
1 parent 279a4cc commit fffb57e
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
Expand Up @@ -18,6 +18,16 @@ namespace
PyErr_Warn(PyExc_DeprecationWarning, "'name' is deprecated, use 'caption' instead.");
return self.caption();
}

/**
* @param self A reference to the calling object
* @return A new Python unicode string with the contents of the utf8Label
*/
PyObject * utf8LabelToUnicode(Unit & self)
{
const auto label = self.utf8Label();
return PyUnicode_FromWideChar(label.c_str(), label.size());
}
}

void export_Unit()
Expand All @@ -28,6 +38,7 @@ void export_Unit()
.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("utf8Label", &utf8LabelToUnicode, "Returns a unicode string to be printed on the axis")
.def("unitID", &Unit::unitID, "Returns the string ID of the unit. This may/may not match its name")
;

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

class UnitsTest(unittest.TestCase):

Expand All @@ -11,5 +12,12 @@ def test_Label_is_returned_from_Factory(self):
self.assertEquals("Temperature", label_unit.caption())
self.assertEquals("K", label_unit.label())

def test_utf8Label_is_converted_to_unicode_object(self):
tof = UnitFactory.Instance().create("TOF")
unit_lbl = tof.utf8Label()
self.assertTrue(isinstance(unit_lbl, types.UnicodeType))
self.assertEquals(u"\u03bcs", unit_lbl)


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

0 comments on commit fffb57e

Please sign in to comment.