def sparqlfy_object(value, *args): """ Create SPARQL-friendly string representation of the value, considering it is or contains a URI. Raises an exception if the value is neither. Example: >>> sparqlfy_object("compressed:uri") ... "compressed:uri" >>> sparqlfy_object("http://some.uri") ... "<http://some.uri>" >>> sparqlfy_object({"@id": "http://some.uri"}) ... "<http://some.uri>" """ if isinstance(value, dict): value = value["@id"] if is_uri(value): return u"<{0}>".format(value) elif is_compressed_uri(value): return value else: raise InstanceError("({0}) is not a URI or cURI".format(value))
def decorate_with_class_prefix(list_of_dicts): for dict_item in list_of_dicts: uri = dict_item["@id"] if is_compressed_uri(uri): class_prefix = uri.rsplit(":")[0] else: pos = uri.rfind("/") + 1 class_prefix = uri[:pos] dict_item["class_prefix"] = class_prefix
def test_is_compressed_uri_given_a_not_string(self): self.assertEqual(is_compressed_uri(12), False)
def test_is_compressed_uri_given_a_literal(self): self.assertEqual(is_compressed_uri("oi"), False)
def test_is_compressed_uri_given_a_compressed_and_prefixes(self): self.assertEqual( is_compressed_uri("newslug:xubiru", {"newslug": "http://newslug.com"}), True)
def test_is_compressed_uri_given_a_uncompressed_uri(self): self.assertEqual(is_compressed_uri("http://something.org/xubiru", {}), False)
def test_is_compressed_uri_given_a_compressed_uri_with_invalid_prefix_slug( self): self.assertEqual(is_compressed_uri("unexistent:Xubi"), False)
def test_is_compressed_uri_given_a_compressed_uri(self): self.assertEqual(is_compressed_uri("person:Person"), True)