diff --git a/include/GApplication.hpp b/include/GApplication.hpp index 69474f1..3a263b0 100644 --- a/include/GApplication.hpp +++ b/include/GApplication.hpp @@ -59,6 +59,8 @@ public: // Operators GApplication& operator=(const GApplication& app); + GApplicationPar& operator[](const int& index); + const GApplicationPar& operator[](const int& index) const; GApplicationPar& operator[](const std::string& name); const GApplicationPar& operator[](const std::string& name) const; @@ -270,4 +272,32 @@ void GApplication::pars(const GApplicationPars& pars) return; } + +/***********************************************************************//** + * @brief Return pointer to application parameter + * + * @param[in] index Parameter index [0,...,m_pars.size()-1]. + * + * Returns a pointer to the application parameter with the specified @p index. + ***************************************************************************/ +inline +GApplicationPar& GApplication::operator[](const int& index) +{ + return (m_pars[index]); +} + + +/***********************************************************************//** + * @brief Return pointer to application parameter (const version) + * + * @param[in] index Model index [0,...,m_pars.size()-1]. + * + * Returns a const pointer to the parameter with the specified @p index. + ***************************************************************************/ +inline +const GApplicationPar& GApplication::operator[](const int& index) const +{ + return (m_pars[index]); +} + #endif /* GAPPLICATION_HPP */ diff --git a/pyext/GApplication.i b/pyext/GApplication.i index 88e2b5d..6e6170e 100644 --- a/pyext/GApplication.i +++ b/pyext/GApplication.i @@ -71,14 +71,12 @@ public: %rename(_logVerbose) logVerbose; %rename(_logDebug) logDebug; %rename(_clobber) clobber; - %rename(_has_par) has_par; %rename(_par_filename) par_filename; %rename(_log_filename) log_filename; %rename(_log_header) log_header; %rename(_log_trailer) log_trailer; %rename(_need_help) need_help; %rename(_log) log; - %rename(_pars) pars; // Methods void clear(void); @@ -219,6 +217,25 @@ GApplication._log_value = _log_value } return; } + GApplicationPar& __getitem__(const int& index) { + if (index >= 0 && index < self->pars().size()) { + return (*self)[index]; + } + else { + throw GException::out_of_range("__getitem__(int)", "Application parameter index", + index, self->pars().size()); + } + } + void __setitem__(const int& index, const GApplicationPar& val) { + if (index >= 0 && index < self->pars().size()) { + (*self)[index] = val; + return; + } + else { + throw GException::out_of_range("__setitem__(int)", "Application parameter index", + index, self->pars().size()); + } + } GApplication copy() { return (*self); } diff --git a/test/test_GApplication.py b/test/test_GApplication.py index 46e5765..c63a55f 100755 --- a/test/test_GApplication.py +++ b/test/test_GApplication.py @@ -17,6 +17,7 @@ # along with this program. If not, see . # # ========================================================================== +import os import gammalib @@ -85,6 +86,7 @@ class Test(gammalib.GPythonTestSuite): # Append tests self.append(self.test_log, 'Test GLog') self.append(self.test_pars, 'Test GApplicationPars') + self.append(self.test_par_iteration, 'Test GApplication parameter iteration') # Return return @@ -204,5 +206,33 @@ class Test(gammalib.GPythonTestSuite): else: self.test_try_failure('This should never happen') + def test_par_iteration(self): + + + # Set PFILES environment temporary + os.environ['PFILES'] = os.environ['PWD']+'/data' + + # Test GApplicationPars from par file + app = gammalib.GApplication('test_GApplication','1.1.0') + + # Test for proper reading of par file + self.test_assert(app._version() == '1.1.0', 'Test for proper application version') + self.test_assert(app.pars().size() == 5, 'Test number of parameters') + + # Loop over parameters + npars = 0 + for par in app: + + # Increment counter + npars += 1 + + # Set chatter parameter to test persistence + if par.name() == 'chatter': + par.integer(4) + + # Test outcome of iteration + self.test_assert(npars == 5, 'Test number of parameters in iterative loop') + self.test_assert(app['chatter'].integer() == 4, 'Test if parameter could be stored correctly') + # Return return diff --git a/test/test_GModel.py b/test/test_GModel.py index b7be47c..2cc75c8 100755 --- a/test/test_GModel.py +++ b/test/test_GModel.py @@ -47,15 +47,36 @@ class Test(gammalib.GPythonTestSuite): self.name("model") # Append tests - self.append(self.test, "Model module dummy test") + self.append(self.test_iterator, "Test Model iterator") # Return return # Test function - def test(self): + def test_iterator(self): """ Test function. """ + + # Initialise model container + models = gammalib.GModels() + + # Read model container + models_test = gammalib.GModels('data/model_point_plaw.xml') + model = models_test[0] + name = model.name() + + for i in range(10): + model.name(name+'_'+str(i)) + models.append(model) + + size = models.size() + nmodels = 0 + for model in models: + nmodels += 1 + + self.test_assert(size == 10, 'Test model container size') + self.test_assert(nmodels == 10, 'Test model iterator') + # Return return