Commit 1b5f3637 authored by Jose Borreguero's avatar Jose Borreguero
Browse files

add get method and unit test

Signed-off-by: default avatarJose Borreguero <>
parent d6d172dc
......@@ -50,6 +50,15 @@ const InstrumentInfo &getInstrument(ConfigServiceImpl &self, const object &name
return self.getInstrument(ExtractStdString(name)());
/// duck typing emulating dict.get method
std::string getStringUsingCacheElseDefault(ConfigServiceImpl &self, const std::string &key,
const std::string &defaultValue) {
std::string value = self.getString(key, true);
if (value.empty())
return defaultValue;
return value;
// Ignore -Wconversion warnings coming from boost::python
// Seen with GCC 7.1.1 and Boost 1.63.0
......@@ -134,6 +143,8 @@ void export_ConfigService() {
.def("keys", &ConfigServiceImpl::keys, arg("self"))
// Treat this as a dictionary
.def("get", &getStringUsingCacheElseDefault, (arg("self"), arg("key")), arg("default"),
"get the value of a property; return a default value if the property is not found in the configuration")
.def("__getitem__", &getStringUsingCache, (arg("self"), arg("key")))
.def("__setitem__", &ConfigServiceImpl::setString, (arg("self"), arg("key"), arg("value")))
.def("__contains__", &ConfigServiceImpl::hasProperty, (arg("self"), arg("key")))
......@@ -68,13 +68,22 @@ class ConfigServiceTest(unittest.TestCase):
dictcall = config[test_prop]
fncall = config.getString(test_prop)
# Use brackets to retrieve a value
dictcall = config[test_prop]
self.assertEqual(dictcall, fncall)
self.assertNotEqual(config[test_prop], "")
# Use "get" to retrieve a value or a default
dictcall = config.get(test_prop, "other")
self.assertEqual(dictcall, fncall)
dictcall = config.get("property_not_found", "default_alternative")
self.assertEqual(dictcall, "default_alternative")
old_value = fncall
config.setString(test_prop, "1")
self.assertEqual(config.getString(test_prop), "1")
config[test_prop] = "2"
config[test_prop] = "2"
self.assertEqual(config.getString(test_prop), "2")
config.setString(test_prop, old_value)
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment