summaryrefslogtreecommitdiff
path: root/examples/custom_eval.py
diff options
context:
space:
mode:
authorIwan Aucamp <aucampia@gmail.com>2023-03-13 01:18:21 +0100
committerGitHub <noreply@github.com>2023-03-13 01:18:21 +0100
commit35a35375d213afe83a825d7dfcc59008f8e2352c (patch)
tree1fa971ef3a9e897e19b5df5fc304fe9c02697e41 /examples/custom_eval.py
parent7a7cc1f929ff6fbc8c3238368e518a00af1025b6 (diff)
downloadrdflib-35a35375d213afe83a825d7dfcc59008f8e2352c.tar.gz
fix: validation issues with examples (#2269)
I want to add examples for securing RDFLib network access using `sys.addaudithook` and `urllib.request.install_opener`, but I want to also validate the examples in our CI pipeline, so we can demonstrate they work to our users. This change adds validation for all examples, and the addition of the security examples in a seperate PR will then also get validated.
Diffstat (limited to 'examples/custom_eval.py')
-rw-r--r--examples/custom_eval.py26
1 files changed, 16 insertions, 10 deletions
diff --git a/examples/custom_eval.py b/examples/custom_eval.py
index 36998087..f8dfd390 100644
--- a/examples/custom_eval.py
+++ b/examples/custom_eval.py
@@ -16,12 +16,19 @@ i.e. in your setup.py::
}
"""
-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):
@@ -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)
@@ -47,12 +54,11 @@ 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))
@@ -60,11 +66,11 @@ if __name__ == "__main__":
# 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 .
}}
"""
):