From cc340ede60b9ad445c6e68cdd21208fd09b8da27 Mon Sep 17 00:00:00 2001 From: Jaimie Murdock Date: Wed, 27 Jan 2021 14:51:10 -0700 Subject: [PATCH 1/3] Adding Namespace.__contains__() --- rdflib/namespace.py | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/rdflib/namespace.py b/rdflib/namespace.py index aa1b4d0d8..4a4d0cfe0 100644 --- a/rdflib/namespace.py +++ b/rdflib/namespace.py @@ -112,7 +112,11 @@ class Namespace(str): rdflib.term.URIRef(u'http://example.org/Person') >>> n['first-name'] # as item - for things that are not valid python identifiers rdflib.term.URIRef(u'http://example.org/first-name') - + >>> n.Person in n + True + >>> n2 = Namespace("http://example2.org/") + >>> n.Person in n2 + False """ def __new__(cls, value): @@ -141,6 +145,9 @@ def __getattr__(self, name): def __repr__(self): return "Namespace(%r)" % str(self) + + def __contains__(self, other): + return other.startswith(self) # test namespace membership with "ref in ns" syntax class URIPattern(str): From c85cf9d6f78a6554e226620c095b6759073ac04d Mon Sep 17 00:00:00 2001 From: Jaimie Murdock Date: Thu, 28 Jan 2021 12:48:03 -0700 Subject: [PATCH 2/3] adding {ClosedNamespace,NamespaceManager,_RDFNamespace}.__contains__() and tests --- rdflib/namespace.py | 21 +++++++++++++++------ test/test_namespace.py | 18 +++++++++++++++++- 2 files changed, 32 insertions(+), 7 deletions(-) diff --git a/rdflib/namespace.py b/rdflib/namespace.py index 4a4d0cfe0..993d82a4d 100644 --- a/rdflib/namespace.py +++ b/rdflib/namespace.py @@ -146,8 +146,8 @@ def __getattr__(self, name): def __repr__(self): return "Namespace(%r)" % str(self) - def __contains__(self, other): - return other.startswith(self) # test namespace membership with "ref in ns" syntax + def __contains__(self, ref): + return ref.startswith(self) # test namespace membership with "ref in ns" syntax class URIPattern(str): @@ -194,11 +194,10 @@ def __init__(self, uri, terms): self.__uris[t] = URIRef(self.uri + t) def term(self, name): - uri = self.__uris.get(name) - if uri is None: + if name not in self.__uris: raise KeyError("term '{}' not in namespace '{}'".format(name, self.uri)) else: - return uri + return self.__uris[name] def __getitem__(self, key, default=None): return self.term(key) @@ -220,9 +219,12 @@ def __repr__(self): def __dir__(self): return list(self._ClosedNamespace__uris) + + def __contains__(self, ref): + return ref in self.__uris.values() # test namespace membership with "ref in ns" syntax def _ipython_key_completions_(self): - return dir(self) + return dir(self.uri) class _RDFNamespace(ClosedNamespace): @@ -597,6 +599,13 @@ def __init__(self, graph): self.bind("rdfs", RDFS) self.bind("xsd", XSD) + def __contians__(self, ref): + # checks if a reference is in any of the managed namespaces with syntax + # "ref in manager". Note that we don't use "ref in ns", as + # NamespaceManager.namespaces() returns Iterator[Tuple[str, URIRef]] + # rather than Iterator[Tuple[str, Namespace]] + return any(ref.startswith(ns) for prefix, ns in self.namespaces()) + def reset(self): self.__cache = {} self.__strie = {} diff --git a/test/test_namespace.py b/test/test_namespace.py index 2706467ff..7d9b04fe4 100644 --- a/test/test_namespace.py +++ b/test/test_namespace.py @@ -1,7 +1,8 @@ +from rdflib.rdflib.namespace import Namespace import unittest from rdflib.graph import Graph -from rdflib.namespace import FOAF +from rdflib.namespace import FOAF, RDF, RDFS, SH from rdflib.term import URIRef @@ -112,3 +113,18 @@ def add_not_in_namespace(s): add_not_in_namespace("givenName"), URIRef("http://xmlns.com/foaf/0.1/givenName"), ) + + def test_contains_method(self): + """Tests for Namespace.__contains__() methods.""" + + ref = URIRef('http://www.w3.org/ns/shacl#example') + self.assertTrue(type(SH) == Namespace, "SH no longer a Namespace, update test.") + self.assertTrue(ref in SH, "sh:example not in SH") + + ref = URIRef('http://www.w3.org/2000/01/rdf-schema#label') + self.assertTrue(ref in RDFS, "ClosedNamespace(RDFS) does not include rdfs:label") + ref = URIRef('http://www.w3.org/2000/01/rdf-schema#example') + self.assertFalse(ref in RDFS, "ClosedNamespace(RDFS) includes out-of-ns member rdfs:example") + + ref = URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type') + self.assertTrue(ref in RDF, "_RDFNamespace does not include rdf:type") \ No newline at end of file From ac51ae5553a3a7fab8784551ec1d4aa8407d651f Mon Sep 17 00:00:00 2001 From: Jaimie Murdock Date: Tue, 2 Feb 2021 17:24:13 -0700 Subject: [PATCH 3/3] Namespace fix --- test/test_namespace.py | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/test/test_namespace.py b/test/test_namespace.py index 7d9b04fe4..12fa0600b 100644 --- a/test/test_namespace.py +++ b/test/test_namespace.py @@ -1,8 +1,7 @@ -from rdflib.rdflib.namespace import Namespace import unittest from rdflib.graph import Graph -from rdflib.namespace import FOAF, RDF, RDFS, SH +from rdflib.namespace import Namespace, FOAF, RDF, RDFS, SH from rdflib.term import URIRef