Skip to content

Commit

Permalink
Merge pull request #1382 from juanjosegzl/iter-dataset
Browse files Browse the repository at this point in the history
Iterate over dataset return quads
  • Loading branch information
nicholascar committed Aug 28, 2021
2 parents e9fea54 + 1048807 commit c1be731
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 5 deletions.
33 changes: 29 additions & 4 deletions rdflib/graph.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import Optional, Union, Type, cast, overload
from typing import Optional, Union, Type, cast, overload, Generator, Tuple
import logging
from warnings import warn
import random
Expand Down Expand Up @@ -29,6 +29,9 @@

logger = logging.getLogger(__name__)

# Type aliases to make unpacking what's going on a little more human friendly
ContextNode = Union[BNode, URIRef]
DatasetQuad = Tuple[Node, URIRef, Node, Optional[ContextNode]]

__doc__ = """\
Expand Down Expand Up @@ -1759,8 +1762,8 @@ class Dataset(ConjunctiveGraph):
rdflib.term.URIRef("http://example.org/y"),
rdflib.term.Literal("bar"))
>>>
>>> # querying quads return quads; the fourth argument can be unrestricted
>>> # or restricted to a graph
>>> # querying quads() return quads; the fourth argument can be unrestricted
>>> # (None) or restricted to a graph
>>> for q in ds.quads((None, None, None, None)): # doctest: +SKIP
... print(q) # doctest: +NORMALIZE_WHITESPACE
(rdflib.term.URIRef("http://example.org/a"),
Expand All @@ -1776,7 +1779,24 @@ class Dataset(ConjunctiveGraph):
rdflib.term.Literal("foo-bar"),
rdflib.term.URIRef("http://www.example.com/gr"))
>>>
>>> for q in ds.quads((None,None,None,g)): # doctest: +SKIP
>>> # unrestricted looping is equivalent to iterating over the entire Dataset
>>> for q in ds: # doctest: +SKIP
... print(q) # doctest: +NORMALIZE_WHITESPACE
(rdflib.term.URIRef("http://example.org/a"),
rdflib.term.URIRef("http://www.example.org/b"),
rdflib.term.Literal("foo"),
None)
(rdflib.term.URIRef("http://example.org/x"),
rdflib.term.URIRef("http://example.org/y"),
rdflib.term.Literal("bar"),
rdflib.term.URIRef("http://www.example.com/gr"))
(rdflib.term.URIRef("http://example.org/x"),
rdflib.term.URIRef("http://example.org/z"),
rdflib.term.Literal("foo-bar"),
rdflib.term.URIRef("http://www.example.com/gr"))
>>>
>>> # resticting iteration to a graph:
>>> for q in ds.quads((None, None, None, g)): # doctest: +SKIP
... print(q) # doctest: +NORMALIZE_WHITESPACE
(rdflib.term.URIRef("http://example.org/x"),
rdflib.term.URIRef("http://example.org/y"),
Expand Down Expand Up @@ -1896,6 +1916,11 @@ def quads(self, quad):
else:
yield s, p, o, c.identifier

def __iter__(self) -> Generator[DatasetQuad, None, None]:
"""Iterates over all quads in the store"""
return self.quads((None, None, None, None))



class QuotedGraph(Graph):
"""
Expand Down
31 changes: 30 additions & 1 deletion test/test_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,36 @@ def testNotUnion(self):
self.assertEqual(list(self.graph.objects(self.tarek, None)), [])
self.assertEqual(list(g1.objects(self.tarek, None)), [self.pizza])


def testIter(self):
"""PR 1382: adds __iter__ to Dataset"""
d = Dataset()
uri_a = URIRef("https://example.com/a")
uri_b = URIRef("https://example.com/b")
uri_c = URIRef("https://example.com/c")
uri_d = URIRef("https://example.com/d")

d.add_graph(URIRef("https://example.com/g1"))
d.add((uri_a, uri_b, uri_c, URIRef("https://example.com/g1")))
d.add((uri_a, uri_b, uri_c, URIRef(
"https://example.com/g1"))) # pointless addition: duplicates above

d.add_graph(URIRef("https://example.com/g2"))
d.add((uri_a, uri_b, uri_c, URIRef("https://example.com/g2")))
d.add((uri_a, uri_b, uri_d, URIRef("https://example.com/g1"))) # new, uri_d

# traditional iterator
i_trad = 0
for t in d.quads((None, None, None)):
i_trad += 1

# new Dataset.__iter__ iterator
i_new = 0
for t in d:
i_new += 1

self.assertEqual(i_new, i_trad) # both should be 3


# dynamically create classes for each registered Store

pluginname = None
Expand Down

0 comments on commit c1be731

Please sign in to comment.