rdflib.namespace package

Module contents

Namespace Utilities

RDFLib provides mechanisms for managing Namespaces.

In particular, there is a Namespace class that takes as its argument the base URI of the namespace.

>>> from rdflib.namespace import Namespace
>>> owl = Namespace('http://www.w3.org/2002/07/owl#')

Fully qualified URIs in the namespace can be constructed either by attribute or by dictionary access on Namespace instances:

>>> owl.seeAlso
rdflib.term.URIRef('http://www.w3.org/2002/07/owl#seeAlso')
>>> owl['seeAlso']
rdflib.term.URIRef('http://www.w3.org/2002/07/owl#seeAlso')

Automatic handling of unknown predicates

As a programming convenience, a namespace binding is automatically created when rdflib.term.URIRef predicates are added to the graph.

Importable namespaces

The following namespaces are available by directly importing from rdflib:

  • RDF

  • RDFS

  • OWL

  • XSD

  • FOAF

  • SKOS

  • DOAP

  • DC

  • DCTERMS

  • VOID

>>> from rdflib import OWL
>>> OWL.seeAlso
rdflib.term.URIRef('http://www.w3.org/2002/07/owl#seeAlso')
class rdflib.namespace.ClosedNamespace(uri, terms)[source]

Bases: object

A namespace with a closed list of members

Trying to create terms not listen is an error

__dict__ = mappingproxy({'__module__': 'rdflib.namespace', '__doc__': '\n    A namespace with a closed list of members\n\n    Trying to create terms not listen is an error\n    ', '__init__': <function ClosedNamespace.__init__>, 'term': <function ClosedNamespace.term>, '__getitem__': <function ClosedNamespace.__getitem__>, '__getattr__': <function ClosedNamespace.__getattr__>, '__str__': <function ClosedNamespace.__str__>, '__repr__': <function ClosedNamespace.__repr__>, '__dict__': <attribute '__dict__' of 'ClosedNamespace' objects>, '__weakref__': <attribute '__weakref__' of 'ClosedNamespace' objects>, '__annotations__': {}})
__getattr__(name)[source]
__getitem__(key, default=None)[source]
__init__(uri, terms)[source]
__module__ = 'rdflib.namespace'
__repr__()[source]

Return repr(self).

__str__()[source]

Return str(self).

__weakref__

list of weak references to the object (if defined)

term(name)[source]
class rdflib.namespace.Namespace(value)[source]

Bases: str

Utility class for quickly generating URIRefs with a common prefix

>>> from rdflib import Namespace
>>> n = Namespace("http://example.org/")
>>> n.Person # as attribute
rdflib.term.URIRef('http://example.org/Person')
>>> n['first-name'] # as item - for things that are not valid python identifiers
rdflib.term.URIRef('http://example.org/first-name')
__annotations__ = {}
__dict__ = mappingproxy({'__module__': 'rdflib.namespace', '__doc__': '\n    Utility class for quickly generating URIRefs with a common prefix\n\n    >>> from rdflib import Namespace\n    >>> n = Namespace("http://example.org/")\n    >>> n.Person # as attribute\n    rdflib.term.URIRef(\'http://example.org/Person\')\n    >>> n[\'first-name\'] # as item - for things that are not valid python identifiers\n    rdflib.term.URIRef(\'http://example.org/first-name\')\n\n    ', '__new__': <staticmethod(<function Namespace.__new__>)>, 'title': <property object>, 'term': <function Namespace.term>, '__getitem__': <function Namespace.__getitem__>, '__getattr__': <function Namespace.__getattr__>, '__repr__': <function Namespace.__repr__>, '__dict__': <attribute '__dict__' of 'Namespace' objects>, '__weakref__': <attribute '__weakref__' of 'Namespace' objects>, '__annotations__': {}})
__getattr__(name)[source]
__getitem__(key, default=None)[source]

Return self[key].

__module__ = 'rdflib.namespace'
static __new__(cls, value)[source]
__repr__()[source]

Return repr(self).

__weakref__

list of weak references to the object (if defined)

term(name)[source]
property title

Return a version of the string where each word is titlecased.

More specifically, words start with uppercased characters and all remaining cased characters have lower case.

class rdflib.namespace.NamespaceManager(graph)[source]

Bases: object

Class for managing prefix => namespace mappings

Sample usage from FuXi …

ruleStore = N3RuleStore(additionalBuiltins=additionalBuiltins)
nsMgr = NamespaceManager(Graph(ruleStore))
ruleGraph = Graph(ruleStore,namespace_manager=nsMgr)

and …

>>> import rdflib
>>> from rdflib import Graph
>>> from rdflib.namespace import Namespace, NamespaceManager
>>> exNs = Namespace('http://example.com/')
>>> namespace_manager = NamespaceManager(Graph())
>>> namespace_manager.bind('ex', exNs, override=False)
>>> g = Graph()
>>> g.namespace_manager = namespace_manager
>>> all_ns = [n for n in g.namespace_manager.namespaces()]
>>> assert ('ex', rdflib.term.URIRef('http://example.com/')) in all_ns
>>>
__dict__ = mappingproxy({'__module__': 'rdflib.namespace', '__doc__': "\n\n    Class for managing prefix => namespace mappings\n\n    Sample usage from FuXi ...\n\n    .. code-block:: python\n\n        ruleStore = N3RuleStore(additionalBuiltins=additionalBuiltins)\n        nsMgr = NamespaceManager(Graph(ruleStore))\n        ruleGraph = Graph(ruleStore,namespace_manager=nsMgr)\n\n\n    and ...\n\n    .. code-block:: pycon\n\n        >>> import rdflib\n        >>> from rdflib import Graph\n        >>> from rdflib.namespace import Namespace, NamespaceManager\n        >>> exNs = Namespace('http://example.com/')\n        >>> namespace_manager = NamespaceManager(Graph())\n        >>> namespace_manager.bind('ex', exNs, override=False)\n        >>> g = Graph()\n        >>> g.namespace_manager = namespace_manager\n        >>> all_ns = [n for n in g.namespace_manager.namespaces()]\n        >>> assert ('ex', rdflib.term.URIRef('http://example.com/')) in all_ns\n        >>>\n\n    ", '__init__': <function NamespaceManager.__init__>, 'reset': <function NamespaceManager.reset>, '_NamespaceManager__get_store': <function NamespaceManager.__get_store>, 'store': <property object>, 'qname': <function NamespaceManager.qname>, 'normalizeUri': <function NamespaceManager.normalizeUri>, 'compute_qname': <function NamespaceManager.compute_qname>, 'bind': <function NamespaceManager.bind>, 'namespaces': <function NamespaceManager.namespaces>, 'absolutize': <function NamespaceManager.absolutize>, '__dict__': <attribute '__dict__' of 'NamespaceManager' objects>, '__weakref__': <attribute '__weakref__' of 'NamespaceManager' objects>, '__annotations__': {}})
__init__(graph)[source]
__module__ = 'rdflib.namespace'
__weakref__

list of weak references to the object (if defined)

absolutize(uri, defrag=1)[source]
bind(prefix, namespace, override=True, replace=False)[source]

bind a given namespace to the prefix

if override, rebind, even if the given namespace is already bound to another prefix.

if replace, replace any existing prefix with the new namespace

compute_qname(uri, generate=True)[source]
namespaces()[source]
normalizeUri(rdfTerm)[source]

Takes an RDF Term and ‘normalizes’ it into a QName (using the registered prefix) or (unlike compute_qname) the Notation 3 form for URIs: <…URI…>

qname(uri)[source]
reset()[source]
property store
rdflib.namespace.is_ncname(name)[source]
rdflib.namespace.split_uri(uri)[source]