Skip to content

Commit

Permalink
Merge branch 'master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
ashleysommer committed May 27, 2020
2 parents 0f38064 + 037ea51 commit f5b7b56
Show file tree
Hide file tree
Showing 195 changed files with 7,904 additions and 6,034 deletions.
2 changes: 1 addition & 1 deletion docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def find_version(filename):
# The name of an image file (within the static path) to use as favicon of the
# docs. This file should be a Windows icon file (.ico) being 16x16 or 32x32
# pixels large.
html_favicon = "_static/logo-rdflib.ico"
html_favicon = "_static/RDFlib.ico"

# Add any paths that contain custom static files (such as style sheets) here,
# relative to this directory. They are copied after the builtin static files,
Expand Down
38 changes: 30 additions & 8 deletions docs/intro_to_creating_rdf.rst
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,46 @@ When removing, it is possible to leave parts of the triple unspecified (i.e. pas

.. code-block:: python
g.remove((bob, None, None)) # remove all triples about bob
g.remove((bob, None, None)) # remove all triples about bob
An example
----------

LiveJournal produces FOAF data for their users, but they seem to use
``foaf:member_name`` for a person's full name. To align with data from
other sources, it would be nice to have ``foaf:name`` act as a synonym
for ``foaf:member_name`` (a poor man's one-way
``owl:equivalentProperty``):
``foaf:member_name`` for a person's full name but ``foaf:member_name``
isn't in FOAF's namespace and perhaps they should have used ``foaf:name``

To retrieve some LiveJournal data, add a ``foaf:name`` for every
``foaf:member_name`` and then remove the ``foaf:member_name`` values to
ensure the data actually aligns with other FOAF data, we could do this:

.. code-block:: python
from rdflib import Graph
from rdflib.namespace import FOAF
g.parse("http://danbri.livejournal.com/data/foaf")
g = Graph()
# get the data
g.parse("http://danbri.livejournal.com/data/foaf")
# for every foaf:member_name, add foaf:name and remove foaf:member_name
for s, p, o in g.triples((None, FOAF['member_name'], None)):
g.add((s, FOAF['name'], o))
g.remove((s, FOAF['member_name'], o))
.. note:: Since rdflib 5.0.0, using ``foaf:member_name`` is somewhat prevented in RDFlib since FOAF is declared
as a :meth:`~rdflib.namespace.ClosedNamespace` class instance that has a closed set of members and
``foaf:member_name`` isn't one of them! If LiveJournal used RDFlib 5.0.0, an error would have been raised for
``foaf:member_name`` when the triple was created.


Creating Containers & Collections
---------------------------------
There are two convenience classes for RDF Containers & Collections which you can use instead of declaring each
triple of a Containers or a Collections individually:

* :meth:`~rdflib.container.Container` (also ``Bag``, ``Seq`` & ``Alt``) and
* :meth:`~rdflib.collection.Collection`

Note that since rdflib 5.0.0, using ``foaf:member_name`` is somewhat prevented in rdflib since FOAF is declared as a :meth:`~rdflib.namespace.ClosedNamespace`
class instance that has a closed set of members and ``foaf:member_name`` isnt one of them!
See their documentation for how.
17 changes: 10 additions & 7 deletions docs/plugintable.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,24 @@
p = {}

for (name, kind), plugin in _plugins.items():
if "/" in name: continue # skip duplicate entries for mimetypes
if "/" in name:
continue # skip duplicate entries for mimetypes
if cls == kind.__name__:
p[name]="%s.%s"%(plugin.module_path, plugin.class_name)
p[name] = "%s.%s" % (plugin.module_path, plugin.class_name)

l1 = max(len(x) for x in p)
l2 = max(10 + len(x) for x in p.values())

l1=max(len(x) for x in p)
l2=max(10+len(x) for x in p.values())

def hr():
print("="*l1,"="*l2)
print("=" * l1, "=" * l2)


hr()
print("%-*s"%(l1,"Name"), "%-*s"%(l2, "Class"))
print("%-*s" % (l1, "Name"), "%-*s" % (l2, "Class"))
hr()

for n in sorted(p):
print("%-*s"%(l1,n), ":class:`~%s`"%p[n])
print("%-*s" % (l1, n), ":class:`~%s`" % p[n])
hr()
print()
5 changes: 2 additions & 3 deletions docs/rdf_terms.rst
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ matching nodes by term-patterns probably will only be terms and not nodes.
BNodes
======

In RDF, a blank node (also called BNode) is a node in an RDF graph representing a resource for which a URI or literal is not given. The resource represented by a blank node is also called an anonymous resource. By RDF standard a blank node can only be used as subject or object in an RDF triple, although in some syntaxes like Notation 3 [1] it is acceptable to use a blank node as a predicate. If a blank node has a node ID (not all blank nodes are labelled in all RDF serializations), it is limited in scope to a serialization of a particular RDF graph, i.e. the node p1 in the subsequent example does not represent the same node as a node named p1 in any other graph --`wikipedia`__
In RDF, a blank node (also called BNode) is a node in an RDF graph representing a resource for which a URI or literal is not given. The resource represented by a blank node is also called an anonymous resource. By RDF standard a blank node can only be used as subject or object in an RDF triple, although in some syntaxes like Notation 3 [1] it is acceptable to use a blank node as a predicate. If a blank node has a node ID (not all blank nodes are labelled in all RDF serializations), it is limited in scope to a serialization of a particular RDF graph, i.e. the node p1 in the subsequent example does not represent the same node as a node named p1 in any other graph --`wikipedia`__


.. __: http://en.wikipedia.org/wiki/Blank_node
Expand All @@ -40,7 +40,7 @@ BNodes
URIRefs
=======

A URI reference within an RDF graph is a Unicode string that does not contain any control characters ( #x00 - #x1F, #x7F-#x9F) and would produce a valid URI character sequence representing an absolute URI with optional fragment identifier -- `W3 RDF Concepts`__
A URI reference within an RDF graph is a Unicode string that does not contain any control characters ( #x00 - #x1F, #x7F-#x9F) and would produce a valid URI character sequence representing an absolute URI with optional fragment identifier -- `W3 RDF Concepts`__

.. __: http://www.w3.org/TR/rdf-concepts/#section-Graph-URIref

Expand Down Expand Up @@ -159,4 +159,3 @@ All this happens automatically when creating ``Literal`` objects by passing Pyth

You can add custom data-types with :func:`rdflib.term.bind`, see also :mod:`examples.custom_datatype`


2 changes: 1 addition & 1 deletion examples/conjunctive_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@

# query the conjunction of all graphs
xx = None
for x in g[mary: ns.loves / ns.hasCuteName]:
for x in g[mary : ns.loves / ns.hasCuteName]:
xx = x
print("Q: Who does Mary love?")
print("A: Mary loves {}".format(xx))
1 change: 0 additions & 1 deletion examples/film.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@

from rdflib import BNode, ConjunctiveGraph, URIRef, Literal, Namespace, RDF
from rdflib.namespace import FOAF, DC
from six.moves import input

storefn = os.path.expanduser("~/movies.n3")
# storefn = '/home/simon/codes/film.dev/movies.n3'
Expand Down
4 changes: 2 additions & 2 deletions examples/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,9 @@

graph.load("foaf.n3", format="n3")

for person in graph[: RDF.type: FOAF.Person]:
for person in graph[: RDF.type : FOAF.Person]:

friends = list(graph[person: FOAF.knows * "+" / FOAF.name])
friends = list(graph[person : FOAF.knows * "+" / FOAF.name])
if friends:
print("%s's circle of friends:" % graph.value(person, FOAF.name))
for name in friends:
Expand Down
19 changes: 12 additions & 7 deletions examples/sparqlstore_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,21 @@
graph = Graph("SPARQLStore", identifier="http://dbpedia.org")
graph.open("http://dbpedia.org/sparql")

pop = graph.value(
URIRef("http://dbpedia.org/resource/Berlin"),
dbo.populationTotal)
pop = graph.value(URIRef("http://dbpedia.org/resource/Berlin"), dbo.populationTotal)

print("According to DBPedia, Berlin has a population of {0:,}".format(int(pop), ',d').replace(",", "."))
print(
"According to DBPedia, Berlin has a population of {0:,}".format(
int(pop), ",d"
).replace(",", ".")
)

# using a SPARQLStore object directly
s = SPARQLStore(endpoint="http://dbpedia.org/sparql")
s.open(None)
pop = graph.value(
URIRef("http://dbpedia.org/resource/Brisbane"),
dbo.populationTotal)
print("According to DBPedia, Brisbane has a population of " "{0:,}".format(int(pop), ',d'))
URIRef("http://dbpedia.org/resource/Brisbane"), dbo.populationTotal
)
print(
"According to DBPedia, Brisbane has a population of "
"{0:,}".format(int(pop), ",d")
)
2 changes: 1 addition & 1 deletion examples/swap_primer.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@
# dataset from the example, and start
# with a fresh new graph.

del(primer)
del primer
primer = ConjunctiveGraph()

# Lets start with a verbatim string straight from the primer text:
Expand Down
26 changes: 3 additions & 23 deletions rdflib/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,16 +85,14 @@

import sys

assert sys.version_info >= (2, 7, 0), "rdflib requires Python 2.7 or higher"

import logging

logger = logging.getLogger(__name__)
_interactive_mode = False
try:
import __main__

if not hasattr(__main__, "__file__") and sys.stdout != None and sys.stderr.isatty():
if not hasattr(__main__, "__file__") and sys.stdout is not None and sys.stderr.isatty():
# show log messages in interactive mode
_interactive_mode = True
logger.setLevel(logging.INFO)
Expand All @@ -115,26 +113,6 @@
del sys


import six

try:
six.unichr(0x10FFFF)
except ValueError:
import warnings

warnings.warn(
"You are using a narrow Python build!\n"
"This means that your Python does not properly support chars > 16bit.\n"
'On your system chars like c=u"\\U0010FFFF" will have a len(c)==2.\n'
"As this can cause hard to debug problems with string processing\n"
"(slicing, regexp, ...) later on, we strongly advise to use a wide\n"
"Python build in production systems.",
ImportWarning,
)
del warnings
del six


NORMALIZE_LITERALS = True
"""
If True - Literals lexical forms are normalized when created.
Expand Down Expand Up @@ -218,3 +196,5 @@
assert query

from rdflib import util

from .container import *
14 changes: 7 additions & 7 deletions rdflib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from rdflib.term import Literal


__all__ = ['Collection']
__all__ = ["Collection"]


class Collection(object):
Expand Down Expand Up @@ -67,7 +67,7 @@ def n3(self):
"2"^^<http://www.w3.org/2001/XMLSchema#integer>
"3"^^<http://www.w3.org/2001/XMLSchema#integer> )
"""
return "( %s )" % (' '.join([i.n3() for i in self]))
return "( %s )" % (" ".join([i.n3() for i in self]))

def _get_container(self, index):
"""Gets the first, rest holding node at index."""
Expand Down Expand Up @@ -103,8 +103,7 @@ def index(self, item):
elif not newLink:
raise Exception("Malformed RDF Collection: %s" % self.uri)
else:
assert len(newLink) == 1, \
"Malformed RDF Collection: %s" % self.uri
assert len(newLink) == 1, "Malformed RDF Collection: %s" % self.uri
listName = newLink[0]

def __getitem__(self, key):
Expand Down Expand Up @@ -246,21 +245,22 @@ def clear(self):

def test():
import doctest

doctest.testmod()


if __name__ == "__main__":
test()

from rdflib import Graph

g = Graph()

c = Collection(g, BNode())

assert len(c) == 0

c = Collection(
g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")])
c = Collection(g, BNode(), [Literal("1"), Literal("2"), Literal("3"), Literal("4")])

assert len(c) == 4

Expand All @@ -272,7 +272,7 @@ def test():

try:
del c[500]
except IndexError as i:
except IndexError:
pass

c.append(Literal("5"))
Expand Down

0 comments on commit f5b7b56

Please sign in to comment.