summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAshley Sommer <ashleysommer@gmail.com>2021-02-09 19:54:30 +1000
committerGitHub <noreply@github.com>2021-02-09 19:54:30 +1000
commitc11f7b503b50b7c3cdeec0f36261fa09b0615380 (patch)
tree7c3ed1e4515cefb53efa8ecb3c9b08b8193e96e3
parentd78f0a40d73903b1ca364af6fd782edb61a29ce0 (diff)
parentac51ae5553a3a7fab8784551ec1d4aa8407d651f (diff)
downloadrdflib-c11f7b503b50b7c3cdeec0f36261fa09b0615380.tar.gz
Merge pull request #1237 from JaimieMurdock/Namespace.__contains__
Adding Namespace.__contains__()
-rw-r--r--rdflib/namespace.py26
-rw-r--r--test/test_namespace.py17
2 files changed, 37 insertions, 6 deletions
diff --git a/rdflib/namespace.py b/rdflib/namespace.py
index f9bb86d4..98bf6bc4 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 @@ class Namespace(str):
def __repr__(self):
return "Namespace(%r)" % str(self)
+
+ def __contains__(self, ref):
+ return ref.startswith(self) # test namespace membership with "ref in ns" syntax
class URIPattern(str):
@@ -187,11 +194,10 @@ class ClosedNamespace(object):
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)
@@ -213,9 +219,12 @@ class ClosedNamespace(object):
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):
@@ -617,6 +626,13 @@ class NamespaceManager(object):
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 8d4f7fd6..2a8a1e65 100644
--- a/test/test_namespace.py
+++ b/test/test_namespace.py
@@ -1,7 +1,7 @@
import unittest
from rdflib.graph import Graph
-from rdflib.namespace import FOAF
+from rdflib.namespace import Namespace, FOAF, RDF, RDFS, SH
from rdflib.term import URIRef
@@ -109,3 +109,18 @@ class NamespacePrefixTest(unittest.TestCase):
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