Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Namespace.__contains__() #1237

Merged
merged 3 commits into from
Feb 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 @@ -590,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 = {}
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 @@ -112,3 +112,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")