From 18df29ff19648a68decff2c4b28e82448bbff99e Mon Sep 17 00:00:00 2001 From: Ger van Diepen <diepen@astron.nl> Date: Mon, 5 Mar 2012 10:55:24 +0000 Subject: [PATCH] Task #3116, #2990 Added python support for new getRecord functions Improved getVector functions Added functions fullModuleName and locateModule --- LCS/pyparameterset/src/__init__.py | 42 ++++++++++-- LCS/pyparameterset/src/pyparameterset.cc | 84 +++++++++++++++++++++--- 2 files changed, 111 insertions(+), 15 deletions(-) diff --git a/LCS/pyparameterset/src/__init__.py b/LCS/pyparameterset/src/__init__.py index e9f1f9e551e..ea0a3a30262 100755 --- a/LCS/pyparameterset/src/__init__.py +++ b/LCS/pyparameterset/src/__init__.py @@ -1,4 +1,4 @@ -# __init__.py: Top level .py file for python parameterset interface +# __init__.py: Top level .py file for python parametersetinterface # Copyright (C) 2008 # ASTRON (Netherlands Institute for Radio Astronomy) # P.O.Box 2, 7990 AA Dwingeloo, The Netherlands @@ -19,16 +19,16 @@ # # $Id$ -from _pyparameterset import ParameterValue +from _pyparameterset import PyParameterValue from _pyparameterset import PyParameterSet -class parametervalue(ParameterValue): +class parametervalue(PyParameterValue): """ The Python interface to ParameterValue """ - def __init__(self, value, trim=True): + def __init__(self, value, trim=True, _copyObj=False): """ Create the parametervalue object. value @@ -37,12 +37,24 @@ class parametervalue(ParameterValue): True = remove leading/trailing whitespace from value. """ - ParameterValue.__init__ (self, value, trim); + if _copyObj == True: + # Copy constructor + PyParameterValue.__init__ (self, value) + else: + PyParameterValue.__init__ (self, value, trim); def __str__(self): """Get the full parameter value.""" return self.get() + def getVector(self): + """Get the value as a vector of values.""" + return [parametervalue(v, _copyObj=True) for v in self._getVector()] + + def getRecord(self): + """Get the value as a record (as a parameterset object).""" + return parameterset (self._getRecord(), _copyObj=True) + @@ -78,7 +90,7 @@ class parameterset(PyParameterSet): def __getitem__(self, key): """Get the parametervalue object of a parameter.""" - return self._get (key) + return parametervalue (self._get(key), _copyObj=True) def makeSubset (self, baseKey, prefix=''): """Return a subset as a new parameterset object. @@ -101,13 +113,29 @@ class parameterset(PyParameterSet): ps = self._makeSubset (baseKey, prefix) return parameterset (ps, _copyObj=True) + def getVector(self, key): + """Get the value as a vector of values.""" + return [parametervalue(v, _copyObj=True) for v in self._getVector(key)] + + def getRecord (self, key): + """Get the value as a record.""" + ps = self._getRecord (key) + return parameterset (ps, _copyObj=True) + def keys(self): """Get a sorted list of all parameter names.""" return self.keywords() + def dict(self): + """Turn the parset into a dict""" + d = {} + for key in self.keys(): + d[key] = self.getString(key) + return d + def get(self, key): """Get the parametervalue object of a parameter.""" - return self._get (key) + return parametervalue (self._get(key), _copyObj=True) def getBoolVector(self, key, default=None, expandable=False): """Get the value as a list of boolean values. diff --git a/LCS/pyparameterset/src/pyparameterset.cc b/LCS/pyparameterset/src/pyparameterset.cc index 5e1df7c8911..0cceb0922df 100644 --- a/LCS/pyparameterset/src/pyparameterset.cc +++ b/LCS/pyparameterset/src/pyparameterset.cc @@ -21,6 +21,7 @@ #include <lofar_config.h> #include <Common/ParameterSet.h> +#include <Common/ParameterRecord.h> #include <pytools/PycExcp.h> #include <pytools/PycBasicData.h> #include <boost/python.hpp> @@ -33,6 +34,32 @@ using namespace boost::python; namespace LOFAR { + //# Forward declarations. + class PyParameterSet; + class PyParameterValue; + + // Declare function to convert vector of ParameterValue to PyParameterValue. + vector<PyParameterValue> convertPVVector (const vector<ParameterValue>& vec); + + //# Make a class PyParameterSet to be able to add the function version(). + //# We also need a PyParameterValue. + class PyParameterValue: public ParameterValue + { + public: + PyParameterValue() + : ParameterValue() + {} + PyParameterValue (const string& value, bool trim=true) + : ParameterValue (value, trim) + {} + PyParameterValue (const ParameterValue& pvalue) + : ParameterValue (pvalue) + {} + vector<PyParameterValue> getVector() const + { return convertPVVector (ParameterValue::getVector()); } + PyParameterSet getRecord() const; + }; + class PyParameterSet : public ParameterSet { public: @@ -55,6 +82,15 @@ namespace LOFAR { PyParameterSet makeSubset (const string& baseKey, const string& prefix) { return ParameterSet::makeSubset (baseKey, prefix); } + PyParameterValue get (const string& key) const + { return ParameterSet::get (key); } + + vector<PyParameterValue> getVector (const string& key) const + { return convertPVVector (ParameterSet::getVector(key)); } + + PyParameterSet getRecord (const string& key) + { return ParameterSet::getRecord (key); } + // Return a sorted list of keywords. vector<string> keywords() const { @@ -67,6 +103,20 @@ namespace LOFAR { return result; } }; + + inline PyParameterSet PyParameterValue::getRecord() const + { return ParameterValue::getRecord(); } + + vector<PyParameterValue> convertPVVector (const vector<ParameterValue>& vec) + { + vector<PyParameterValue> out; + out.reserve (vec.size()); + for (vector<ParameterValue>::const_iterator iter=vec.begin(); + iter!=vec.end(); ++iter) { + out.push_back (*iter); + } + return out; + } // Define function pointers for overloaded functions to be able to tell // boost-python which function to take. @@ -124,9 +174,10 @@ namespace LOFAR { // Define the python interface to ParameterValue. void pyparametervalue() { - class_<ParameterValue> ("ParameterValue", - init<std::string, bool>()) + class_<PyParameterValue> ("PyParameterValue") + .def (init<PyParameterValue>()) + .def (init<std::string, bool>()) .def ("get", &ParameterValue::get, return_value_policy < copy_const_reference> (), "Get the original value.") @@ -134,8 +185,12 @@ namespace LOFAR { "Expand possible range and repeat values (using .. and *)") .def ("isVector", &ParameterValue::isVector, "Test if the value contains a vector (if enclosed in [])") - .def ("getVector", &ParameterValue::getVector, - "Split the vector into its parts and return as a list.") + .def ("isRecord", &ParameterValue::isRecord, + "Test if the value contains a record (if enclosed in {})") + .def ("_getVector", &PyParameterValue::getVector, + "Split the vector value into its parts and return as a list.") + .def ("_getRecord", &PyParameterValue::getRecord, + "Split the record value into its parts and return as a PyParameterSet.") .def ("getBool", &ParameterValue::getBool, "Get the value as a boolean value.") .def ("getInt", &ParameterValue::getInt, @@ -196,16 +251,29 @@ namespace LOFAR { boost::python::arg("value")), "Replace the value of a parameter.") .def ("remove", &ParameterSet::remove, - (boost::python::arg("key"))) + (boost::python::arg("key")), + "Remove a parameter.") .def ("clear", &ParameterSet::clear, "Clear this parameterset object.") + .def ("locateModule", &ParameterSet::locateModule, + (boost::python::arg("key")), + "Search for a module whose name ends in the given modulename.") + .def ("fullModuleName", &ParameterSet::fullModuleName, + (boost::python::arg("key")), + "Search the module name or module hierarchy and return its full position.") .def ("isDefined", &ParameterSet::isDefined, (boost::python::arg("key")), "Does a parameter with the given name exist? ") - .def ("_get", &ParameterSet::get, - return_value_policy < copy_const_reference> (), + .def ("_get", &PyParameterSet::get, + /// return_value_policy < copy_const_reference> (), (boost::python::arg("key"))) + .def ("_getVector", &PyParameterSet::getVector, + (boost::python::arg("key")), + "Get a vector of parameter values. Exception if undefined.") + .def ("_getRecord", &PyParameterSet::getRecord, + (boost::python::arg("key")), + "Get a parameter record. Exception if undefined.") .def ("getBool", fgetbool1, (boost::python::arg("key")), "Get a boolean parameter value. Exception if undefined.") @@ -289,7 +357,7 @@ BOOST_PYTHON_MODULE(_pyparameterset) { LOFAR::pytools::register_convert_excp(); LOFAR::pytools::register_convert_basicdata(); - LOFAR::pytools::register_convert_std_vector<LOFAR::ParameterValue>(); + LOFAR::pytools::register_convert_std_vector<LOFAR::PyParameterValue>(); LOFAR::pytools::register_convert_std_vector<bool>(); LOFAR::pytools::register_convert_std_vector<int>(); LOFAR::pytools::register_convert_std_vector<float>(); -- GitLab