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 data input" ) except TypeError: pass
def test_invalid_path(self): try: reach("dict.test", self.test_dict) self.fail( "Reach did not throw a ValueError after getting path with invalid start" ) except ValueError: pass try: reach("$.", self.test_dict) self.fail( "Reach did not throw a ValueError after getting invalid path") except ValueError: pass
def influence(self, document): """ This allows the Collection to set some attributes and or properties on the Document :param document: The document that should be influenced :return: The influenced document """ if self.identifier: document.identity = reach("$." + self.identifier, document.properties) if self.referee: document.reference = reach("$." + self.referee, document.properties) return document
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 DGNoContent("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 application_json(self, data): context = {} for name, objective in self._context.items(): context[name] = reach( objective, data) if not callable(objective) else objective(data) nodes = reach(self._at, data) if not callable(self._at) else self._at(data) if isinstance(nodes, dict) and self.config.extract_from_object_values: nodes = nodes.values() elif nodes is None: raise DGNoContent("Found no nodes at {}".format(self._at)) elif not isinstance(nodes, list): nodes = [nodes] for node in nodes: result = copy(context) for name, objective in self._objective.items(): result[name] = reach( objective, node) if not callable(objective) else objective(node) yield result
def output_from_content(content, *args): if len(args) > 1: return map(DocumentBase.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 DocumentBase.output_from_content(content, *frm) else: return [DocumentBase.output_from_content(content, *frm)] elif isinstance(frm, dict): return {key: DocumentBase.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_none_and_dollar_key(self): self.assertEqual(reach(None, self.test_dict), self.test_dict) self.assertEqual(reach(None, self.test_list), self.test_list) self.assertEqual(reach("$", self.test_dict), self.test_dict) self.assertEqual(reach("$", self.test_list), self.test_list)
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 get_is_part_of(cls, node): return reach("$.attributes.partOf", node)