-
Notifications
You must be signed in to change notification settings - Fork 555
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #271 from RDFLib/slice
Slices as syntactic sugar for graphs / resources
- Loading branch information
Showing
4 changed files
with
162 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
|
||
from rdflib import Graph, URIRef | ||
import unittest | ||
|
||
class GraphSlice(unittest.TestCase): | ||
|
||
def testSlice(self): | ||
""" | ||
We pervert the slice object, | ||
and use start, stop, step as subject, predicate, object | ||
all operations return generators over full triples | ||
""" | ||
|
||
sl=lambda x,y: self.assertEquals(len(list(x)),y) | ||
soe=lambda x,y: self.assertEquals(set([a[2] for a in x]),set(y)) # equals objects | ||
g=self.graph | ||
|
||
# Single terms are all trivial: | ||
|
||
# single index slices by subject, i.e. return triples((x,None,None)) | ||
# tell me everything about "tarek" | ||
sl(g[self.tarek],2) | ||
|
||
# single slice slices by s,p,o, with : used to split | ||
# tell me everything about "tarek" (same as above) | ||
sl(g[self.tarek::],2) | ||
|
||
# give me every "likes" relationship | ||
sl(g[:self.likes:],5) | ||
|
||
# give me every relationship to pizza | ||
sl(g[::self.pizza],3) | ||
|
||
# give me everyone who likes pizza | ||
sl(g[:self.likes:self.pizza],2) | ||
|
||
# does tarek like pizza? | ||
sl(g[self.tarek:self.likes:self.pizza],1) | ||
|
||
# More intesting is using paths | ||
|
||
# everything hated or liked | ||
sl(g[:self.hates|self.likes], 7) | ||
|
||
|
||
|
||
|
||
|
||
def setUp(self): | ||
self.graph = Graph() | ||
|
||
self.michel = URIRef(u'michel') | ||
self.tarek = URIRef(u'tarek') | ||
self.bob = URIRef(u'bob') | ||
self.likes = URIRef(u'likes') | ||
self.hates = URIRef(u'hates') | ||
self.pizza = URIRef(u'pizza') | ||
self.cheese = URIRef(u'cheese') | ||
|
||
self.addStuff() | ||
|
||
def addStuff(self): | ||
tarek = self.tarek | ||
michel = self.michel | ||
bob = self.bob | ||
likes = self.likes | ||
hates = self.hates | ||
pizza = self.pizza | ||
cheese = self.cheese | ||
|
||
self.graph.add((tarek, likes, pizza)) | ||
self.graph.add((tarek, likes, cheese)) | ||
self.graph.add((michel, likes, pizza)) | ||
self.graph.add((michel, likes, cheese)) | ||
self.graph.add((bob, likes, cheese)) | ||
self.graph.add((bob, hates, pizza)) | ||
self.graph.add((bob, hates, michel)) # gasp! | ||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |