Skip to content

Commit

Permalink
Merge branch 'main' into main
Browse files Browse the repository at this point in the history
  • Loading branch information
aucampia committed Mar 13, 2023
2 parents f20efdd + 35a3537 commit 82677ee
Show file tree
Hide file tree
Showing 146 changed files with 2,191 additions and 792 deletions.
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ repos:
- repo: https://github.com/psf/black
# WARNING: version should be the same as in `pyproject.toml`
# Using git ref spec because of https://github.com/psf/black/issues/2493
rev: 'refs/tags/22.12.0:refs/tags/22.12.0'
rev: 'refs/tags/23.1.0:refs/tags/23.1.0'
hooks:
- id: black
pass_filenames: false
Expand Down
2 changes: 1 addition & 1 deletion docker/latest/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/library/python:3.11.2-slim@sha256:64eb3661444eb66fa45d5aaaba743f165990235dd7da61a1f5042c61e98da37a
FROM docker.io/library/python:3.11.2-slim@sha256:d0e839882b87135b355361efeb9e9030c9d2a808da06434f4c99eb4009c15e64

COPY docker/latest/requirements.txt /var/tmp/build/

Expand Down
2 changes: 1 addition & 1 deletion docker/unstable/Dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
FROM docker.io/library/python:3.11.2-slim@sha256:64eb3661444eb66fa45d5aaaba743f165990235dd7da61a1f5042c61e98da37a
FROM docker.io/library/python:3.11.2-slim@sha256:d0e839882b87135b355361efeb9e9030c9d2a808da06434f4c99eb4009c15e64

# This file is generated from docker:unstable in Taskfile.yml
COPY var/requirements.txt /var/tmp/build/
Expand Down
2 changes: 1 addition & 1 deletion docs/developers.rst
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ This document describes the process and conventions to follow when
developing RDFLib code.

* Please be as Pythonic as possible (:pep:`8`).
* Code should be formatted using `black <https://github.com/psf/black>`_ and we use Black v22.12.0, with the black config in ``pyproject.toml``.
* Code should be formatted using `black <https://github.com/psf/black>`_ and we use Black v23.1.0, with the black config in ``pyproject.toml``.
* Code should also pass `flake8 <https://flake8.pycqa.org/en/latest/>`_ linting
and `mypy <http://mypy-lang.org/>`_ type checking.
* You must supply tests for new code.
Expand Down
18 changes: 11 additions & 7 deletions examples/berkeleydb_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
* does not delete the DB at the end so you can see it on disk
"""
import os
from rdflib import ConjunctiveGraph, Namespace, Literal
from rdflib.store import NO_STORE, VALID_STORE
import tempfile

from rdflib import ConjunctiveGraph, Literal, Namespace
from rdflib.plugins.stores.berkeleydb import has_bsddb
from rdflib.store import NO_STORE, VALID_STORE


def example_1():
"""Creates a ConjunctiveGraph and performs some BerkeleyDB tasks with it"""
Expand Down Expand Up @@ -98,10 +100,10 @@ def example_2():
9719
...
"""
from urllib.request import urlopen, Request
from urllib.error import HTTPError
import json
import base64
import json
from urllib.error import HTTPError
from urllib.request import Request, urlopen

g = ConjunctiveGraph("BerkeleyDB")
g.open("gsg_vocabs", create=True)
Expand Down Expand Up @@ -129,5 +131,7 @@ def example_2():


if __name__ == "__main__":
example_1()
example_2()
if has_bsddb:
# Only run the examples if BerkeleyDB is available
example_1()
example_2()
7 changes: 3 additions & 4 deletions examples/conjunctive_graphs.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,11 @@
conjunction (union) of all the graphs.
"""

from rdflib import Namespace, Literal, URIRef
from rdflib.graph import Graph, ConjunctiveGraph
from rdflib import Literal, Namespace, URIRef
from rdflib.graph import ConjunctiveGraph, Graph
from rdflib.plugins.stores.memory import Memory

if __name__ == "__main__":

LOVE = Namespace("http://love.com#")
LOVERS = Namespace("http://love.com/lovers/")

Expand Down Expand Up @@ -59,7 +58,7 @@

print("Query the conjunction of all graphs:")
xx = None
for x in g[mary : LOVE.loves / LOVE.hasCuteName]:
for x in g[mary : LOVE.loves / LOVE.hasCuteName]: # type: ignore[misc]
xx = x
print("Q: Who does Mary love?")
print("A: Mary loves {}".format(xx))
5 changes: 2 additions & 3 deletions examples/custom_datatype.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@
"""


from rdflib import Graph, Literal, Namespace, XSD
from rdflib import term
from rdflib import XSD, Graph, Literal, Namespace, term

if __name__ == "__main__":

# Complex numbers are not registered by default
# No custom constructor/serializer needed since
# complex('(2+3j)') works fine
Expand Down Expand Up @@ -46,4 +44,5 @@

# Compare with the original python complex object (should be True)
# l2[2] is the object of the triple
assert isinstance(l2[2], Literal)
print(l2[2].value == c)
26 changes: 16 additions & 10 deletions examples/custom_eval.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,19 @@
}
"""

import rdflib

from rdflib.plugins.sparql.evaluate import evalBGP
from pathlib import Path

import rdflib
from rdflib.namespace import FOAF, RDF, RDFS
from rdflib.plugins.sparql.evaluate import evalBGP

inferredSubClass = RDFS.subClassOf * "*" # any number of rdfs.subClassOf
EXAMPLES_DIR = Path(__file__).parent


inferred_sub_class = (
RDFS.subClassOf * "*" # type: ignore[operator]
) # any number of rdfs.subClassOf


def customEval(ctx, part):
Expand All @@ -36,7 +43,7 @@ def customEval(ctx, part):
if t[1] == RDF.type:
bnode = rdflib.BNode()
triples.append((t[0], t[1], bnode))
triples.append((bnode, inferredSubClass, t[2]))
triples.append((bnode, inferred_sub_class, t[2]))
else:
triples.append(t)

Expand All @@ -47,24 +54,23 @@ def customEval(ctx, part):


if __name__ == "__main__":

# add function directly, normally we would use setuptools and entry_points
rdflib.plugins.sparql.CUSTOM_EVALS["exampleEval"] = customEval

g = rdflib.Graph()
g.parse("foaf.n3")
g.parse(f"{EXAMPLES_DIR / 'foaf.n3'}")

# Add the subClassStmt so that we can query for it!
g.add((FOAF.Person, RDFS.subClassOf, FOAF.Agent))

# Find all FOAF Agents
for x in g.query(
f"""
PREFIX foaf: <{FOAF}>
SELECT *
PREFIX foaf: <{FOAF}>
SELECT *
WHERE {{
?s a foaf:Agent .
?s a foaf:Agent .
}}
"""
):
Expand Down
15 changes: 13 additions & 2 deletions examples/datasets.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,18 @@
and remove things from it.
"""

from rdflib import Dataset, URIRef, Literal, Namespace
from rdflib import Dataset, Literal, Namespace, URIRef

# Note regarding `mypy: ignore_errors=true`:
#
# This example is using URIRef values as context identifiers. This is contrary
# to the type hints, but it does work. Most likely the type hints are wrong.
# Ideally we should just use `# type: ignore` comments for the lines that are
# causing problems, but for some reason the error occurs on different lines with
# different python versions, so the only option is to ignore errors for the
# whole file.

# mypy: ignore_errors=true

#
# Create & Add
Expand Down Expand Up @@ -99,7 +110,7 @@
"""
print("Printing all triple from one Graph in the Dataset:")
print("---")
for triple in d.triples((None, None, None, graph_1)):
for triple in d.triples((None, None, None, graph_1)): # type: ignore[arg-type]
print(triple)
print("---")
print()
Expand Down
9 changes: 6 additions & 3 deletions examples/foafpaths.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,16 @@
This example shows how to get the name of friends (i.e values two steps away x knows y, y name z) with a single query.
"""

from rdflib import URIRef, Graph
from pathlib import Path

from rdflib import Graph, URIRef
from rdflib.namespace import FOAF

if __name__ == "__main__":
EXAMPLES_DIR = Path(__file__).parent

if __name__ == "__main__":
g = Graph()
g.parse("foaf.n3")
g.parse(f"{EXAMPLES_DIR / 'foaf.n3'}")

tim = URIRef("http://www.w3.org/People/Berners-Lee/card#i")

Expand Down
11 changes: 8 additions & 3 deletions examples/prepared_query.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,27 @@
``initBindings`` keyword parameter.
"""

from pathlib import Path

import rdflib
from rdflib.plugins.sparql import prepareQuery
from rdflib.namespace import FOAF
from rdflib.plugins.sparql import prepareQuery

EXAMPLES_DIR = Path(__file__).parent

if __name__ == "__main__":

q = prepareQuery(
"SELECT ?name WHERE { ?person foaf:knows/foaf:name ?name . }",
initNs={"foaf": FOAF},
)

g = rdflib.Graph()
g.parse("foaf.n3")
g.parse(f"{EXAMPLES_DIR / 'foaf.n3'}")

tim = rdflib.URIRef("http://www.w3.org/People/Berners-Lee/card#i")

for row in g.query(q, initBindings={"person": tim}):
# For select queries, the Result object is an iterable of ResultRow
# objects.
assert isinstance(row, rdflib.query.ResultRow)
print(row.name)
2 changes: 1 addition & 1 deletion examples/resource_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
This example shows g.resource() in action.
"""

from rdflib import Graph, RDF, RDFS, Literal
from rdflib import RDF, RDFS, Graph, Literal
from rdflib.namespace import FOAF

if __name__ == "__main__":
Expand Down
14 changes: 9 additions & 5 deletions examples/simple_example.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
from rdflib import Graph, Literal, BNode, RDF
from rdflib.namespace import FOAF, DC
import os.path
from tempfile import TemporaryDirectory

if __name__ == "__main__":
from rdflib import RDF, BNode, Graph, Literal
from rdflib.namespace import DC, FOAF

if __name__ == "__main__":
store = Graph()

# Bind a few prefix, namespace pairs for pretty output
Expand All @@ -29,9 +31,11 @@
for mbox in store.objects(person, FOAF["mbox"]):
print(mbox)

print("--- saving RDF to a file (donna_foaf.rdf) ---")
tmp_dir = TemporaryDirectory()
output_file = os.path.join(tmp_dir.name, "donna_foaf.rdf")
print(f"--- saving RDF to a file ({output_file}) ---")
# Serialize the store as RDF/XML to the file donna_foaf.rdf.
store.serialize("donna_foaf.rdf", format="pretty-xml", max_depth=3)
store.serialize(f"{output_file}", format="pretty-xml", max_depth=3)

# Let's show off the serializers
print()
Expand Down
14 changes: 9 additions & 5 deletions examples/slice.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,20 @@
See :meth:`rdflib.graph.Graph.__getitem__` for details
"""

from rdflib import Graph, RDF
from pathlib import Path

from rdflib import RDF, Graph
from rdflib.namespace import FOAF

if __name__ == "__main__":
EXAMPLES_DIR = Path(__file__).parent


if __name__ == "__main__":
graph = Graph()
graph.parse("foaf.n3", format="n3")
graph.parse(f"{EXAMPLES_DIR / 'foaf.n3'}", format="n3")

for person in graph[: RDF.type : FOAF.Person]:
friends = list(graph[person : FOAF.knows * "+" / FOAF.name])
for person in graph[: RDF.type : FOAF.Person]: # type: ignore[misc]
friends = list(graph[person : FOAF.knows * "+" / FOAF.name]) # type: ignore[operator]
if friends:
print(f"{graph.value(person, FOAF.name)}'s circle of friends:")
for name in friends:
Expand Down
10 changes: 9 additions & 1 deletion examples/smushing.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,25 @@
with my existing data.
"""

from pathlib import Path

from rdflib import Graph, Namespace
from rdflib.namespace import FOAF

STABLE = Namespace("http://example.com/person/mbox_sha1sum/")

EXAMPLES_DIR = Path(__file__).parent

if __name__ == "__main__":
g = Graph()
g.parse("smushingdemo.n3", format="n3")
g.parse(f"{EXAMPLES_DIR / 'smushingdemo.n3'}", format="n3")

newURI = {} # old subject : stable uri
for s, p, o in g.triples((None, FOAF["mbox_sha1sum"], None)):
# For this graph, all objects are Identifiers, which is a subclass of
# string. `n3` does allow for objects which are not Identifiers, like
# subgraphs.
assert isinstance(o, str)
newURI[s] = STABLE[o]

out = Graph()
Expand Down
12 changes: 11 additions & 1 deletion examples/sparql_query_example.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,26 @@
:attr:`~rdflib.query.Result.vars` contains the variables
"""

import logging
import sys
from pathlib import Path

import rdflib

EXAMPLES_DIR = Path(__file__).parent

if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG, stream=sys.stderr)

g = rdflib.Graph()
g.parse("foaf.n3", format="n3")
g.parse(f"{EXAMPLES_DIR / 'foaf.n3'}", format="n3")

# The QueryProcessor knows the FOAF prefix from the graph
# which in turn knows it from reading the N3 RDF file
for row in g.query("SELECT ?s WHERE { [] foaf:knows ?s .}"):
# For select queries, the Result object is an iterable of ResultRow
# objects.
assert isinstance(row, rdflib.query.ResultRow)
print(row.s)
# or row["s"]
# or row[rdflib.Variable("s")]
Loading

0 comments on commit 82677ee

Please sign in to comment.