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
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
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
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
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)))
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)))
def test_invalid_key(self): self.assertEqual(reach("does.not.exist", self.test_dict), None)
def test_key_with_dots(self): self.assertEqual(reach("dotted.key", self.test_dict), self.test_dict["dotted.key"])
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"])
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"])
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
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)