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
>>> RDFS = Namespace("http://www.w3.org/1999/02/22-rdf-syntax-ns#")

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

>>> RDFS.seeAlso
rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#seeAlso')
>>> RDFS['seeAlso']
rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#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:

  • BRICK

  • CSVW

  • DC

  • DCAT

  • DCMITYPE

  • DCTERMS

  • DCAM

  • DOAP

  • FOAF

  • ODRL2

  • ORG

  • OWL

  • PROF

  • PROV

  • QB

  • RDF

  • RDFS

  • SDO

  • SH

  • SKOS

  • SOSA

  • SSN

  • TIME

  • VANN

  • VOID

  • WGS

  • XSD

>>> from rdflib.namespace import RDFS
>>> RDFS.seeAlso
rdflib.term.URIRef('http://www.w3.org/2000/01/rdf-schema#seeAlso')
class rdflib.namespace.ClosedNamespace(uri: str, terms: List[str])[source]

Bases: Namespace

A namespace with a closed list of members

Trying to create terms not listed is an error

__annotations__ = {'_ClosedNamespace__uris': typing.Dict[str, rdflib.term.URIRef]}
__contains__(ref)[source]

Allows to check if a URI is within (starts with) this Namespace.

>>> from rdflib import URIRef
>>> namespace = Namespace('http://example.org/')
>>> uri = URIRef('http://example.org/foo')
>>> uri in namespace
True
>>> person_class = namespace['Person']
>>> person_class in namespace
True
>>> obj = URIRef('http://not.example.org/bar')
>>> obj in namespace
False
Parameters:

ref (str) –

Return type:

bool

__dir__()[source]

Default dir() implementation.

Return type:

List[str]

__getattr__(name)[source]
Parameters:

name (str) –

Return type:

URIRef

__getitem__(key)[source]

Return self[key].

Parameters:

key (str) –

Return type:

URIRef

__module__ = 'rdflib.namespace'
static __new__(cls, uri, terms)[source]
Parameters:
__repr__()[source]

Return repr(self).

Return type:

str

term(name)[source]
Parameters:

name (str) –

Return type:

URIRef

property uri: str
Return type:

str

class rdflib.namespace.DefinedNamespace[source]

Bases: object

A Namespace with an enumerated list of members. Warnings are emitted if unknown members are referenced if _warn is True

class rdflib.namespace.Namespace(value: Union[str, bytes])[source]

Bases: str

Utility class for quickly generating URIRefs with a common prefix

>>> from rdflib.namespace 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')
>>> n.Person in n
True
>>> n2 = Namespace("http://example2.org/")
>>> n.Person in n2
False
__annotations__ = {}
__contains__(ref)[source]

Allows to check if a URI is within (starts with) this Namespace.

>>> from rdflib import URIRef
>>> namespace = Namespace('http://example.org/')
>>> uri = URIRef('http://example.org/foo')
>>> uri in namespace
True
>>> person_class = namespace['Person']
>>> person_class in namespace
True
>>> obj = URIRef('http://not.example.org/bar')
>>> obj in namespace
False
Parameters:

ref (str) –

Return type:

bool

__dict__ = mappingproxy({'__module__': 'rdflib.namespace', '__doc__': '\n    Utility class for quickly generating URIRefs with a common prefix\n\n    >>> from rdflib.namespace 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.Person in n\n    True\n    >>> n2 = Namespace("http://example2.org/")\n    >>> n.Person in n2\n    False\n    ', '__new__': <staticmethod object>, 'title': <property object>, 'term': <function Namespace.term>, '__getitem__': <function Namespace.__getitem__>, '__getattr__': <function Namespace.__getattr__>, '__repr__': <function Namespace.__repr__>, '__contains__': <function Namespace.__contains__>, '__dict__': <attribute '__dict__' of 'Namespace' objects>, '__weakref__': <attribute '__weakref__' of 'Namespace' objects>, '__annotations__': {}})
__getattr__(name)[source]
Parameters:

name (str) –

Return type:

URIRef

__getitem__(key)[source]

Return self[key].

Parameters:

key (str) –

Return type:

URIRef

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

value (Union[str, bytes]) –

Return type:

Namespace

__repr__()[source]

Return repr(self).

Return type:

str

__weakref__

list of weak references to the object (if defined)

term(name)[source]
Parameters:

name (str) –

Return type:

URIRef

property title: URIRef

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.

Return type:

URIRef

class rdflib.namespace.NamespaceManager(graph, bind_namespaces='core')[source]

Bases: object

Class for managing prefix => namespace mappings

This class requires an RDFlib Graph as an input parameter and may optionally have the parameter bind_namespaces set. This second parameter selects a strategy which is one of the following:

  • core:
    • binds several core RDF prefixes only

    • owl, rdf, rdfs, xsd, xml from the NAMESPACE_PREFIXES_CORE object

    • this is default

  • rdflib:
    • binds all the namespaces shipped with RDFLib as DefinedNamespace instances

    • all the core namespaces and all the following: brick, csvw, dc, dcat

    • dcmitype, cdterms, dcam, doap, foaf, geo, odrl, org, prof, prov, qb, sdo

    • sh, skos, sosa, ssn, time, vann, void

    • see the NAMESPACE_PREFIXES_RDFLIB object for the up-to-date list

  • none:
    • binds no namespaces to prefixes

    • note this is NOT default behaviour

  • cc:
    • using prefix bindings from prefix.cc which is a online prefixes database

    • not implemented yet - this is aspirational

See the Sample usage

>>> import rdflib
>>> from rdflib import Graph
>>> from rdflib.namespace import Namespace, NamespaceManager
>>> EX = Namespace('http://example.com/')
>>> namespace_manager = NamespaceManager(Graph())
>>> namespace_manager.bind('ex', EX, 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
>>>
Parameters:
  • graph (Graph) –

  • bind_namespaces (Literal[‘core’, ‘rdflib’, ‘none’]) –

__contains__(ref)[source]
Parameters:

ref (str) –

Return type:

bool

__dict__ = mappingproxy({'__module__': 'rdflib.namespace', '__doc__': "Class for managing prefix => namespace mappings\n\n    This class requires an RDFlib Graph as an input parameter and may optionally have\n    the parameter bind_namespaces set. This second parameter selects a strategy which\n    is one of the following:\n\n    * core:\n        * binds several core RDF prefixes only\n        * owl, rdf, rdfs, xsd, xml from the NAMESPACE_PREFIXES_CORE object\n        * this is default\n    * rdflib:\n        * binds all the namespaces shipped with RDFLib as DefinedNamespace instances\n        * all the core namespaces and all the following: brick, csvw, dc, dcat\n        * dcmitype, cdterms, dcam, doap, foaf, geo, odrl, org, prof, prov, qb, sdo\n        * sh, skos, sosa, ssn, time, vann, void\n        * see the NAMESPACE_PREFIXES_RDFLIB object for the up-to-date list\n    * none:\n        * binds no namespaces to prefixes\n        * note this is NOT default behaviour\n    * cc:\n        * using prefix bindings from prefix.cc which is a online prefixes database\n        * not implemented yet - this is aspirational\n\n    See the\n    Sample usage\n\n    .. code-block:: pycon\n\n        >>> import rdflib\n        >>> from rdflib import Graph\n        >>> from rdflib.namespace import Namespace, NamespaceManager\n        >>> EX = Namespace('http://example.com/')\n        >>> namespace_manager = NamespaceManager(Graph())\n        >>> namespace_manager.bind('ex', EX, 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__>, '__contains__': <function NamespaceManager.__contains__>, 'reset': <function NamespaceManager.reset>, 'store': <property object>, 'qname': <function NamespaceManager.qname>, 'qname_strict': <function NamespaceManager.qname_strict>, 'normalizeUri': <function NamespaceManager.normalizeUri>, 'compute_qname': <function NamespaceManager.compute_qname>, 'compute_qname_strict': <function NamespaceManager.compute_qname_strict>, 'expand_curie': <function NamespaceManager.expand_curie>, '_store_bind': <function NamespaceManager._store_bind>, '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__': {'__cache': 'Dict[str, Tuple[str, URIRef, str]]', '__cache_strict': 'Dict[str, Tuple[str, URIRef, str]]', '__strie': 'Dict[str, Any]', '__trie': 'Dict[str, Any]'}})
__init__(graph, bind_namespaces='core')[source]
Parameters:
  • graph (Graph) –

  • bind_namespaces (Literal[‘core’, ‘rdflib’, ‘none’]) –

__module__ = 'rdflib.namespace'
__weakref__

list of weak references to the object (if defined)

absolutize(uri, defrag=1)[source]
Parameters:
  • uri (str) –

  • defrag (int) –

Return type:

URIRef

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

Parameters:
Return type:

None

compute_qname(uri, generate=True)[source]
Parameters:
  • uri (str) –

  • generate (bool) –

Return type:

Tuple[str, URIRef, str]

compute_qname_strict(uri, generate=True)[source]
Parameters:
  • uri (str) –

  • generate (bool) –

Return type:

Tuple[str, str, str]

expand_curie(curie)[source]

Expand a CURIE of the form <prefix:element>, e.g. “rdf:type” into its full expression:

>>> import rdflib
>>> g = rdflib.Graph()
>>> g.namespace_manager.expand_curie("rdf:type")
rdflib.term.URIRef('http://www.w3.org/1999/02/22-rdf-syntax-ns#type')

Raises exception if a namespace is not bound to the prefix.

Parameters:

curie (str) –

Return type:

Optional[URIRef]

namespaces()[source]
Return type:

Iterable[Tuple[str, URIRef]]

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…>

Parameters:

rdfTerm (str) –

Return type:

str

qname(uri)[source]
Parameters:

uri (str) –

Return type:

str

qname_strict(uri)[source]
Parameters:

uri (str) –

Return type:

str

reset()[source]
Return type:

None

property store: Store
Return type:

Store

rdflib.namespace.is_ncname(name)[source]
Parameters:

name (str) –

Return type:

int

rdflib.namespace.split_uri(uri, split_start=['Ll', 'Lu', 'Lo', 'Lt', 'Nl', 'Nd'])[source]
Parameters:
Return type:

Tuple[str, str]