Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make graph and other methods chainable #1394

Merged
merged 6 commits into from
Sep 5, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions rdflib/collection.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,18 @@ class Collection(object):
>>> g = Graph('Memory')
>>> listItem1 = BNode()
>>> listItem2 = BNode()
>>> g.add((listName, RDF.first, Literal(1)))
>>> g.add((listName, RDF.rest, listItem1))
>>> g.add((listItem1, RDF.first, Literal(2)))
>>> g.add((listItem1, RDF.rest, listItem2))
>>> g.add((listItem2, RDF.rest, RDF.nil))
>>> g.add((listItem2, RDF.first, Literal(3)))
>>> g.add((listName, RDF.first, Literal(1))) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listName, RDF.rest, listItem1)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listItem1, RDF.first, Literal(2))) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listItem1, RDF.rest, listItem2)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listItem2, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listItem2, RDF.first, Literal(3))) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> c = Collection(g,listName)
>>> pprint([term.n3() for term in c])
[u'"1"^^<http://www.w3.org/2001/XMLSchema#integer>',
Expand Down Expand Up @@ -51,12 +57,18 @@ def n3(self):
>>> g = Graph('Memory')
>>> listItem1 = BNode()
>>> listItem2 = BNode()
>>> g.add((listName, RDF.first, Literal(1)))
>>> g.add((listName, RDF.rest, listItem1))
>>> g.add((listItem1, RDF.first, Literal(2)))
>>> g.add((listItem1, RDF.rest, listItem2))
>>> g.add((listItem2, RDF.rest, RDF.nil))
>>> g.add((listItem2, RDF.first, Literal(3)))
>>> g.add((listName, RDF.first, Literal(1))) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listName, RDF.rest, listItem1)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listItem1, RDF.first, Literal(2))) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listItem1, RDF.rest, listItem2)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listItem2, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((listItem2, RDF.first, Literal(3))) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> c = Collection(g, listName)
>>> print(c.n3()) #doctest: +NORMALIZE_WHITESPACE
( "1"^^<http://www.w3.org/2001/XMLSchema#integer>
Expand Down Expand Up @@ -131,12 +143,18 @@ def __delitem__(self, key):
>>> a = BNode('foo')
>>> b = BNode('bar')
>>> c = BNode('baz')
>>> g.add((a, RDF.first, RDF.type))
>>> g.add((a, RDF.rest, b))
>>> g.add((b, RDF.first, RDFS.label))
>>> g.add((b, RDF.rest, c))
>>> g.add((c, RDF.first, RDFS.comment))
>>> g.add((c, RDF.rest, RDF.nil))
>>> g.add((a, RDF.first, RDF.type)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((a, RDF.rest, b)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((b, RDF.first, RDFS.label)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((b, RDF.rest, c)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((c, RDF.first, RDFS.comment)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> g.add((c, RDF.rest, RDF.nil)) # doctest: +ELLIPSIS
<Graph identifier=... (<class 'rdflib.graph.Graph'>)>
>>> len(g)
6
>>> def listAncestry(node, graph):
Expand Down Expand Up @@ -213,6 +231,7 @@ def append(self, item):

self.graph.add((end, RDF.first, item))
self.graph.add((end, RDF.rest, RDF.nil))
return self

def __iadd__(self, other):

Expand All @@ -228,6 +247,7 @@ def __iadd__(self, other):
self.graph.add((end, RDF.first, item))

self.graph.add((end, RDF.rest, RDF.nil))
return self

def clear(self):
container = self.uri
Expand All @@ -237,6 +257,7 @@ def clear(self):
graph.remove((container, RDF.first, None))
graph.remove((container, RDF.rest, None))
container = rest
return self


def test():
Expand Down
9 changes: 8 additions & 1 deletion rdflib/container.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ class Container(object):
Two

>>> # add a new item
>>> b.append(Literal("Hello"))
>>> b.append(Literal("Hello")) # doctest: +ELLIPSIS
<rdflib.container.Bag object at ...>
>>> print(g.serialize(format="turtle"))
@prefix rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> .
<BLANKLINE>
Expand Down Expand Up @@ -183,6 +184,8 @@ def append(self, item):
self.graph.add((container, URIRef(elem_uri), item))
self._len += 1

return self

def append_multiple(self, other):
"""Adding multiple elements to the container to the end which are in python list other"""

Expand All @@ -196,6 +199,8 @@ def append_multiple(self, other):
elem_uri = str(RDF) + "_" + str(end)
self.graph.add((container, URIRef(elem_uri), item))

return self

def clear(self):
"""Removing all elements from the container"""

Expand All @@ -210,6 +215,7 @@ def clear(self):
else:
break
self._len = 0
return self


class Bag(Container):
Expand Down Expand Up @@ -254,6 +260,7 @@ def add_at_position(self, pos, item):
elem_uri_pos = str(RDF) + "_" + str(pos)
self.graph.add((container, URIRef(elem_uri_pos), item))
self._len += 1
return self


class NoElementException(Exception):
Expand Down
5 changes: 4 additions & 1 deletion rdflib/events.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,8 @@
accepts the event as an argument:

>>> def handler1(event): print(repr(event))
>>> d.subscribe(Event, handler1)
>>> d.subscribe(Event, handler1) # doctest: +ELLIPSIS
<rdflib.events.Dispatcher object at ...>

Now dispatch a new event into the dispatcher, and see handler1 get
fired:
Expand Down Expand Up @@ -56,6 +57,7 @@ class Dispatcher(object):

def set_map(self, amap):
self._dispatch_map = amap
return self

def get_map(self):
return self._dispatch_map
Expand All @@ -72,6 +74,7 @@ def subscribe(self, event_type, handler):
else:
lst.append(handler)
self._dispatch_map[event_type] = lst
return self

def dispatch(self, event):
"""Dispatch the given event to the subscribed handlers for
Expand Down
Loading