浏览代码

[Feature] #203 Update the code and tests to manage list as parameter

monf 8 年之前
父节点
当前提交
012bd60aec
共有 3 个文件被更改,包括 74 次插入14 次删除
  1. 37 1
      Tests/test_order_analyser.py
  2. 33 13
      kalliope/core/OrderAnalyser.py
  3. 4 0
      kalliope/neurons/sleep/sleep.py

+ 37 - 1
Tests/test_order_analyser.py

@@ -571,7 +571,6 @@ class TestOrderAnalyser(unittest.TestCase):
                           expected_result,
                           "Fail to run the default synapse")
 
-
     def test_replace_global_variables(self):
         """
         Testing the _replace_global_variables function from the OrderAnalyser.
@@ -580,6 +579,7 @@ class TestOrderAnalyser(unittest.TestCase):
             - 2/ global variable with string after
             - 3/ global variable with int after
             - 4/ multiple global variables
+            - 5/ parameter value is a list
 
         """
 
@@ -651,5 +651,41 @@ class TestOrderAnalyser(unittest.TestCase):
                           expected_neuron_result,
                           "Fail to assign multiple global variables to neuron")
 
+        # 5/ parameter value is a list
+        neuron1 = Neuron(name='neuron1', parameters={'var1': '[hello {{name}}, bonjour {{name}}]'})
+        variables = {
+            "name": "LaMonf",
+            "hello2": "test2",
+        }
+        st = Settings(variables=variables)
+
+        expected_neuron_result = Neuron(name='neuron1', parameters={'var1': '[hello LaMonf, bonjour LaMonf]'})
+
+        # assign global variable to neuron1
+        OrderAnalyser._replace_global_variables(neuron=neuron1,
+                                                settings=st)
+        self.assertEquals(neuron1,
+                          expected_neuron_result,
+                          "Fail to assign a single global when parameter value is a list to neuron")
+
+    def test_get_global_variable(self):
+        """
+        Test the get_global_variable of the OrderAnalyser Class
+        """
+        sentence = "i am {{name2}}"
+        variables = {
+            "name": "LaMonf",
+            "name2": "kalliope",
+        }
+        st = Settings(variables=variables)
+
+        expected_result = "i am kalliope"
+
+        self.assertEquals(OrderAnalyser._get_global_variable(sentence=sentence,
+                                                             settings=st),
+                          expected_result,
+                          "Fail to get the global variable from the sentence")
+
+
 if __name__ == '__main__':
     unittest.main()

+ 33 - 13
kalliope/core/OrderAnalyser.py

@@ -152,25 +152,45 @@ class OrderAnalyser:
             cls._replace_global_variables(neuron, settings)
             cls._start_neuron(neuron, params)
 
-    @staticmethod
-    def _replace_global_variables(neuron, settings):
+    @classmethod
+    def _replace_global_variables(cls, neuron, settings):
         """
         Replace all the parameters with variables with the variable value.
         :param neuron: the neuron
         :param settings: the settings
         """
         for param in neuron.parameters:
-            if Utils.is_containing_bracket(neuron.parameters[param]):
-                sentence_no_spaces = Utils.remove_spaces_in_brackets(sentence=neuron.parameters[param])
-                list_of_bracket_params = Utils.find_all_matching_brackets(sentence=sentence_no_spaces)
-                for param_with_bracket in list_of_bracket_params:
-                    param_no_brackets = param_with_bracket.replace("{{", "").replace("}}", "")
-                    if param_no_brackets in settings.variables:
-                        logger.debug("Replacing variable %s with  %s" % (param_with_bracket,
-                                                                         settings.variables[param_no_brackets]))
-                        sentence_no_spaces = sentence_no_spaces.replace(param_with_bracket,
-                                                                        str(settings.variables[param_no_brackets]))
-                        neuron.parameters[param] = sentence_no_spaces
+            if isinstance(neuron.parameters[param], list):
+                list_param_value = list()
+                for sentence in neuron.parameters[param]:
+                    sentence_with_global_variables = cls._get_global_variable(sentence=sentence,
+                                                                              settings=settings)
+                    list_param_value.append(sentence_with_global_variables)
+                neuron.parameters[param] = list_param_value
+
+            else:
+                if Utils.is_containing_bracket(neuron.parameters[param]):
+                    sentence_with_global_variables = cls._get_global_variable(sentence=neuron.parameters[param],
+                                                                              settings=settings)
+                    neuron.parameters[param] = sentence_with_global_variables
+
+    @staticmethod
+    def _get_global_variable(sentence, settings):
+        """
+        Get the global variable from the sentence with brackets
+        :param sentence: the sentence to check
+        :return: the global variable
+        """
+        sentence_no_spaces = Utils.remove_spaces_in_brackets(sentence=sentence)
+        list_of_bracket_params = Utils.find_all_matching_brackets(sentence=sentence_no_spaces)
+        for param_with_bracket in list_of_bracket_params:
+            param_no_brackets = param_with_bracket.replace("{{", "").replace("}}", "")
+            if param_no_brackets in settings.variables:
+                logger.debug("Replacing variable %s with  %s" % (param_with_bracket,
+                                                                 settings.variables[param_no_brackets]))
+                sentence_no_spaces = sentence_no_spaces.replace(param_with_bracket,
+                                                                str(settings.variables[param_no_brackets]))
+        return sentence_no_spaces
 
     @staticmethod
     def _start_neuron(neuron, params):

+ 4 - 0
kalliope/neurons/sleep/sleep.py

@@ -10,6 +10,10 @@ class Sleep(NeuronModule):
 
         # check parameters
         if self._is_parameters_ok():
+
+            if isinstance(self.seconds, str):
+                self.seconds = float(self.seconds)
+
             time.sleep(self.seconds)
 
     def _is_parameters_ok(self):