Skip to content

Commit f4b1d44

Browse files
Merge pull request #38900 from rosswhitfield/fix_addProperty_export
Fix addProperty python export when value is a Property
2 parents ecc009f + 7f59e9d commit f4b1d44

File tree

6 files changed

+86
-4
lines changed

6 files changed

+86
-4
lines changed

Framework/Kernel/inc/MantidKernel/Property.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,8 @@ class MANTID_KERNEL_DLL Property {
104104
const std::type_info *type_info() const;
105105
const std::string type() const;
106106

107+
void setName(const std::string &name);
108+
107109
/// Overridden function that checks whether the property, if not overriden
108110
/// returns ""
109111
virtual std::string isValid() const;

Framework/Kernel/src/Property.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,16 @@ Property::~Property() = default;
5959
*/
6060
const std::string &Property::name() const { return m_name; }
6161

62+
/** Set the property's name
63+
* @param name :: The name of the property
64+
*/
65+
void Property::setName(const std::string &name) {
66+
if (name.empty()) {
67+
throw std::invalid_argument("An empty property name is not permitted");
68+
}
69+
m_name = name;
70+
}
71+
6272
/** Get the property's documentation string
6373
* @return The documentation string
6474
*/

Framework/PythonInterface/mantid/api/src/Exports/Run.cpp

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,15 @@ void addPropertyWithUnit(Run &self, const std::string &name, const bpl::object &
7474
extract<Property *> extractor(value);
7575
if (extractor.check()) {
7676
Property *prop = extractor();
77-
self.addProperty(prop->clone(),
78-
replace); // Clone the property as Python owns the one that is passed in
77+
// Clone the property as Python owns the one that is passed in
78+
auto new_prop = prop->clone();
79+
// set name and units if provided
80+
if (!name.empty())
81+
new_prop->setName(name);
82+
83+
if (!units.empty())
84+
new_prop->setUnits(units);
85+
self.addProperty(new_prop, replace);
7986
return;
8087
}
8188

Framework/PythonInterface/test/python/mantid/api/RunTest.py

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
import unittest
88
import copy
99
from mantid.geometry import Goniometer
10-
from mantid.kernel import DateAndTime, FloatTimeSeriesProperty, TimeROI
10+
from mantid.kernel import DateAndTime, FloatTimeSeriesProperty, TimeROI, FloatPropertyWithValue
1111
from mantid.api import Run
1212
import numpy as np
1313

@@ -181,6 +181,68 @@ def test_equals(self):
181181
other.addProperty("pressure", 1, True)
182182
self.assertNotEqual(self._run, other)
183183

184+
def test_setting_property_from_property(self):
185+
property = FloatPropertyWithValue("property", 42)
186+
property.units = "meters"
187+
188+
run = Run()
189+
self.assertEqual(len(run.getProperties()), 0)
190+
191+
# not setting name or units
192+
run.addProperty("", property, True)
193+
self.assertEqual(len(run.getProperties()), 1)
194+
self.assertTrue("property" in run)
195+
prop = run.getProperty("property")
196+
self.assertEqual(prop.name, "property")
197+
self.assertEqual(prop.value, 42)
198+
self.assertEqual(prop.units, "meters")
199+
200+
# setting name but not units
201+
run.addProperty("prop1", property, True)
202+
self.assertEqual(len(run.getProperties()), 2)
203+
self.assertTrue("prop1" in run)
204+
prop = run.getProperty("prop1")
205+
self.assertEqual(prop.name, "prop1")
206+
self.assertEqual(prop.value, 42)
207+
self.assertEqual(prop.units, "meters")
208+
209+
# setting name and units
210+
run.addProperty("prop2", property, "yards", True)
211+
self.assertEqual(len(run.getProperties()), 3)
212+
self.assertTrue("prop2" in run)
213+
prop = run.getProperty("prop2")
214+
self.assertEqual(prop.name, "prop2")
215+
self.assertEqual(prop.value, 42)
216+
self.assertEqual(prop.units, "yards")
217+
218+
# test __setitem__
219+
run["prop3"] = property
220+
self.assertEqual(len(run.getProperties()), 4)
221+
self.assertTrue("prop3" in run)
222+
prop = run.getProperty("prop3")
223+
self.assertEqual(prop.name, "prop3")
224+
self.assertEqual(prop.value, 42)
225+
self.assertEqual(prop.units, "meters")
226+
227+
# test with time series property
228+
time_series = FloatTimeSeriesProperty("time_series")
229+
time_series.addValue("2008-Jun-17 11:10:44", 1.0)
230+
time_series.addValue("2008-Jun-17 11:10:45", 2.0)
231+
232+
run.addProperty("", time_series, True)
233+
self.assertEqual(len(run.getProperties()), 5)
234+
self.assertTrue("time_series" in run)
235+
tsp = run.getProperty("time_series")
236+
self.assertEqual(tsp.name, "time_series")
237+
self.assertEqual(list(tsp.value), [1, 2])
238+
239+
run.addProperty("other_time_series", time_series, True)
240+
self.assertEqual(len(run.getProperties()), 6)
241+
self.assertTrue("other_time_series" in run)
242+
tsp2 = run.getProperty("other_time_series")
243+
self.assertEqual(tsp2.name, "other_time_series")
244+
self.assertEqual(list(tsp2.value), [1, 2])
245+
184246

185247
if __name__ == "__main__":
186248
unittest.main()

buildconfig/CMake/CppCheck_Suppressions.txt.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,7 @@ unknownMacro:${CMAKE_SOURCE_DIR}/Framework/PythonInterface/mantid/api/src/Export
916916
cstyleCast:${CMAKE_SOURCE_DIR}/Framework/PythonInterface/mantid/api/src/Exports/MDGeometry.cpp:96
917917
unknownMacro:${CMAKE_SOURCE_DIR}/Framework/PythonInterface/mantid/api/src/Exports/MatrixWorkspace.cpp:65
918918
cstyleCast:${CMAKE_SOURCE_DIR}/Framework/PythonInterface/mantid/api/src/Exports/MultipleExperimentInfos.cpp:36
919-
syntaxError:${CMAKE_SOURCE_DIR}/Framework/PythonInterface/mantid/api/src/Exports/Run.cpp:251
919+
syntaxError:${CMAKE_SOURCE_DIR}/Framework/PythonInterface/mantid/api/src/Exports/Run.cpp:258
920920
syntaxError:${CMAKE_SOURCE_DIR}/Framework/PythonInterface/mantid/api/src/Exports/Sample.cpp:81
921921
syntaxError:${CMAKE_SOURCE_DIR}/Framework/PythonInterface/mantid/api/src/Exports/SpectrumDefinition.cpp:40
922922
cstyleCast:${CMAKE_SOURCE_DIR}/Framework/PythonInterface/mantid/api/src/Exports/SpectrumInfo.cpp:87
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
- Fixed a bug where the method :meth:`mantid.api.Run.addProperty` was ignoring the ``name`` and ``units`` parameters if the ``value`` was of type :class:`mantid.kernel.Property`. Now only if the ``name`` and ``units`` are empty will the existing values on the ``Property`` be used.

0 commit comments

Comments
 (0)