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
- __module__ = 'rdflib.namespace'¶
- 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
- __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__': {}})¶
- __module__ = 'rdflib.namespace'¶
- __weakref__¶
list of weak references to the object (if defined)
- 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’]) –
- __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)
- 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
- 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.