Skip to content

Commit

Permalink
Merge pull request #1237 from JaimieMurdock/Namespace.__contains__
Browse files Browse the repository at this point in the history
Adding Namespace.__contains__()
  • Loading branch information
ashleysommer authored Feb 9, 2021
2 parents d78f0a4 + ac51ae5 commit c11f7b5
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 6 deletions.
26 changes: 21 additions & 5 deletions rdflib/namespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -141,6 +145,9 @@ def __getattr__(self, name):

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):
Expand Down Expand Up @@ -187,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)
Expand All @@ -213,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):
Expand Down Expand Up @@ -617,6 +626,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 = {}
Expand Down
17 changes: 16 additions & 1 deletion test/test_namespace.py
Original file line number Diff line number Diff line change
@@ -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


Expand Down Expand Up @@ -109,3 +109,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")

0 comments on commit c11f7b5

Please sign in to comment.