Ejemplo n.º 1
0
    def application_json(self, data):
        context = {}
        for name, objective in six.iteritems(self._context):
            context[name] = reach(objective, data)

        nodes = reach(self._at, data)
        if isinstance(nodes, dict):
            nodes = six.itervalues(nodes)

        if nodes is None:
            raise DSNoContent("Found no nodes at {}".format(self._at))

        for node in nodes:
            result = copy(context)
            for name, objective in six.iteritems(self._objective):
                result[name] = reach(objective, node)
            yield result
Ejemplo n.º 2
0
    def application_json(self, data):
        context = {}
        for name, objective in self._context.items():
            context[name] = reach(objective, data)

        nodes = reach(self._at, data)
        if isinstance(nodes, dict):
            nodes = nodes.values()

        if nodes is None:
            raise DSNoContent("Found no nodes at {}".format(self._at))

        for node in nodes:
            result = copy(context)
            for name, objective in self._objective.items():
                result[name] = reach(objective, node)
            yield result
Ejemplo n.º 3
0
    def influence(self, individual):
        """
        This allows the Collective to set some attributes and or properties on the Individual

        :param individual: The individual that should be influenced
        :return: The influenced individual
        """
        if self.identifier:
            individual.identity = reach("$." + self.identifier, individual.properties)
        return individual
Ejemplo n.º 4
0
    def influence(self, individual):
        """
        This allows the Collective to set some attributes and or properties on the Individual

        :param individual: The individual that should be influenced
        :return: The influenced individual
        """
        if self.identifier:
            individual.identity = reach("$." + self.identifier,
                                        individual.properties)
        if self.indexes:
            index_keys = self._get_index_keys()
            individual = self.set_index_for_individual(individual, index_keys)

        return individual
Ejemplo n.º 5
0
 def output_from_content(content, *args):  # TODO: test to unlock
     if len(args) > 1:
         return map(Individual.output_from_content, repeat(content), args)
     frm = args[0]
     if not frm:
         return frm
     if isinstance(frm, str):
         return reach(frm, content)
     elif isinstance(frm, list):
         if len(frm) > 1:
             return Individual.output_from_content(content, *frm)
         else:
             return [Individual.output_from_content(content, *frm)]
     elif isinstance(frm, dict):
         return {key: Individual.output_from_content(content, value) for key, value in frm.items()}
     else:
         raise AssertionError("Expected a string, list or dict as argument got {} instead".format(type(frm)))
Ejemplo n.º 6
0
 def output_from_content(content, *args):
     if len(args) > 1:
         return map(Individual.output_from_content, repeat(content), args)
     frm = args[0]
     if not frm:
         return frm
     if isinstance(frm, str):
         return reach(frm, content)
     elif isinstance(frm, list):
         if len(frm) > 1:
             return Individual.output_from_content(content, *frm)
         else:
             return [Individual.output_from_content(content, *frm)]
     elif isinstance(frm, dict):
         return {
             key: Individual.output_from_content(content, value)
             for key, value in frm.items()
         }
     else:
         raise AssertionError(
             "Expected a string, list or dict as argument got {} instead".
             format(type(frm)))
Ejemplo n.º 7
0
 def test_invalid_key(self):
     self.assertEqual(reach("does.not.exist", self.test_dict), None)
Ejemplo n.º 8
0
 def test_key_with_dots(self):
     self.assertEqual(reach("dotted.key", self.test_dict), self.test_dict["dotted.key"])
Ejemplo n.º 9
0
 def test_list_access(self):
     self.assertEqual(reach("list.0", self.test_dict), self.test_dict["list"][0])
     self.assertEqual(reach("dict.list.0", self.test_dict), self.test_dict["dict"]["list"][0])
     self.assertEqual(reach("0.test", self.test_list), self.test_list[0]["test"])
Ejemplo n.º 10
0
 def test_dict_access(self):
     self.assertEqual(reach("dict.test", self.test_dict), self.test_dict["dict"]["test"])
     self.assertEqual(reach("dict.dict", self.test_dict), self.test_dict["dict"]["dict"])
     self.assertEqual(reach("dict.list", self.test_dict), self.test_dict["dict"]["list"])
Ejemplo n.º 11
0
 def test_invalid_data(self):
     try:
         reach("irrelevant", "invalid-input")
         self.fail("Reach did not throw a TypeError after getting invalid input")
     except TypeError:
         pass
Ejemplo n.º 12
0
 def test_invalid_key(self):
     self.assertEqual(reach("does.not.exist", self.test_dict), None)
Ejemplo n.º 13
0
 def test_key_with_dots(self):
     self.assertEqual(reach("dotted.key", self.test_dict), self.test_dict["dotted.key"])
Ejemplo n.º 14
0
 def test_list_access(self):
     self.assertEqual(reach("list.0", self.test_dict), self.test_dict["list"][0])
     self.assertEqual(reach("dict.list.0", self.test_dict), self.test_dict["dict"]["list"][0])
     self.assertEqual(reach("0.test", self.test_list), self.test_list[0]["test"])
Ejemplo n.º 15
0
 def test_dict_access(self):
     self.assertEqual(reach("dict.test", self.test_dict), self.test_dict["dict"]["test"])
     self.assertEqual(reach("dict.dict", self.test_dict), self.test_dict["dict"]["dict"])
     self.assertEqual(reach("dict.list", self.test_dict), self.test_dict["dict"]["list"])
Ejemplo n.º 16
0
 def test_none_key(self):
     self.assertEqual(reach(None, self.test_dict), self.test_dict)
     self.assertEqual(reach(None, self.test_list), self.test_list)
Ejemplo n.º 17
0
 def test_invalid_data(self):
     try:
         reach("irrelevant", "invalid-input")
         self.fail("Reach did not throw a TypeError after getting invalid input")
     except TypeError:
         pass
Ejemplo n.º 18
0
 def test_none_key(self):
     self.assertEqual(reach(None, self.test_dict), self.test_dict)
     self.assertEqual(reach(None, self.test_list), self.test_list)