Skip to content

Commit c24f444

Browse files
author
nicholascar
committed
'none' option
1 parent 6a6f632 commit c24f444

File tree

2 files changed

+81
-25
lines changed

2 files changed

+81
-25
lines changed

docs/namespaces_and_bindings.rst

Lines changed: 71 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -10,37 +10,88 @@ The :mod:`rdflib.namespace` defines the :class:`rdflib.namespace.Namespace` clas
1010

1111
from rdflib import Namespace
1212

13-
n = Namespace("http://example.org/")
14-
n.Person # as attribute
15-
# = rdflib.term.URIRef("http://example.org/Person")
13+
EX = Namespace("http://example.org/")
14+
EX.Person # a Python attribute for EX. This example is equivalent to rdflib.term.URIRef("http://example.org/Person")
1615

17-
n['first%20name'] # as item - for things that are not valid python identifiers
18-
# = rdflib.term.URIRef("http://example.org/first%20name")
16+
# use dict notation for things that are not valid Python identifiers, e.g.:
17+
n['first%20name'] # as rdflib.term.URIRef("http://example.org/first%20name")
1918

20-
Note that if a name string is valid for use in an RDF namespace but not valid as a Python identifier, such as '1234', it must be addressed with the "item" syntax (using the "attribute" syntax will raise a Syntax Error).
19+
These two styles of namespace creation - object attribute and dict - are equivalent and are made available just to allow for valid
20+
RDF namespaces and URIs that are not valid Python identifiers. This isn't just for syntactic things like spaces, as per
21+
the example of ``first%20name`` above, but also for Python reserved words like ``class`` or ``while``, so for the URI
22+
``http://example.org/class``, create it with ``EX['class']``, not ``EX.class``.
2123

22-
The ``namespace`` module also defines many common namespaces such as RDF, RDFS, OWL, FOAF, SKOS, PROF, etc.
24+
Common Namespaces
25+
-----------------
2326

24-
Namespaces can also be associated with prefixes, in a :class:`rdflib.namespace.NamespaceManager`, i.e. using ``foaf`` for ``http://xmlns.com/foaf/0.1/``. Each RDFLib graph has a :attr:`~rdflib.graph.Graph.namespace_manager` that keeps a list of namespace to prefix mappings. The namespace manager is populated when reading in RDF, and these prefixes are used when serialising RDF, or when parsing SPARQL queries. Additional prefixes can be bound with the :meth:`rdflib.graph.bind` method.
27+
The ``namespace`` module defines many common namespaces such as RDF, RDFS, OWL, FOAF, SKOS, PROF, etc. The list of the
28+
namespaces provided grows with user contributions to RDFLib.
29+
30+
These Namespaces, and any others that users define, can also be associated with prefixes using the :class:`rdflib.namespace.NamespaceManager`, e.g. using ``foaf`` for ``http://xmlns.com/foaf/0.1/``.
31+
32+
Each RDFLib graph has a :attr:`~rdflib.graph.Graph.namespace_manager` that keeps a list of namespace to prefix mappings. The namespace manager is populated when reading in RDF, and these prefixes are used when serialising RDF, or when parsing SPARQL queries. Prefixes can be bound with the :meth:`rdflib.graph.bind` method::
33+
34+
from rdflib import Graph, Namespace
35+
from rdflib.namespace import FOAF
36+
37+
EX = Namespace("http://example.org/")
38+
39+
g = Graph()
40+
g.bind("foaf", FOAF) # bind an RDFLib-provided namespace to a prefix
41+
g.bind("ex", EX) # bind a user-declared namespace to a prefix
42+
43+
44+
The :meth:`rdflib.graph.bind` method is actually supplied by the :class:`rdflib.namespace.NamespaceManager` class - see next.
2545

2646
NamespaceManager
2747
----------------
2848

49+
Each RDFLib graph comes with a :class:`rdflib.namespace.NamespaceManager` instance in the `namespace_manager` field; you can use the `bind` method of this instance to bind a prefix to a namespace URI,
50+
as above, however note that the `NamespaceManager` automatically performs some bindings according to a selected strategy.
51+
52+
Namespace binding strategies are indicated with the `bind_namespaces` input parameter to `NamespaceManager` instances
53+
and may be set via ``Graph`` also::
54+
55+
from rdflib import Graph
56+
from rdflib.namespace import NamespaceManager
57+
58+
g = Graph(bind_namespaces="rdflib") # bind via Graph
59+
60+
g2 = Graph()
61+
nm = NamespaceManager(g2, bind_namespaces="rdflib") # bind via NamespaceManager
62+
2963

30-
Each graph comes with a `NamespaceManager`__ instance in the `namespace_manager` field; you can use the `bind` method of this instance to bind a prefix to a namespace URI::
64+
Valid strategies are:
3165

32-
myGraph.namespace_manager.bind('prefix', URIRef('scheme:my-namespace-uri:'))
33-
myGraph.namespace_manager.bind('owl', OWL_NS, override=False)
66+
* core:
67+
* binds several core RDF prefixes only
68+
* owl, rdf, rdfs, xsd, xml from the NAMESPACE_PREFIXES_CORE object
69+
* this is default
70+
* rdflib:
71+
* binds all the namespaces shipped with RDFLib as DefinedNamespace instances
72+
* all the core namespaces and all the following: brick, csvw, dc, dcat
73+
* dcmitype, cdterms, dcam, doap, foaf, geo, odrl, org, prof, prov, qb, sdo
74+
* sh, skos, sosa, ssn, time, vann, void
75+
* see the NAMESPACE_PREFIXES_RDFLIB object in :class:`rdflib.namespace` for up-to-date list
76+
* none:
77+
* binds no namespaces to prefixes
78+
* note this is NOT default behaviour
79+
* cc:
80+
* using prefix bindings from prefix.cc which is a online prefixes database
81+
* not implemented yet - this is aspirational
3482

35-
It has a method to normalize a given url :
83+
`NamespaceManager` also has a method to normalize a given url::
3684

37-
myGraph.namespace_manager.normalizeUri(t)
85+
from rdflib.namespace import NamespaceManager
86+
87+
nm = NamespaceManager(Graph())
88+
nm.normalizeUri(t)
3889

3990

4091
For simple output, or simple serialisation, you often want a nice
41-
readable representation of a term. All terms have a
42-
``.n3(namespace_manager = None)`` method, which will return a suitable
43-
N3 format::
92+
readable representation of a term. All RDFLib terms have a
93+
``.n3()`` method, which will return a suitable N3 format and into which you can supply a NamespaceManager instance
94+
to provide prefixes, i.e. ``.n3(namespace_manager=some_nm)``::
4495

4596
>>> from rdflib import Graph, URIRef, Literal, BNode
4697
>>> from rdflib.namespace import FOAF, NamespaceManager
@@ -59,16 +110,15 @@ N3 format::
59110
>>> l.n3()
60111
'"2"^^<http://www.w3.org/2001/XMLSchema#integer>'
61112
62-
>>> l.n3(g.namespace_manager)
113+
>>> l.n3(NamespaceManager(Graph(), bind_namespaces="core"))
63114
'"2"^^xsd:integer'
64115
65-
The namespace manage also has a useful method compute_qname
66-
g.namespace_manager.compute_qname(x) which takes an url and decomposes it into the parts::
116+
The namespace manage also has a useful method ``compute_qname``
117+
``g.namespace_manager.compute_qname(x)`` (or just ``g.compute_qname(x)``) which takes a URI and decomposes it into the parts::
67118

68119
self.assertEqual(g.compute_qname(URIRef("http://foo/bar#baz")),
69120
("ns2", URIRef("http://foo/bar#"), "baz"))
70121
71-
__ http://rdflib.net/rdflib-2.4.0/html/public/rdflib.syntax.NamespaceManager.NamespaceManager-class.html
72122

73123

74124
Namespaces in SPARQL Queries
@@ -78,7 +128,7 @@ The ``initNs`` argument supplied to :meth:`~rdflib.graph.Graph.query` is a dicti
78128
If you pass no ``initNs`` argument, the namespaces registered with the graphs namespace_manager are used::
79129

80130
from rdflib.namespace import FOAF
81-
graph.query('SELECT * WHERE { ?p a foaf:Person }', initNs={ 'foaf': FOAF })
131+
graph.query('SELECT * WHERE { ?p a foaf:Person }', initNs={'foaf': FOAF})
82132

83133

84134
In order to use an empty prefix (e.g. ``?a :knows ?b``), use a ``PREFIX`` directive with no prefix in the SPARQL query to set a default namespace:

rdflib/namespace/__init__.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,17 +312,21 @@ class NamespaceManager(object):
312312
is one of the following:
313313
314314
* core:
315-
* binds several core prefixes only
315+
* binds several core RDF prefixes only
316316
* owl, rdf, rdfs, xsd, xml from the NAMESPACE_PREFIXES_CORE object
317-
* this is detfault
317+
* this is default
318318
* rdflib:
319319
* binds all the namespaces shipped with RDFLib as DefinedNamespace instances
320320
* all the core namespaces and all the following: brick, csvw, dc, dcat
321321
* dcmitype, cdterms, dcam, doap, foaf, geo, odrl, org, prof, prov, qb, sdo
322322
* sh, skos, sosa, ssn, time, vann, void
323323
* see the NAMESPACE_PREFIXES_RDFLIB object for the up-to-date list
324+
* none:
325+
* binds no namespaces to prefixes
326+
* note this is NOT default behaviour
324327
* cc:
325328
* using prefix bindings from prefix.cc which is a online prefixes database
329+
* not implemented yet - this is aspirational
326330
327331
See the
328332
Sample usage
@@ -355,6 +359,10 @@ def __init__(self, graph: "Graph", bind_namespaces: str = "core"):
355359

356360
# bind Namespaces as per options.
357361
# default is core
362+
if bind_namespaces == "none":
363+
# binds no namespaces to prefixes
364+
# note this is NOT default
365+
pass
358366
if bind_namespaces == "core":
359367
# bind a few core RDF namespaces
360368
for prefix, ns in NAMESPACE_PREFIXES_CORE.items():
@@ -372,8 +380,6 @@ def __init__(self, graph: "Graph", bind_namespaces: str = "core"):
372380
# work out remainder - namespaces without prefixes
373381
# only look those ones up
374382
raise NotImplementedError("Haven't got to this option yet")
375-
else: # bind_namespaces is None
376-
pass # bind nothing
377383

378384
def __contains__(self, ref: str) -> bool:
379385
# checks if a reference is in any of the managed namespaces with syntax

0 commit comments

Comments
 (0)