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 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 sparqlfy_with_casting(value, predicate_datatype): """ Create SPARQL-friendly string representation of the value, adding the property casting. Example: >>> sparqlfy_with_casting(1, "http://www.w3.org/2001/XMLSchema#int") ... '"1"^^<http://www.w3.org/2001/XMLSchema#int>' """ assert is_uri(predicate_datatype) template = u'"{0}"^^<{1}>' return template.format(value, predicate_datatype)
def sparqlfy_with_casting(value, predicate_datatype): """ Create SPARQL-friendly string representation of the value, adding the property casting. Example: >>> sparqlfy_with_casting(1, "http://www.w3.org/2001/XMLSchema#int") ... '"1"^^<http://www.w3.org/2001/XMLSchema#int>' """ assert is_uri(predicate_datatype) template = u'"{0}"^^<{1}>' return template.format(value, predicate_datatype)
def normalize_term(term, language=""): """ Provided a query term (literal, variable, expanded uri or compressed uri), and language (to be applied in literals), return the term in the form so it can be used inside a SPARQL Query. examples: (1) http://expanded.predicate -> <http://expanded.predicate> (2) compressed:predicate -> compressed:predicate (3) "some literal" -> '"some literal"@lang' (4) ?variable -> ?variable """ language_tag = u"@%s" % language if language else u"" if (not term.startswith("?")): if (":" in term): expanded_term = expand_uri(term) if expanded_term != term or is_uri(expanded_term): term = u"<%s>" % expanded_term else: term = u'"%s"%s' % (term, language_tag) return term
def normalize_term(term, language=""): """ Provided a query term (literal, variable, expanded uri or compressed uri), and language (to be applied in literals), return the term in the form so it can be used inside a SPARQL Query. examples: (1) http://expanded.predicate -> <http://expanded.predicate> (2) compressed:predicate -> compressed:predicate (3) "some literal" -> '"some literal"@lang' (4) ?variable -> ?variable """ language_tag = u"@%s" % language if language else u"" if (not term.startswith("?")): if (":" in term): expanded_term = expand_uri(term) if expanded_term != term or is_uri(expanded_term): term = u"<%s>" % expanded_term else: term = u'"%s"%s' % (term, language_tag) return term
def test_is_not_uri(self): uri = "ftp://example.onto/predicate" self.assertFalse(is_uri(uri))
def test_is_uri_not_string(self): uri = {"http://example.onto/predicate": []} self.assertFalse(is_uri(uri))
def test_is_uri_none(self): uri = None self.assertFalse(is_uri(uri))
def test_is_uri(self): uri = "http://example.onto/predicate" self.assertTrue(is_uri(uri))