diff --git a/.pep8 b/.pep8 index 7047ac005..acc7b2d34 100644 --- a/.pep8 +++ b/.pep8 @@ -1,4 +1,4 @@ [pep8] ignore = W806 max-line-length = 85 -exclude = pyRdfa,host,extras,transform,rdfs,pyMicrodata +exclude = host,extras,transform,rdfs diff --git a/examples/conjunctive_graphs.py b/examples/conjunctive_graphs.py index a5e5a71f7..81cbab9ef 100644 --- a/examples/conjunctive_graphs.py +++ b/examples/conjunctive_graphs.py @@ -14,21 +14,20 @@ from rdflib.graph import Graph, ConjunctiveGraph from rdflib.plugins.memory import IOMemory -if __name__=='__main__': - +if __name__ == '__main__': ns = Namespace("http://love.com#") mary = URIRef("http://love.com/lovers/mary#") john = URIRef("http://love.com/lovers/john#") - cmary=URIRef("http://love.com/lovers/mary#") - cjohn=URIRef("http://love.com/lovers/john#") + cmary = URIRef("http://love.com/lovers/mary#") + cjohn = URIRef("http://love.com/lovers/john#") store = IOMemory() g = ConjunctiveGraph(store=store) - g.bind("love",ns) + g.bind("love", ns) gmary = Graph(store=store, identifier=cmary) @@ -38,21 +37,21 @@ gjohn = Graph(store=store, identifier=cjohn) gjohn.add((john, ns['hasName'], Literal("John"))) - #enumerate contexts + # enumerate contexts for c in g.contexts(): print("-- %s " % c) - #separate graphs + # separate graphs print(gjohn.serialize(format='n3')) print("===================") print(gmary.serialize(format='n3')) print("===================") - #full graph + # full graph print(g.serialize(format='n3')) # query the conjunction of all graphs print('Mary loves:') - for x in g[mary : ns.loves/ns.hasName]: + for x in g[mary: ns.loves / ns.hasName]: print(x) diff --git a/examples/custom_datatype.py b/examples/custom_datatype.py index 34bde44cd..f2d4fb286 100644 --- a/examples/custom_datatype.py +++ b/examples/custom_datatype.py @@ -14,31 +14,31 @@ from rdflib import Graph, Literal, Namespace, XSD from rdflib.term import bind -if __name__=='__main__': +if __name__ == '__main__': # complex numbers are not registered by default # no custom constructor/serializer needed since # complex('(2+3j)') works fine bind(XSD.complexNumber, complex) - ns=Namespace("urn:my:namespace:") + ns = Namespace("urn:my:namespace:") - c=complex(2,3) + c = complex(2, 3) - l=Literal(c) + l = Literal(c) - g=Graph() + g = Graph() g.add((ns.mysubject, ns.myprop, l)) - n3=g.serialize(format='n3') + n3 = g.serialize(format='n3') # round-trip through n3 - g2=Graph() + g2 = Graph() g2.parse(data=n3, format='n3') - l2=list(g2)[0][2] + l2 = list(g2)[0][2] print(l2) - print(l2.value == c) # back to a python complex object + print(l2.value == c) # back to a python complex object diff --git a/examples/custom_eval.py b/examples/custom_eval.py index bad8ff1ce..69cd173a9 100644 --- a/examples/custom_eval.py +++ b/examples/custom_eval.py @@ -49,7 +49,8 @@ def customEval(ctx, part): raise NotImplementedError() -if __name__=='__main__': + +if __name__ == '__main__': # add function directly, normally we would use setuptools and entry_points rdflib.plugins.sparql.CUSTOM_EVALS['exampleEval'] = customEval diff --git a/examples/film.py b/examples/film.py index 0672840ac..6bbda04a0 100644 --- a/examples/film.py +++ b/examples/film.py @@ -23,7 +23,11 @@ film.py http://www.imdb.com/title/tt0105236/ Review the movie "Reservoir Dogs" """ -import datetime, os, sys, re, time +import datetime +import os +import sys +import re +import time try: import imdb @@ -36,7 +40,7 @@ storefn = os.path.expanduser('~/movies.n3') #storefn = '/home/simon/codes/film.dev/movies.n3' -storeuri = 'file://'+storefn +storeuri = 'file://' + storefn title = 'Movies viewed by %s' r_who = re.compile('^(.*?) <([a-z0-9_-]+(\.[a-z0-9_-]+)*@[a-z0-9_-]+(\.[a-z0-9_-]+)+)>$') @@ -44,6 +48,7 @@ IMDB = Namespace('http://www.csd.abdn.ac.uk/~ggrimnes/dev/imdb/IMDB#') REV = Namespace('http://purl.org/stuff/rev#') + class Store: def __init__(self): self.graph = ConjunctiveGraph() @@ -61,12 +66,14 @@ def who(self, who=None): if who is not None: name, email = (r_who.match(who).group(1), r_who.match(who).group(2)) self.graph.add((URIRef(storeuri), DC['title'], Literal(title % name))) - self.graph.add((URIRef(storeuri+'#author'), RDF.type, FOAF['Person'])) - self.graph.add((URIRef(storeuri+'#author'), FOAF['name'], Literal(name))) - self.graph.add((URIRef(storeuri+'#author'), FOAF['mbox'], Literal(email))) + self.graph.add((URIRef(storeuri + '#author'), RDF.type, FOAF['Person'])) + self.graph.add((URIRef(storeuri + '#author'), + FOAF['name'], Literal(name))) + self.graph.add((URIRef(storeuri + '#author'), + FOAF['mbox'], Literal(email))) self.save() else: - return self.graph.objects(URIRef(storeuri+'#author'), FOAF['name']) + return self.graph.objects(URIRef(storeuri + '#author'), FOAF['name']) def new_movie(self, movie): movieuri = URIRef('http://www.imdb.com/title/tt%s/' % movie.movieID) @@ -76,14 +83,14 @@ def new_movie(self, movie): self.save() def new_review(self, movie, date, rating, comment=None): - review = BNode() # @@ humanize the identifier (something like #rev-$date) + review = BNode() # @@ humanize the identifier (something like #rev-$date) movieuri = URIRef('http://www.imdb.com/title/tt%s/' % movie.movieID) self.graph.add((movieuri, REV['hasReview'], URIRef('%s#%s' % (storeuri, review)))) self.graph.add((review, RDF.type, REV['Review'])) self.graph.add((review, DC['date'], Literal(date))) self.graph.add((review, REV['maxRating'], Literal(5))) self.graph.add((review, REV['minRating'], Literal(0))) - self.graph.add((review, REV['reviewer'], URIRef(storeuri+'#author'))) + self.graph.add((review, REV['reviewer'], URIRef(storeuri + '#author'))) self.graph.add((review, REV['rating'], Literal(rating))) if comment is not None: self.graph.add((review, REV['text'], Literal(comment))) @@ -92,9 +99,11 @@ def new_review(self, movie, date, rating, comment=None): def movie_is_in(self, uri): return (URIRef(uri), RDF.type, IMDB['Movie']) in self.graph + def help(): print(__doc__.split('--')[1]) + def main(argv=None): if not argv: argv = sys.argv @@ -136,6 +145,7 @@ def main(argv=None): else: help() + if __name__ == '__main__': if not imdb: raise Exception('This example requires the IMDB library! Install with "pip install imdbpy"') diff --git a/examples/foafpaths.py b/examples/foafpaths.py index 9209500be..4650d228a 100644 --- a/examples/foafpaths.py +++ b/examples/foafpaths.py @@ -31,7 +31,7 @@ from rdflib import URIRef, Graph from rdflib.namespace import FOAF -if __name__=='__main__': +if __name__ == '__main__': g = Graph() g.load("foaf.rdf") diff --git a/examples/graph_digest_benchmark.py b/examples/graph_digest_benchmark.py index 2e64e1bf2..678425d1e 100644 --- a/examples/graph_digest_benchmark.py +++ b/examples/graph_digest_benchmark.py @@ -12,7 +12,8 @@ from rdflib.compare import to_isomorphic from six.moves.urllib.request import urlopen from six.moves import queue -import sys, csv +import sys +import csv from io import StringIO from collections import defaultdict @@ -49,7 +50,7 @@ 'to_hash_runtime', 'canonicalize_triples_runtime', 'error', - ] +] def files_benchmark(ontologies, output_file, threads): @@ -160,6 +161,7 @@ def worker(q, finished_tasks, dl_lock): w.flush() written_tasks += 1 + if __name__ == '__main__': if len(sys.argv) > 4: files_benchmark(sys.argv[1:-2], sys.argv[-2], sys.argv[-1]) diff --git a/examples/prepared_query.py b/examples/prepared_query.py index 2906783fb..eb5f2b8cb 100644 --- a/examples/prepared_query.py +++ b/examples/prepared_query.py @@ -14,11 +14,11 @@ from rdflib.plugins.sparql import prepareQuery from rdflib.namespace import FOAF -if __name__=='__main__': +if __name__ == '__main__': q = prepareQuery( 'SELECT ?s WHERE { ?person foaf:knows ?s .}', - initNs = { "foaf": FOAF }) + initNs={"foaf": FOAF}) g = rdflib.Graph() g.load("foaf.rdf") diff --git a/examples/rdfa_example.py b/examples/rdfa_example.py index 000644ec7..e49bebbc6 100644 --- a/examples/rdfa_example.py +++ b/examples/rdfa_example.py @@ -19,4 +19,4 @@ schema:name ?title ] FILTER (LANG(?title) = 'en') } """): - print("%s by %s"%(row.title, row.author)) + print("%s by %s" % (row.title, row.author)) diff --git a/examples/resource.py b/examples/resource.py index d254e751c..d676ae5fd 100644 --- a/examples/resource.py +++ b/examples/resource.py @@ -12,19 +12,18 @@ from rdflib import Graph, RDF, RDFS, Literal from rdflib.namespace import FOAF -if __name__=='__main__': +if __name__ == '__main__': g = Graph() bob = g.resource('urn:bob') - bob.set(RDF.type, FOAF.Person) # .set replaces all other values + bob.set(RDF.type, FOAF.Person) # .set replaces all other values bob.set(FOAF.name, Literal("Bob")) - bill = g.resource('urn:bill') - bill.add(RDF.type, FOAF.Person) # add adds to existing values + bill.add(RDF.type, FOAF.Person) # add adds to existing values bill.add(RDF.type, FOAF.Agent) bill.set(RDFS.label, Literal("Bill")) @@ -42,8 +41,8 @@ # or even quicker with paths: print("Bill knows: ") - for friend in bill[FOAF.knows/FOAF.name]: + for friend in bill[FOAF.knows / FOAF.name]: print(friend) # setting single properties is also possible: - bill[RDFS.label]=Literal("William") + bill[RDFS.label] = Literal("William") diff --git a/examples/simple_example.py b/examples/simple_example.py index 5ed4be62b..ef5ec73ab 100644 --- a/examples/simple_example.py +++ b/examples/simple_example.py @@ -2,7 +2,7 @@ from rdflib import Graph, Literal, BNode, RDF from rdflib.namespace import FOAF, DC -if __name__=='__main__': +if __name__ == '__main__': store = Graph() diff --git a/examples/sleepycat_example.py b/examples/sleepycat_example.py index 417897fac..b112717ee 100644 --- a/examples/sleepycat_example.py +++ b/examples/sleepycat_example.py @@ -47,7 +47,7 @@ graph = ConjunctiveGraph('Sleepycat') - graph.open(path, create = False) + graph.open(path, create=False) print('Triples still in graph: ', len(graph)) @@ -56,5 +56,5 @@ # Clean up the temp folder to remove the Sleepycat database files... import os for f in os.listdir(path): - os.unlink(path+'/'+f) + os.unlink(path + '/' + f) os.rmdir(path) diff --git a/examples/slice.py b/examples/slice.py index e3d21b408..525edb95f 100644 --- a/examples/slice.py +++ b/examples/slice.py @@ -14,16 +14,16 @@ from rdflib import Graph, RDF from rdflib.namespace import FOAF -if __name__=='__main__': +if __name__ == '__main__': graph = Graph() graph.load("foaf.rdf") - for person in graph[: RDF.type : FOAF.Person]: + for person in graph[: RDF.type: FOAF.Person]: - friends = list(graph[person:FOAF.knows * '+'/FOAF.name]) + friends = list(graph[person:FOAF.knows * '+' / FOAF.name]) if friends: - print("%s's circle of friends:"%graph.value(person, FOAF.name)) + print("%s's circle of friends:" % graph.value(person, FOAF.name)) for name in friends: print(name) diff --git a/examples/smushing.py b/examples/smushing.py index fbd5dd250..3b174f83e 100644 --- a/examples/smushing.py +++ b/examples/smushing.py @@ -27,21 +27,20 @@ STABLE = Namespace("http://example.com/person/mbox_sha1sum/") -if __name__=='__main__': +if __name__ == '__main__': g = Graph() g.parse("smushingdemo.n3", format="n3") - newURI = {} # old subject : stable uri - for s,p,o in g.triples((None, FOAF['mbox_sha1sum'], None)): + newURI = {} # old subject : stable uri + for s, p, o in g.triples((None, FOAF['mbox_sha1sum'], None)): newURI[s] = STABLE[o] - out = Graph() out.bind('foaf', FOAF) - for s,p,o in g: + for s, p, o in g: s = newURI.get(s, s) - o = newURI.get(o, o) # might be linked to another person - out.add((s,p,o)) + o = newURI.get(o, o) # might be linked to another person + out.add((s, p, o)) print(out.serialize(format="n3").decode('utf-8')) diff --git a/examples/sparql_query_example.py b/examples/sparql_query_example.py index 1698c706b..b1ffff5f6 100644 --- a/examples/sparql_query_example.py +++ b/examples/sparql_query_example.py @@ -16,7 +16,7 @@ import rdflib -if __name__=='__main__': +if __name__ == '__main__': g = rdflib.Graph() g.load("foaf.rdf") diff --git a/examples/sparql_update_example.py b/examples/sparql_update_example.py index 991561355..a604eebd4 100644 --- a/examples/sparql_update_example.py +++ b/examples/sparql_update_example.py @@ -7,7 +7,7 @@ import rdflib -if __name__=='__main__': +if __name__ == '__main__': g = rdflib.Graph() g.load("foaf.rdf") diff --git a/examples/sparqlstore_example.py b/examples/sparqlstore_example.py index 529ee233e..afef011d9 100644 --- a/examples/sparqlstore_example.py +++ b/examples/sparqlstore_example.py @@ -14,7 +14,6 @@ graph.open("http://dbpedia.org/sparql") - pop = graph.value( URIRef("http://dbpedia.org/resource/Berlin"), dbo.populationTotal) diff --git a/examples/swap_primer.py b/examples/swap_primer.py index e44c34f28..dacb92e44 100644 --- a/examples/swap_primer.py +++ b/examples/swap_primer.py @@ -12,7 +12,7 @@ from rdflib import ConjunctiveGraph, Namespace, Literal from rdflib.namespace import OWL, DC -if __name__=='__main__': +if __name__ == '__main__': # Firstly, it doesn't have to be so complex. # Here we create a "Graph" of our work. @@ -25,7 +25,6 @@ # or: primer.add((myNS['pat'], myNS['age'], Literal(24))) - # Now, with just that, lets see how the system # recorded *way* too many details about what # you just asserted as fact. @@ -34,7 +33,6 @@ from pprint import pprint pprint(list(primer)) - # just think .whatever((s, p, o)) # here we report on what we know @@ -50,16 +48,12 @@ # who is what age? pprint(list(primer.subject_objects(myNS.age))) - - # Okay, so lets now work with a bigger # dataset from the example, and start # with a fresh new graph. - primer = ConjunctiveGraph() - # Lets start with a verbatim string straight from the primer text: mySource = """ @@ -101,7 +95,7 @@ - """ # --- End of primer code + """ # --- End of primer code # To make this go easier to spit back out... # technically, we already created a namespace @@ -119,7 +113,6 @@ primer.parse(data=mySource, format='n3') - # Now you can query, either directly straight into a list: [(x, y, z) for x, y, z in primer] diff --git a/examples/transitive.py b/examples/transitive.py index dad3a07ba..5251ea791 100644 --- a/examples/transitive.py +++ b/examples/transitive.py @@ -45,7 +45,7 @@ """ -if __name__=='__main__': +if __name__ == '__main__': from rdflib import ConjunctiveGraph, URIRef person = URIRef('ex:person') diff --git a/rdflib/collection.py b/rdflib/collection.py index 8338a24c9..c99aa4c75 100644 --- a/rdflib/collection.py +++ b/rdflib/collection.py @@ -232,7 +232,6 @@ def __iadd__(self, other): self.graph.add((end, RDF.first, item)) - self.graph.add((end, RDF.rest, RDF.nil)) def clear(self): @@ -249,6 +248,7 @@ def test(): import doctest doctest.testmod() + if __name__ == "__main__": test() diff --git a/rdflib/compare.py b/rdflib/compare.py index 97de047b1..d28b33368 100644 --- a/rdflib/compare.py +++ b/rdflib/compare.py @@ -195,9 +195,9 @@ def __init__(self, nodes, hashfunc, color=(), hash_cache=None): self._hash_color = None def __str__(self): - nodes, color = self.key() + nodes, color = self.key() return "Color %s (%s nodes)" % (color, nodes) - + def key(self): return (len(self.nodes), self.hash_color()) @@ -264,11 +264,9 @@ def _hashfunc(s): self._hash_cache = {} self.hashfunc = _hashfunc - def _discrete(self, coloring): return len([c for c in coloring if not c.discrete()]) == 0 - def _initial_color(self): """Finds an initial color for the graph. @@ -494,6 +492,7 @@ def to_isomorphic(graph): result += graph return result + def isomorphic(graph1, graph2): """Compare graph for equality. @@ -530,7 +529,6 @@ def isomorphic(graph1, graph2): return gd1 == gd2 - def to_canonical_graph(g1, stats=None): """Creates a canonical, read-only graph. @@ -553,7 +551,6 @@ def graph_diff(g1, g2): return (in_both, in_first, in_second) - _MOCK_BNODE = BNode() diff --git a/rdflib/compat.py b/rdflib/compat.py index c82f426f1..2c5e4cf4c 100644 --- a/rdflib/compat.py +++ b/rdflib/compat.py @@ -15,25 +15,26 @@ # clean ElementTree import try: - from lxml import etree + from lxml import etree except ImportError: - try: - # Python 2.5 - import xml.etree.cElementTree as etree - except ImportError: try: - # Python 2.5 - import xml.etree.ElementTree as etree + # Python 2.5 + import xml.etree.cElementTree as etree except ImportError: - try: - # normal cElementTree install - import cElementTree as etree - except ImportError: try: - # normal ElementTree install - import elementtree.ElementTree as etree + # Python 2.5 + import xml.etree.ElementTree as etree except ImportError: - raise Exception("Failed to import ElementTree from any known place") + try: + # normal cElementTree install + import cElementTree as etree + except ImportError: + try: + # normal ElementTree install + import elementtree.ElementTree as etree + except ImportError: + raise Exception( + "Failed to import ElementTree from any known place") try: etree_register_namespace = etree.register_namespace @@ -44,11 +45,13 @@ def etree_register_namespace(prefix, uri): etreenative._namespace_map[uri] = prefix + def cast_bytes(s, enc='utf-8'): if isinstance(s, six.text_type): return s.encode(enc) return s + if six.PY3: # Python 3: # --------- @@ -57,7 +60,7 @@ def ascii(stream): return codecs.getreader('ascii')(stream) def bopen(*args, **kwargs): - return open(*args, mode = 'rb', **kwargs) + return open(*args, mode='rb', **kwargs) long_type = int @@ -84,9 +87,11 @@ def sign(n): r_unicodeEscape = re.compile(r'(\\u[0-9A-Fa-f]{4}|\\U[0-9A-Fa-f]{8})') + def _unicodeExpand(s): return r_unicodeEscape.sub(lambda m: six.unichr(int(m.group(0)[2:], 16)), s) + narrow_build = False try: six.unichr(0x10FFFF) @@ -108,7 +113,6 @@ def _unicodeExpand(s): def decodeStringEscape(s): - """ s is byte-string - replace \ escapes in string """ @@ -126,7 +130,8 @@ def decodeStringEscape(s): s = s.replace('\\\\', '\\') return s - #return _unicodeExpand(s) # hmm - string escape doesn't do unicode escaping + # return _unicodeExpand(s) # hmm - string escape doesn't do unicode escaping + def decodeUnicodeEscape(s): """ @@ -146,6 +151,6 @@ def decodeUnicodeEscape(s): s = s.replace("\\'", "'") s = s.replace('\\\\', '\\') - s = _unicodeExpand(s) # hmm - string escape doesn't do unicode escaping + s = _unicodeExpand(s) # hmm - string escape doesn't do unicode escaping return s diff --git a/rdflib/events.py b/rdflib/events.py index 3deebbb2a..2c563c106 100644 --- a/rdflib/events.py +++ b/rdflib/events.py @@ -92,5 +92,6 @@ def test(): import doctest doctest.testmod() + if __name__ == '__main__': test() diff --git a/rdflib/exceptions.py b/rdflib/exceptions.py index bbfdb594a..85195a530 100644 --- a/rdflib/exceptions.py +++ b/rdflib/exceptions.py @@ -9,6 +9,7 @@ class Error(Exception): """Base class for rdflib exceptions.""" + def __init__(self, msg=None): Exception.__init__(self, msg) self.msg = msg @@ -25,6 +26,7 @@ def __init__(self, node): class SubjectTypeError(TypeCheckError): """Subject of an assertion must be an instance of URIRef.""" + def __init__(self, node): TypeCheckError.__init__(self, node) self.msg = "Subject must be instance of URIRef or BNode: %s(%s)" \ @@ -33,6 +35,7 @@ def __init__(self, node): class PredicateTypeError(TypeCheckError): """Predicate of an assertion must be an instance of URIRef.""" + def __init__(self, node): TypeCheckError.__init__(self, node) self.msg = "Predicate must be a URIRef instance: %s(%s)" \ @@ -42,6 +45,7 @@ def __init__(self, node): class ObjectTypeError(TypeCheckError): """Object of an assertion must be an instance of URIRef, Literal, or BNode.""" + def __init__(self, node): TypeCheckError.__init__(self, node) self.msg = "\ @@ -51,6 +55,7 @@ def __init__(self, node): class ContextTypeError(TypeCheckError): """Context of an assertion must be an instance of URIRef.""" + def __init__(self, node): TypeCheckError.__init__(self, node) self.msg = "Context must be instance of URIRef or BNode: %s(%s)" \ @@ -59,6 +64,7 @@ def __init__(self, node): class ParserError(Error): """RDF Parser error.""" + def __init__(self, msg): Error.__init__(self, msg) self.msg = msg @@ -69,6 +75,7 @@ def __str__(self): class UniquenessError(Error): """A uniqueness assumption was made in the context, and that is not true""" + def __init__(self, values): Error.__init__(self, "\ Uniqueness assumption is not fulfilled. Multiple values are: %s" % values) diff --git a/rdflib/extras/describer.py b/rdflib/extras/describer.py index 105da668c..68e85240a 100644 --- a/rdflib/extras/describer.py +++ b/rdflib/extras/describer.py @@ -128,7 +128,6 @@ def __init__(self, graph=None, about=None, base=None): self._subjects = [] self.about(about or None) - def about(self, subject, **kws): """ Sets the current subject. Will convert the given object into an @@ -151,7 +150,6 @@ def about(self, subject, **kws): else: self._subjects.append(subject) - def value(self, p, v, **kws): """ Set a literal value for the given property. Will cast the value to an @@ -170,7 +168,6 @@ def value(self, p, v, **kws): v = cast_value(v, **kws) self.graph.add((self._current(), p, v)) - def rel(self, p, o=None, **kws): """Set an object for the given property. Will convert the given object into an ``URIRef`` if it's not an ``Identifier``. If none is given, a @@ -204,7 +201,6 @@ def rel(self, p, o=None, **kws): self.graph.add((self._current(), p, o)) return self._subject_stack(o) - def rev(self, p, s=None, **kws): """ Same as ``rel``, but uses current subject as *object* of the relation. diff --git a/rdflib/extras/external_graph_libs.py b/rdflib/extras/external_graph_libs.py index 2bb62a188..8617b370f 100644 --- a/rdflib/extras/external_graph_libs.py +++ b/rdflib/extras/external_graph_libs.py @@ -19,7 +19,9 @@ import logging logger = logging.getLogger(__name__) -_identity = lambda x: x + +def _identity(x): return x + def _rdflib_to_networkx_graph( graph, @@ -65,6 +67,7 @@ def _rdflib_to_networkx_graph( d = edge_attrs(s, p, o) data['triples'].extend(d['triples']) + def rdflib_to_networkx_multidigraph( graph, edge_attrs=lambda s, p, o: {'key': p}, @@ -117,6 +120,7 @@ def rdflib_to_networkx_multidigraph( _rdflib_to_networkx_graph(graph, mdg, False, edge_attrs, **kwds) return mdg + def rdflib_to_networkx_digraph( graph, calc_weights=True, @@ -238,13 +242,13 @@ def rdflib_to_networkx_graph( def rdflib_to_graphtool( - graph, - v_prop_names=[str('term')], - e_prop_names=[str('term')], - transform_s=lambda s, p, o: {str('term'): s}, - transform_p=lambda s, p, o: {str('term'): p}, - transform_o=lambda s, p, o: {str('term'): o}, - ): + graph, + v_prop_names=[str('term')], + e_prop_names=[str('term')], + transform_s=lambda s, p, o: {str('term'): s}, + transform_p=lambda s, p, o: {str('term'): p}, + transform_o=lambda s, p, o: {str('term'): o}, +): """Converts the given graph into a graph_tool.Graph(). The subjects and objects are the later vertices of the Graph. diff --git a/rdflib/extras/infixowl.py b/rdflib/extras/infixowl.py index 8f0a439d0..f3ce40d3a 100644 --- a/rdflib/extras/infixowl.py +++ b/rdflib/extras/infixowl.py @@ -214,6 +214,7 @@ def __rshift__(self, other): def __call__(self, value1, value2): return self.function(value1, value2) + OWL_NS = Namespace("http://www.w3.org/2002/07/owl#") nsBinds = { @@ -515,6 +516,7 @@ class AnnotatableTerms(Individual): """ Terms in an OWL ontology with rdfs:label and rdfs:comment """ + def __init__(self, identifier, graph=None, @@ -633,6 +635,7 @@ def _delete_label(self): class Ontology(AnnotatableTerms): """ The owl ontology metadata""" + def __init__(self, identifier=None, imports=None, comment=None, graph=None): super(Ontology, self).__init__(identifier, graph) @@ -707,6 +710,7 @@ def __getattr__(self, name): else: return self.term(name) + CLASS_RELATIONS = set( OWL_NS.resourceProperties ).difference([OWL_NS.onProperty, @@ -877,8 +881,8 @@ def CastClass(c, graph=None): else: for s, p, o in graph.triples_choices((classOrIdentifier(c), [OWL_NS.intersectionOf, - OWL_NS.unionOf, - OWL_NS.oneOf], + OWL_NS.unionOf, + OWL_NS.oneOf], None)): if p == OWL_NS.oneOf: return EnumeratedClass(classOrIdentifier(c), graph=graph) @@ -919,6 +923,7 @@ class Class(AnnotatableTerms): description." """ + def _serialize(self, graph): for cl in self.subClassOf: CastClass(cl, self.graph).serialize(graph) @@ -1102,7 +1107,7 @@ def _set_equivalentClass(self, other): return for sc in other: self.graph.add((self.identifier, - OWL_NS.equivalentClass, classOrIdentifier(sc))) + OWL_NS.equivalentClass, classOrIdentifier(sc))) @TermDeletionHelper(OWL_NS.equivalentClass) def _del_equivalentClass(self): @@ -1259,8 +1264,8 @@ def __repr__(self, full=False, normalization=True): else: scJoin = ', ' necStatements = [ - isinstance(s, Class) and isinstance(self.identifier, BNode) and - repr(CastClass(s, self.graph)) or + isinstance(s, Class) and isinstance(self.identifier, BNode) + and repr(CastClass(s, self.graph)) or # repr(BooleanClass(classOrIdentifier(s), # operator=None, # graph=self.graph)) or @@ -1273,8 +1278,8 @@ def __repr__(self, full=False, normalization=True): exprs[-1] = "\n " + exprs[-1] if ec: nec_SuffStatements = [ - isinstance(s, str) and s or - manchesterSyntax(classOrIdentifier(s), self.graph) for s in ec] + isinstance(s, str) and s + or manchesterSyntax(classOrIdentifier(s), self.graph) for s in ec] if nec_SuffStatements: klassKind = "A Defined Class %s" % label exprs.append("EquivalentTo: %s" % ', '.join(nec_SuffStatements)) @@ -1294,9 +1299,9 @@ def __repr__(self, full=False, normalization=True): else: klassDescr = full and (descr and "\n %s" % descr[0] or '') or '' + ' . '.join(exprs) - return (isinstance(self.identifier, BNode) - and "Some Class " - or "Class: %s " % self.qname) + klassDescr + return (isinstance(self.identifier, BNode) and + "Some Class " or + "Class: %s " % self.qname) + klassDescr class OWLRDFListProxy(object): @@ -1438,6 +1443,7 @@ def serialize(self, graph): graph.add((s, p, o)) self._serialize(graph) + BooleanPredicates = [OWL_NS.intersectionOf, OWL_NS.unionOf] @@ -1461,6 +1467,7 @@ class BooleanClassExtentHelper: ... print(c) #doctest: +SKIP ( ex:Fire OR ex:Water ) """ + def __init__(self, operator): self.operator = operator @@ -1501,8 +1508,8 @@ def __init__(self, identifier=None, operator=OWL_NS.intersectionOf, props = [] for s, p, o in graph.triples_choices((identifier, [OWL_NS.intersectionOf, - OWL_NS.unionOf], - None)): + OWL_NS.unionOf], + None)): props.append(p) operator = p assert len(props) == 1, repr(props) @@ -1629,7 +1636,7 @@ def __init__(self, OWL_NS.onProperty, propertyOrIdentifier(onProperty)) not in graph: graph.add((self.identifier, OWL_NS.onProperty, - propertyOrIdentifier(onProperty))) + propertyOrIdentifier(onProperty))) self.onProperty = onProperty restrTypes = [ (allValuesFrom, OWL_NS.allValuesFrom), @@ -1659,7 +1666,6 @@ def __init__(self, self.graph.add((self.identifier, RDF.type, OWL_NS.Restriction)) self.graph.remove((self.identifier, RDF.type, OWL_NS.Class)) - def serialize(self, graph): """ >>> g1 = Graph() @@ -1880,6 +1886,7 @@ def __repr__(self): ### Infix Operators ### + some = Infix(lambda prop, _class: Restriction(prop, graph=_class.graph, someValuesFrom=_class)) only = Infix(lambda prop, _class: Restriction(prop, graph=_class.graph, @@ -1989,8 +1996,8 @@ def __repr__(self): rt = [] if OWL_NS.ObjectProperty in self.type: rt.append('ObjectProperty( %s annotation(%s)' - % (self.qname, first(self.comment) - and first(self.comment) or '')) + % (self.qname, first(self.comment) and + first(self.comment) or '')) if first(self.inverseOf): twoLinkInverse = first(first(self.inverseOf).inverseOf) if twoLinkInverse \ @@ -2000,9 +2007,9 @@ def __repr__(self): inverseRepr = repr(first(self.inverseOf)) rt.append(" inverseOf( %s )%s" % ( inverseRepr, - OWL_NS.SymmetricProperty in self.type - and ' Symmetric' - or '')) + OWL_NS.SymmetricProperty in self.type and + ' Symmetric' or + '')) for s, p, roleType in self.graph.triples_choices( (self.identifier, RDF.type, @@ -2012,9 +2019,9 @@ def __repr__(self): rt.append(str(roleType.split(OWL_NS)[-1])) else: rt.append('DatatypeProperty( %s %s' - % (self.qname, first(self.comment) - and first(self.comment) - or '')) + % (self.qname, first(self.comment) and + first(self.comment) or + '')) for s, p, roleType in self.graph.triples(( self.identifier, RDF.type, OWL_NS.FunctionalProperty)): rt.append(' Functional') @@ -2028,7 +2035,7 @@ def canonicalName(term, g): elif first(g.triples_choices(( normalizedName, [OWL_NS.unionOf, - OWL_NS.intersectionOf], None))): + OWL_NS.intersectionOf], None))): return repr(term) else: return str(term.qname) @@ -2149,5 +2156,6 @@ def test(): import doctest doctest.testmod() + if __name__ == '__main__': test() diff --git a/rdflib/graph.py b/rdflib/graph.py index 363629132..b7092bb9b 100644 --- a/rdflib/graph.py +++ b/rdflib/graph.py @@ -395,9 +395,9 @@ def addN(self, quads): """Add a sequence of triple with context""" self.__store.addN((s, p, o, c) for s, p, o, c in quads - if isinstance(c, Graph) - and c.identifier is self.identifier - and _assertnode(s,p,o) + if isinstance(c, Graph) and + c.identifier is self.identifier and + _assertnode(s, p, o) ) def remove(self, triple): @@ -422,7 +422,6 @@ def triples(self, triple): for (s, p, o), cg in self.__store.triples((s, p, o), context=self): yield (s, p, o) - def __getitem__(self, item): """ A graph can be "sliced" as a shortcut for the triples method @@ -466,9 +465,9 @@ def __getitem__(self, item): if isinstance(item, slice): - s,p,o=item.start,item.stop,item.step + s, p, o = item.start, item.stop, item.step if s is None and p is None and o is None: - return self.triples((s,p,o)) + return self.triples((s, p, o)) elif s is None and p is None: return self.subject_predicates(o) elif s is None and o is None: @@ -476,16 +475,16 @@ def __getitem__(self, item): elif p is None and o is None: return self.predicate_objects(s) elif s is None: - return self.subjects(p,o) + return self.subjects(p, o) elif p is None: - return self.predicates(s,o) + return self.predicates(s, o) elif o is None: - return self.objects(s,p) + return self.objects(s, p) else: # all given - return (s,p,o) in self + return (s, p, o) in self - elif isinstance(item, (Path,Node)): + elif isinstance(item, (Path, Node)): return self.predicate_objects(item) @@ -530,15 +529,15 @@ def __eq__(self, other): def __lt__(self, other): return (other is None) \ - or (isinstance(other, Graph) - and self.identifier < other.identifier) + or (isinstance(other, Graph) and + self.identifier < other.identifier) def __le__(self, other): return self < other or self == other def __gt__(self, other): - return (isinstance(other, Graph) - and self.identifier > other.identifier) \ + return (isinstance(other, Graph) and + self.identifier > other.identifier) \ or (other is not None) def __ge__(self, other): @@ -708,7 +707,6 @@ def label(self, subject, default=''): return default return self.value(subject, RDFS.label, default=default, any=True) - def preferredLabel(self, subject, lang=None, default=None, labelProperties=(SKOS.prefLabel, RDFS.label)): """ @@ -758,11 +756,11 @@ def preferredLabel(self, subject, lang=None, default=None, # setup the language filtering if lang is not None: if lang == '': # we only want not language-tagged literals - langfilter = lambda l: l.language is None + def langfilter(l): return l.language is None else: - langfilter = lambda l: l.language == lang + def langfilter(l): return l.language == lang else: # we don't care about language tags - langfilter = lambda l: True + def langfilter(l): return True for labelProp in labelProperties: labels = list(filter(langfilter, self.objects(subject, labelProp))) @@ -1062,9 +1060,9 @@ def query(self, query_object, processor='sparql', try: return self.store.query( query_object, initNs, initBindings, - self.default_union - and '__UNION__' - or self.identifier, + self.default_union and + '__UNION__' or + self.identifier, **kwargs) except NotImplementedError: pass # store has no own implementation @@ -1078,8 +1076,8 @@ def query(self, query_object, processor='sparql', query_object, initBindings, initNs, **kwargs)) def update(self, update_object, processor='sparql', - initNs=None, initBindings=None, - use_store_provided=True, **kwargs): + initNs=None, initBindings=None, + use_store_provided=True, **kwargs): """Update this graph with the given update query.""" initBindings = initBindings or {} initNs = initNs or dict(self.namespaces()) @@ -1088,9 +1086,9 @@ def update(self, update_object, processor='sparql', try: return self.store.update( update_object, initNs, initBindings, - self.default_union - and '__UNION__' - or self.identifier, + self.default_union and + '__UNION__' or + self.identifier, **kwargs) except NotImplementedError: pass # store has no own implementation @@ -1100,7 +1098,6 @@ def update(self, update_object, processor='sparql', return processor.update(update_object, initBindings, initNs, **kwargs) - def n3(self): """return an n3 identifier for the Graph""" return "[%s]" % self.identifier.n3() @@ -1191,8 +1188,6 @@ def collection(self, identifier): return Collection(self, identifier) - - def resource(self, identifier): """Create a new ``Resource`` instance. @@ -1272,6 +1267,7 @@ def do_de_skolemize2(t): return retval + class ConjunctiveGraph(Graph): """ @@ -1294,7 +1290,7 @@ def __init__(self, store='default', identifier=None): assert self.store.context_aware, ("ConjunctiveGraph must be backed by" " a context aware store.") self.context_aware = True - self.default_union = True # Conjunctive! + self.default_union = True # Conjunctive! self.default_context = Graph(store=self.store, identifier=identifier or BNode()) @@ -1316,46 +1312,43 @@ def _spoc(self, triple_or_quad, default=False): elif len(triple_or_quad) == 4: (s, p, o, c) = triple_or_quad c = self._graph(c) - return s,p,o,c - + return s, p, o, c def __contains__(self, triple_or_quad): """Support for 'triple/quad in graph' syntax""" - s,p,o,c = self._spoc(triple_or_quad) - for t in self.triples((s,p,o), context=c): + s, p, o, c = self._spoc(triple_or_quad) + for t in self.triples((s, p, o), context=c): return True return False - def add(self, triple_or_quad): - """ Add a triple or quad to the store. if a triple is given it is added to the default context """ - s,p,o,c = self._spoc(triple_or_quad, default=True) + s, p, o, c = self._spoc(triple_or_quad, default=True) - _assertnode(s,p,o) + _assertnode(s, p, o) self.store.add((s, p, o), context=c, quoted=False) def _graph(self, c): - if c is None: return None + if c is None: + return None if not isinstance(c, Graph): return self.get_context(c) else: return c - def addN(self, quads): """Add a sequence of triples with context""" self.store.addN( (s, p, o, self._graph(c)) for s, p, o, c in quads if _assertnode(s, p, o) - ) + ) def remove(self, triple_or_quad): """ @@ -1366,7 +1359,7 @@ def remove(self, triple_or_quad): a quad is removed from the given context only """ - s,p,o,c = self._spoc(triple_or_quad) + s, p, o, c = self._spoc(triple_or_quad) self.store.remove((s, p, o), context=c) @@ -1379,11 +1372,11 @@ def triples(self, triple_or_quad, context=None): keyword parameter. The kw param takes precedence. """ - s,p,o,c = self._spoc(triple_or_quad) + s, p, o, c = self._spoc(triple_or_quad) context = self._graph(context or c) if self.default_union: - if context==self.default_context: + if context == self.default_context: context = None else: if context is None: @@ -1402,7 +1395,7 @@ def triples(self, triple_or_quad, context=None): def quads(self, triple_or_quad=None): """Iterate over all the quads in the entire conjunctive graph""" - s,p,o,c = self._spoc(triple_or_quad) + s, p, o, c = self._spoc(triple_or_quad) for (s, p, o), cg in self.store.triples((s, p, o), context=c): for ctx in cg: @@ -1413,7 +1406,7 @@ def triples_choices(self, triple, context=None): s, p, o = triple if context is None: if not self.default_union: - context=self.default_context + context = self.default_context else: context = self._graph(context) @@ -1481,7 +1474,7 @@ def parse(self, source=None, publicID=None, format="xml", g_id = URIRef(g_id) context = Graph(store=self.store, identifier=g_id) - context.remove((None, None, None)) # hmm ? + context.remove((None, None, None)) # hmm ? context.parse(source, publicID=publicID, format=format, **args) return context @@ -1489,9 +1482,9 @@ def __reduce__(self): return (ConjunctiveGraph, (self.store, self.identifier)) - DATASET_DEFAULT_GRAPH_ID = URIRef('urn:x-rdflib:default') + class Dataset(ConjunctiveGraph): __doc__ = """ RDF 1.1 Dataset. Small extension to the Conjunctive Graph: @@ -1604,7 +1597,6 @@ def __init__(self, store='default', default_union=False): self.default_union = default_union - def __str__(self): pattern = ("[a rdflib:Dataset;rdflib:storage " "[a rdflib:Store;rdfs:label '%s']]") @@ -1655,11 +1647,12 @@ def contexts(self, triple=None): def quads(self, quad): for s, p, o, c in super(Dataset, self).quads(quad): - if c.identifier==self.default_context: + if c.identifier == self.default_context: yield (s, p, o, None) else: yield (s, p, o, c.identifier) + class QuotedGraph(Graph): """ Quoted Graphs are intended to implement Notation 3 formulae. They are @@ -1667,6 +1660,7 @@ class QuotedGraph(Graph): in order to maintain consistent formulae identification for scenarios such as implication and other such processing. """ + def __init__(self, store, identifier): super(QuotedGraph, self).__init__(store, identifier) @@ -1687,10 +1681,10 @@ def addN(self, quads): self.store.addN( (s, p, o, c) for s, p, o, c in quads - if isinstance(c, QuotedGraph) - and c.identifier is self.identifier - and _assertnode(s, p, o) - ) + if isinstance(c, QuotedGraph) and + c.identifier is self.identifier and + _assertnode(s, p, o) + ) def n3(self): """Return an n3 identifier for the Graph""" @@ -1711,7 +1705,7 @@ def __reduce__(self): # wrt to other Terms. # this must be done here, as the QuotedGraph cannot be # circularily imported in term.py -rdflib.term._ORDERING[QuotedGraph]=11 +rdflib.term._ORDERING[QuotedGraph] = 11 class Seq(object): @@ -1926,6 +1920,7 @@ def n3(self): def __reduce__(self): raise UnSupportedAggregateOperation() + def _assertnode(*terms): for t in terms: assert isinstance(t, Node), \ @@ -1937,5 +1932,6 @@ def test(): import doctest doctest.testmod() + if __name__ == '__main__': test() diff --git a/rdflib/namespace.py b/rdflib/namespace.py index 7ab00990b..5cdbfb807 100644 --- a/rdflib/namespace.py +++ b/rdflib/namespace.py @@ -198,6 +198,7 @@ class _RDFNamespace(ClosedNamespace): """ Closed namespace for RDF terms """ + def __init__(self): super(_RDFNamespace, self).__init__( URIRef("http://www.w3.org/1999/02/22-rdf-syntax-ns#"), @@ -284,6 +285,7 @@ class NamespaceManager(object): >>> """ + def __init__(self, graph): self.graph = graph self.__cache = {} @@ -357,7 +359,6 @@ def compute_qname(self, uri, generate=True): return self.__cache[uri] def bind(self, prefix, namespace, override=True, replace=False): - """bind a given namespace to the prefix if override, rebind, even if the given namespace is already diff --git a/rdflib/parser.py b/rdflib/parser.py index 5864820a8..9dc60f65e 100644 --- a/rdflib/parser.py +++ b/rdflib/parser.py @@ -117,7 +117,7 @@ def __init__(self, system_id=None, format=None): self.content_type = self.content_type.split(";", 1)[0] self.setByteStream(file) # TODO: self.setEncoding(encoding) - self.response_info = file.info() # a mimetools.Message instance + self.response_info = file.info() # a mimetools.Message instance def __repr__(self): return self.url @@ -191,7 +191,7 @@ def create_input_source(source=None, publicID=None, input_source = URLInputSource(absolute_location, format) auto_close = True # publicID = publicID or absolute_location # Further to fix - # for issue 130 + # for issue 130 if file is not None: input_source = FileInputSource(file) diff --git a/rdflib/paths.py b/rdflib/paths.py index ea6a8b38d..69147fbe4 100644 --- a/rdflib/paths.py +++ b/rdflib/paths.py @@ -308,7 +308,6 @@ def n3(self): return '|'.join(a.n3() for a in self.args) - class MulPath(Path): def __init__(self, path, mod): self.path = path @@ -413,7 +412,6 @@ def n3(self): return '%s%s' % (self.path.n3(), self.mod) - class NegatedPath(Path): def __init__(self, arg): if isinstance(arg, (URIRef, InvPath)): @@ -471,6 +469,7 @@ def path_sequence(self, other): def evalPath(graph, t): return ((s, o) for s, p, o in graph.triples(t)) + def mul_path(p, mul): """ cardinality path @@ -492,7 +491,6 @@ def neg_path(p): return NegatedPath(p) - if __name__ == '__main__': import doctest diff --git a/rdflib/plugin.py b/rdflib/plugin.py index 3ad1cafd6..33d6ae7a8 100644 --- a/rdflib/plugin.py +++ b/rdflib/plugin.py @@ -129,6 +129,7 @@ def plugins(name=None, kind=None): kind is None or kind == p.kind): yield p + register( 'default', Store, 'rdflib.plugins.memory', 'IOMemory') @@ -254,7 +255,6 @@ def plugins(name=None, kind=None): 'rdflib.plugins.parsers.trig', 'TrigParser') - register( 'sparql', Result, 'rdflib.plugins.sparql.processor', 'SPARQLResult') diff --git a/rdflib/plugins/memory.py b/rdflib/plugins/memory.py index f1cb5870d..7345311a9 100644 --- a/rdflib/plugins/memory.py +++ b/rdflib/plugins/memory.py @@ -2,6 +2,8 @@ from __future__ import division from __future__ import print_function +import random + from rdflib.term import BNode from rdflib.store import Store, NO_STORE, VALID_STORE from six import iteritems @@ -21,6 +23,7 @@ class Memory(Store): Authors: Michel Pelletier, Daniel Krech, Stefan Niederhauser """ + def __init__(self, configuration=None, identifier=None): super(Memory, self).__init__(configuration) self.identifier = identifier @@ -147,7 +150,7 @@ def triples(self, triple_pattern, context=None): yield (s, p, o), self.__contexts() def __len__(self, context=None): - #@@ optimize + # @@ optimize i = 0 for triple in self.triples((None, None, None)): i += 1 @@ -357,7 +360,7 @@ def triples(self, triplein, context=None): if self.__tripleHasContext(enctriple, cid)) def contexts(self, triple=None): - if triple is None or triple is (None,None,None): + if triple is None or triple is (None, None, None): return (context for context in self.__all_contexts) enctriple = self.__encodeTriple(triple) @@ -383,13 +386,11 @@ def remove_graph(self, graph): if not self.graph_aware: Store.remove_graph(self, graph) else: - self.remove((None,None,None), graph) + self.remove((None, None, None), graph) try: self.__all_contexts.remove(graph) except KeyError: - pass # we didn't know this graph, no problem - - + pass # we didn't know this graph, no problem # internal utility methods below @@ -500,10 +501,8 @@ def __emptygen(self): yield -import random - - def randid(randint=random.randint, choice=random.choice, signs=(-1, 1)): return choice(signs) * randint(1, 2000000000) + del random diff --git a/rdflib/plugins/parsers/ntriples.py b/rdflib/plugins/parsers/ntriples.py index 4f56a8901..0a47ecb91 100644 --- a/rdflib/plugins/parsers/ntriples.py +++ b/rdflib/plugins/parsers/ntriples.py @@ -60,6 +60,7 @@ def triple(self, s, p, o): self.length += 1 print(s, p, o) + quot = {'t': u'\t', 'n': u'\n', 'r': u'\r', '"': u'"', '\\': u'\\'} r_safe = re.compile(r'([\x20\x21\x23-\x5B\x5D-\x7E]+)') @@ -71,7 +72,7 @@ def unquote(s): """Unquote an N-Triples string.""" if not validate: - if isinstance(s, text_type): # nquads + if isinstance(s, text_type): # nquads s = decodeUnicodeEscape(s) else: s = s.decode('unicode-escape') @@ -106,6 +107,7 @@ def unquote(s): raise ParseError("Illegal literal character: %r" % s[0]) return u''.join(result) + r_hibyte = re.compile(r'([\x80-\xFF])') diff --git a/rdflib/plugins/parsers/rdfxml.py b/rdflib/plugins/parsers/rdfxml.py index 6e6d2df2f..dd8659a1b 100644 --- a/rdflib/plugins/parsers/rdfxml.py +++ b/rdflib/plugins/parsers/rdfxml.py @@ -173,7 +173,7 @@ def processingInstruction(self, target, data): pass def add_reified(self, sid, spo): - s,p,o = spo + s, p, o = spo self.store.add((sid, RDF.type, RDF.Statement)) self.store.add((sid, RDF.subject, s)) self.store.add((sid, RDF.predicate, p)) @@ -477,7 +477,7 @@ def property_element_end(self, name, qname): (self.parent.subject, current.predicate, current.object)) if current.id is not None: self.add_reified(current.id, (self.parent.subject, - current.predicate, current.object)) + current.predicate, current.object)) current.subject = None def list_node_element_end(self, name, qname): diff --git a/rdflib/plugins/parsers/trig.py b/rdflib/plugins/parsers/trig.py index b0410a1da..f4c3ff1b3 100644 --- a/rdflib/plugins/parsers/trig.py +++ b/rdflib/plugins/parsers/trig.py @@ -34,7 +34,6 @@ def directiveOrStatement(self, argstr, h): if j >= 0: return self.checkDot(argstr, j) - return j def labelOrSubject(self, argstr, i, res): @@ -48,13 +47,13 @@ def labelOrSubject(self, argstr, i, res): return j if argstr[i] == '[': - j = self.skipSpace(argstr, i+1) + j = self.skipSpace(argstr, i + 1) if j < 0: self.BadSyntax(argstr, i, - "Expected ] got EOF") + "Expected ] got EOF") if argstr[j] == ']': res.append(self.blankNode()) - return j+1 + return j + 1 return -1 def graph(self, argstr, i): @@ -68,8 +67,9 @@ def graph(self, argstr, i): """ #import pdb; pdb.set_trace() - j = self.sparqlTok('GRAPH', argstr, i) # optional GRAPH keyword - if j >= 0: i = j + j = self.sparqlTok('GRAPH', argstr, i) # optional GRAPH keyword + if j >= 0: + i = j r = [] j = self.labelOrSubject(argstr, i, r) @@ -77,15 +77,14 @@ def graph(self, argstr, i): graph = r[0] i = j else: - graph = self._store.graph.identifier # hack - + graph = self._store.graph.identifier # hack j = self.skipSpace(argstr, i) if j < 0: self.BadSyntax(argstr, i, "EOF found when expected graph") - if argstr[j:j + 1] == "=": # optional = for legacy support + if argstr[j:j + 1] == "=": # optional = for legacy support i = self.skipSpace(argstr, j + 1) if i < 0: @@ -93,11 +92,10 @@ def graph(self, argstr, i): else: i = j - if argstr[i:i+1] != "{": - return -1 # the node wasn't part of a graph + if argstr[i:i + 1] != "{": + return -1 # the node wasn't part of a graph - - j = i+1 + j = i + 1 oldParentContext = self._parentContext self._parentContext = self._context @@ -123,12 +121,10 @@ def graph(self, argstr, i): self._context = self._parentContext self._reason2 = reason2 self._parentContext = oldParentContext - #res.append(subj.close()) # No use until closed + # res.append(subj.close()) # No use until closed return j - - class TrigParser(Parser): """ An RDFLib parser for TriG @@ -151,7 +147,7 @@ def parse(self, source, graph, encoding="utf-8"): conj_graph = ConjunctiveGraph(store=graph.store, identifier=graph.identifier) conj_graph.default_context = graph # TODO: CG __init__ should have a # default_context arg - # TODO: update N3Processor so that it can use conj_graph as the sink + # TODO: update N3Processor so that it can use conj_graph as the sink conj_graph.namespace_manager = graph.namespace_manager sink = RDFSink(conj_graph) diff --git a/rdflib/plugins/serializers/nt.py b/rdflib/plugins/serializers/nt.py index 77ea5e8c8..ea2e2f32c 100644 --- a/rdflib/plugins/serializers/nt.py +++ b/rdflib/plugins/serializers/nt.py @@ -20,7 +20,7 @@ class NTSerializer(Serializer): def __init__(self, store): Serializer.__init__(self, store) - self.encoding = 'ascii' # n-triples are ascii encoded + self.encoding = 'ascii' # n-triples are ascii encoded def serialize(self, stream, base=None, encoding=None, **args): if base is not None: @@ -41,7 +41,7 @@ class NT11Serializer(NTSerializer): """ def __init__(self, store): - Serializer.__init__(self, store) # default to utf-8 + Serializer.__init__(self, store) # default to utf-8 def _nt_row(triple): @@ -79,8 +79,8 @@ def _quote_encode(l): .replace('"', '\\"')\ .replace('\r', '\\r') -def _nt_unicode_error_resolver(err): +def _nt_unicode_error_resolver(err): """ Do unicode char replaces as defined in https://www.w3.org/TR/2004/REC-rdf-testcases-20040210/#ntrip_strings """ @@ -91,6 +91,7 @@ def _replace_single(c): return fmt % c string = err.object[err.start:err.end] - return ( "".join( _replace_single(c) for c in string ), err.end ) + return ("".join(_replace_single(c) for c in string), err.end) + codecs.register_error('_rdflib_nt_escape', _nt_unicode_error_resolver) diff --git a/rdflib/plugins/serializers/rdfxml.py b/rdflib/plugins/serializers/rdfxml.py index 71602438a..d5ca78b9d 100644 --- a/rdflib/plugins/serializers/rdfxml.py +++ b/rdflib/plugins/serializers/rdfxml.py @@ -139,6 +139,7 @@ def predicate(self, predicate, object, depth=1): write("%s<%s rdf:resource=%s/>\n" % (indent, qname, quoteattr(self.relativize(object)))) + XMLLANG = "http://www.w3.org/XML/1998/namespacelang" XMLBASE = "http://www.w3.org/XML/1998/namespacebase" OWL_NS = Namespace('http://www.w3.org/2002/07/owl#') @@ -273,8 +274,8 @@ def predicate(self, predicate, object, depth=1): if object.language: writer.attribute(XMLLANG, object.language) - if (object.datatype == RDF.XMLLiteral and - isinstance(object.value, xml.dom.minidom.Document)): + if (object.datatype == RDF.XMLLiteral + and isinstance(object.value, xml.dom.minidom.Document)): writer.attribute(RDF.parseType, "Literal") writer.text(u"") writer.stream.write(object) diff --git a/rdflib/plugins/serializers/trig.py b/rdflib/plugins/serializers/trig.py index 6ccd8e10f..6c05ad75a 100644 --- a/rdflib/plugins/serializers/trig.py +++ b/rdflib/plugins/serializers/trig.py @@ -60,14 +60,15 @@ def serialize(self, stream, base=None, encoding=None, firstTime = True for store, (ordered_subjects, subjects, ref) in self._contexts.items(): - if not ordered_subjects: continue + if not ordered_subjects: + continue self._references = ref self._serialized = {} self.store = store self._subjects = subjects - if self.default_context and store.identifier==self.default_context: + if self.default_context and store.identifier == self.default_context: self.write(self.indent() + '\n{') else: if isinstance(store.identifier, BNode): diff --git a/rdflib/plugins/serializers/trix.py b/rdflib/plugins/serializers/trix.py index 84b9f8205..fceec6bdd 100644 --- a/rdflib/plugins/serializers/trix.py +++ b/rdflib/plugins/serializers/trix.py @@ -10,7 +10,7 @@ __all__ = ['TriXSerializer'] -## TODO: MOve this somewhere central +# TODO: Move this somewhere central TRIXNS = Namespace("http://www.w3.org/2004/03/trix/trix-1/") XMLNS = Namespace("http://www.w3.org/XML/1998/namespace") diff --git a/rdflib/plugins/serializers/turtle.py b/rdflib/plugins/serializers/turtle.py index 156f8a6cd..8ab9538e7 100644 --- a/rdflib/plugins/serializers/turtle.py +++ b/rdflib/plugins/serializers/turtle.py @@ -14,7 +14,8 @@ __all__ = ['RecursiveSerializer', 'TurtleSerializer'] -def _object_comparator(a,b): + +def _object_comparator(a, b): """ for nice clean output we sort the objects of triples, some of them are literals, @@ -24,8 +25,10 @@ def _object_comparator(a,b): """ try: - if a>b: return 1 - if a b: + return 1 + if a < b: + return -1 return 0 except TypeError: @@ -48,8 +51,8 @@ def __init__(self, store): self.reset() def addNamespace(self, prefix, uri): - if prefix in self.namespaces and self.namespaces[prefix]!=uri: - raise Exception("Trying to override namespace prefix %s => %s, but it's already bound to %s"%(prefix, uri, self.namespaces[prefix])) + if prefix in self.namespaces and self.namespaces[prefix] != uri: + raise Exception("Trying to override namespace prefix %s => %s, but it's already bound to %s" % (prefix, uri, self.namespaces[prefix])) self.namespaces[prefix] = uri def checkSubject(self, subject): @@ -57,8 +60,8 @@ def checkSubject(self, subject): if ((self.isDone(subject)) or (subject not in self._subjects) or ((subject in self._topLevels) and (self.depth > 1)) - or (isinstance(subject, URIRef) - and (self.depth >= self.maxDepth))): + or (isinstance(subject, URIRef) and + (self.depth >= self.maxDepth))): return False return True @@ -95,7 +98,7 @@ def preprocess(self): def preprocessTriple(self, spo): s, p, o = spo - self._references[o]+=1 + self._references[o] += 1 self._subjects[s] = True def reset(self): @@ -243,8 +246,8 @@ def preprocessTriple(self, triple): if isinstance(node, Literal) and node.datatype: self.getQName(node.datatype, gen_prefix=_GEN_QNAME_FOR_DT) p = triple[1] - if isinstance(p, BNode): # hmm - when is P ever a bnode? - self._references[p]+=1 + if isinstance(p, BNode): # hmm - when is P ever a bnode? + self._references[p] += 1 def getQName(self, uri, gen_prefix=True): if not isinstance(uri, URIRef): @@ -268,7 +271,8 @@ def getQName(self, uri, gen_prefix=True): prefix, namespace, local = parts # QName cannot end with . - if local.endswith("."): return None + if local.endswith("."): + return None prefix = self.addNamespace(prefix, namespace) diff --git a/rdflib/plugins/sleepycat.py b/rdflib/plugins/sleepycat.py index c265e1902..745e270ad 100644 --- a/rdflib/plugins/sleepycat.py +++ b/rdflib/plugins/sleepycat.py @@ -1,3 +1,7 @@ +import logging +from threading import Thread +from os.path import exists, abspath +from os import mkdir from rdflib.store import Store, VALID_STORE, NO_STORE from rdflib.term import URIRef from six import b @@ -17,10 +21,7 @@ def bb(u): has_bsddb = True except ImportError: has_bsddb = False -from os import mkdir -from os.path import exists, abspath -from threading import Thread if has_bsddb: # These are passed to bsddb when creating DBs @@ -34,7 +35,6 @@ def bb(u): # passed to db.DB.Open() DBOPENFLAGS = db.DB_THREAD -import logging logger = logging.getLogger(__name__) __all__ = ['Sleepycat'] @@ -65,7 +65,7 @@ def _init_db_environment(self, homeDir, create=True): if not exists(homeDir): if create is True: mkdir(homeDir) - # TODO: implement create method and refactor this to it + # TODO: implement create method and refactor this to it self.create(homeDir) else: return NO_STORE @@ -314,7 +314,7 @@ def remove(self, spo, context, txn=None): o = _to_string(object, txn=txn) c = _to_string(context, txn=txn) value = self.__indicies[0].get(bb("%s^%s^%s^%s^" % - (c, s, p, o)), txn=txn) + (c, s, p, o)), txn=txn) if value is not None: self.__remove((bb(s), bb(p), bb(o)), bb(c), txn=txn) self.__needs_sync = True diff --git a/rdflib/plugins/sparql/__init__.py b/rdflib/plugins/sparql/__init__.py index 250fb501b..bc1227f2f 100644 --- a/rdflib/plugins/sparql/__init__.py +++ b/rdflib/plugins/sparql/__init__.py @@ -30,8 +30,6 @@ PLUGIN_ENTRY_POINT = 'rdf.plugins.sparqleval' - - from . import parser from . import operators from . import parserutils diff --git a/rdflib/plugins/sparql/aggregates.py b/rdflib/plugins/sparql/aggregates.py index 3e657c994..b63b6edb8 100644 --- a/rdflib/plugins/sparql/aggregates.py +++ b/rdflib/plugins/sparql/aggregates.py @@ -13,6 +13,7 @@ Aggregation functions """ + class Accumulator(object): """abstract base class for different aggregation functions """ @@ -73,8 +74,8 @@ def use_row(self, row): def type_safe_numbers(*args): if ( - any(isinstance(arg, float) for arg in args) - and any(isinstance(arg, Decimal) for arg in args) + any(isinstance(arg, float) for arg in args) and + any(isinstance(arg, Decimal) for arg in args) ): return map(float, args) return args @@ -106,6 +107,7 @@ def update(self, row, aggregator): def get_value(self): return Literal(self.value, datatype=self.datatype) + class Average(Accumulator): def __init__(self, aggregation): @@ -193,7 +195,7 @@ def __init__(self, aggregation): def update(self, row, aggregator): try: # set the value now - aggregator.bindings[self.var] = _eval(self.expr, row) + aggregator.bindings[self.var] = _eval(self.expr, row) # and skip this accumulator for future rows del aggregator.accumulators[self.var] except NotBoundError: @@ -203,6 +205,7 @@ def get_value(self): # set None if no value was set return None + class GroupConcat(Accumulator): def __init__(self, aggregation): diff --git a/rdflib/plugins/sparql/algebra.py b/rdflib/plugins/sparql/algebra.py index 56a5cfad5..5fa445c70 100644 --- a/rdflib/plugins/sparql/algebra.py +++ b/rdflib/plugins/sparql/algebra.py @@ -67,9 +67,11 @@ def Filter(expr, p): def Extend(p, expr, var): return CompValue('Extend', p=p, expr=expr, var=var) + def Values(res): return CompValue('values', res=res) + def Project(p, PV): return CompValue('Project', p=p, PV=PV) @@ -79,11 +81,11 @@ def Group(p, expr=None): def _knownTerms(triple, varsknown, varscount): - return (len([ x for x in triple if x not in varsknown and - isinstance(x, (Variable, BNode)) ]), + return (len([x for x in triple if x not in varsknown and + isinstance(x, (Variable, BNode))]), -sum(varscount.get(x, 0) for x in triple), not isinstance(triple[2], Literal), - ) + ) def reorderTriples(l): @@ -117,8 +119,8 @@ def _addvar(term, varsknown): 1], varsknown, varscount), x[1]) for x in l[i:]) t = l[i][0][0] # top block has this many terms bound j = 0 - while i+j < len(l) and l[i+j][0][0] == t: - for c in l[i+j][1]: + while i + j < len(l) and l[i + j][0][0] == t: + for c in l[i + j][1]: _addvar(c, varsknown) j += 1 i += 1 @@ -150,7 +152,6 @@ def translatePName(p, prologue): def translatePath(p): - """ Translate PropertyPath expressions """ @@ -196,7 +197,6 @@ def translatePath(p): def translateExists(e): - """ Translate the graph pattern used by EXISTS and NOT EXISTS http://www.w3.org/TR/sparql11-query/#sparqlCollectFilters @@ -216,7 +216,6 @@ def _c(n): def collectAndRemoveFilters(parts): - """ FILTER expressions apply to the whole group graph pattern in which @@ -444,7 +443,8 @@ def _addVars(x, children): x["_vars"] = set() elif x.name == "Extend": # vars only used in the expr for a bind should not be included - x["_vars"] = reduce(operator.or_, [ child for child,part in zip(children,x) if part!='expr' ], set()) + x["_vars"] = reduce(operator.or_, [child for child, + part in zip(children, x) if part != 'expr'], set()) else: x["_vars"] = set(reduce(operator.or_, children, set())) @@ -490,7 +490,6 @@ def translateAggregates(q, M): v.expr = traverse(v.expr, functools.partial(_sample, v=v.evar)) v.expr = traverse(v.expr, functools.partial(_aggs, A=A)) - # having clause if traverse(q.having, _hasAggregate, complete=False): q.having = traverse(q.having, _sample) @@ -606,7 +605,7 @@ def translate(q): # ORDER BY if q.orderby: M = OrderBy(M, [CompValue('OrderCondition', expr=c.expr, - order=c.order) for c in q.orderby.condition]) + order=c.order) for c in q.orderby.condition]) # PROJECT M = Project(M, PV) @@ -645,7 +644,6 @@ def simplify(n): def analyse(n, children): - """ Some things can be lazily joined. This propegates whether they can up the tree @@ -807,6 +805,7 @@ def pp(p, ind=" "): for x in q: pp(x) + if __name__ == '__main__': import sys from rdflib.plugins.sparql import parser diff --git a/rdflib/plugins/sparql/evaluate.py b/rdflib/plugins/sparql/evaluate.py index a693dff79..4ce4c9548 100644 --- a/rdflib/plugins/sparql/evaluate.py +++ b/rdflib/plugins/sparql/evaluate.py @@ -30,8 +30,8 @@ from rdflib.plugins.sparql.aggregates import Aggregator from rdflib.plugins.sparql.algebra import Join, ToMultiSet, Values -def evalBGP(ctx, bgp): +def evalBGP(ctx, bgp): """ A basic graph pattern """ @@ -96,7 +96,7 @@ def evalLazyJoin(ctx, join): for a in evalPart(ctx, join.p1): c = ctx.thaw(a) for b in evalPart(c, join.p2): - yield b.merge(a) # merge, as some bindings may have been forgotten + yield b.merge(a) # merge, as some bindings may have been forgotten def evalJoin(ctx, join): @@ -142,8 +142,8 @@ def evalLeftJoin(ctx, join): # even without prior bindings... p1_vars = join.p1._vars if p1_vars is None \ - or not any(_ebv(join.expr, b) for b in - evalPart(ctx.thaw(a.remember(p1_vars)), join.p2)): + or not any(_ebv(join.expr, b) for b in + evalPart(ctx.thaw(a.remember(p1_vars)), join.p2)): yield a @@ -272,7 +272,6 @@ def evalPart(ctx, part): def evalGroup(ctx, group): - """ http://www.w3.org/TR/sparql11-query/#defn_algGroup """ @@ -324,7 +323,7 @@ def evalOrderBy(ctx, part): def evalSlice(ctx, slice): res = evalPart(ctx, slice.p) - return itertools.islice(res, slice.start, slice.start+slice.length if slice.length is not None else None) + return itertools.islice(res, slice.start, slice.start + slice.length if slice.length is not None else None) def evalReduced(ctx, part): @@ -355,7 +354,7 @@ def evalReduced(ctx, part): # forget last position of row mru_queue.remove(row) else: - #row seems to be new + # row seems to be new yield row mru_set.add(row) if len(mru_set) > MAX: @@ -422,7 +421,7 @@ def evalConstructQuery(ctx, query): def evalQuery(graph, query, initBindings, base=None): - initBindings = dict( ( Variable(k),v ) for k,v in iteritems(initBindings) ) + initBindings = dict((Variable(k), v) for k, v in iteritems(initBindings)) ctx = QueryContext(graph, initBindings=initBindings) diff --git a/rdflib/plugins/sparql/evalutils.py b/rdflib/plugins/sparql/evalutils.py index dd0924d00..25353fe05 100644 --- a/rdflib/plugins/sparql/evalutils.py +++ b/rdflib/plugins/sparql/evalutils.py @@ -23,7 +23,6 @@ def _minus(a, b): yield x - def _join(a, b): for x in a: for y in b: @@ -32,7 +31,6 @@ def _join(a, b): def _ebv(expr, ctx): - """ Return true/false for the given expr Either the expr is itself true/false @@ -88,7 +86,6 @@ def _filter(a, expr): def _fillTemplate(template, solution): - """ For construct/deleteWhere and friends @@ -113,6 +110,7 @@ def _fillTemplate(template, solution): yield (_s, _p, _o) + def _val(v): """ utilitity for ordering things""" if isinstance(v, Variable): diff --git a/rdflib/plugins/sparql/operators.py b/rdflib/plugins/sparql/operators.py index 5fec1f457..74cd12697 100644 --- a/rdflib/plugins/sparql/operators.py +++ b/rdflib/plugins/sparql/operators.py @@ -534,7 +534,6 @@ def Builtin_UCASE(e, ctx): def Builtin_LANG(e, ctx): - """ http://www.w3.org/TR/sparql11-query/#func-lang @@ -587,6 +586,7 @@ def Builtin_EXISTS(e, ctx): _CUSTOM_FUNCTIONS = {} + def register_custom_function(uri, func, override=False, raw=False): """ Register a custom SPARQL function. @@ -600,6 +600,7 @@ def register_custom_function(uri, func, override=False, raw=False): raise ValueError("A function is already registered as %s" % uri.n3()) _CUSTOM_FUNCTIONS[uri] = (func, raw) + def custom_function(uri, override=False, raw=False): """ Decorator version of :func:`register_custom_function`. @@ -609,17 +610,18 @@ def decorator(func): return func return decorator + def unregister_custom_function(uri, func): if _CUSTOM_FUNCTIONS.get(uri, (None, None))[0] != func: raise ValueError("This function is not registered as %s" % uri.n3()) del _CUSTOM_FUNCTIONS[uri] - + def Function(e, ctx): """ Custom functions and casts """ - pair =_CUSTOM_FUNCTIONS.get(e.iri) + pair = _CUSTOM_FUNCTIONS.get(e.iri) if pair is None: # no such function is registered raise SPARQLError('Unknown function %r' % e.iri) @@ -644,71 +646,71 @@ def Function(e, ctx): @custom_function(XSD.integer, raw=True) @custom_function(XSD.boolean, raw=True) def default_cast(e, ctx): - if not e.expr: - raise SPARQLError("Nothing given to cast.") - if len(e.expr) > 1: - raise SPARQLError("Cannot cast more than one thing!") + if not e.expr: + raise SPARQLError("Nothing given to cast.") + if len(e.expr) > 1: + raise SPARQLError("Cannot cast more than one thing!") - x = e.expr[0] + x = e.expr[0] - if e.iri == XSD.string: + if e.iri == XSD.string: - if isinstance(x, (URIRef, Literal)): - return Literal(x, datatype=XSD.string) - else: - raise SPARQLError( - "Cannot cast term %r of type %r" % (x, type(x))) - - if not isinstance(x, Literal): + if isinstance(x, (URIRef, Literal)): + return Literal(x, datatype=XSD.string) + else: raise SPARQLError( - "Can only cast Literals to non-string data-types") + "Cannot cast term %r of type %r" % (x, type(x))) - if x.datatype and not x.datatype in XSD_DTs: - raise SPARQLError( - "Cannot cast literal with unknown datatype: %r" % x.datatype) + if not isinstance(x, Literal): + raise SPARQLError( + "Can only cast Literals to non-string data-types") - if e.iri == XSD.dateTime: - if x.datatype and x.datatype not in (XSD.dateTime, XSD.string): - raise SPARQLError( - "Cannot cast %r to XSD:dateTime" % x.datatype) - try: - return Literal(isodate.parse_datetime(x), datatype=e.iri) - except: - raise SPARQLError("Cannot interpret '%r' as datetime" % x) + if x.datatype and not x.datatype in XSD_DTs: + raise SPARQLError( + "Cannot cast literal with unknown datatype: %r" % x.datatype) - if x.datatype == XSD.dateTime: - raise SPARQLError("Cannot cast XSD.dateTime to %r" % e.iri) + if e.iri == XSD.dateTime: + if x.datatype and x.datatype not in (XSD.dateTime, XSD.string): + raise SPARQLError( + "Cannot cast %r to XSD:dateTime" % x.datatype) + try: + return Literal(isodate.parse_datetime(x), datatype=e.iri) + except: + raise SPARQLError("Cannot interpret '%r' as datetime" % x) - if e.iri in (XSD.float, XSD.double): - try: - return Literal(float(x), datatype=e.iri) - except: - raise SPARQLError("Cannot interpret '%r' as float" % x) + if x.datatype == XSD.dateTime: + raise SPARQLError("Cannot cast XSD.dateTime to %r" % e.iri) - elif e.iri == XSD.decimal: - if "e" in x or "E" in x: # SPARQL/XSD does not allow exponents in decimals - raise SPARQLError("Cannot interpret '%r' as decimal" % x) - try: - return Literal(Decimal(x), datatype=e.iri) - except: - raise SPARQLError("Cannot interpret '%r' as decimal" % x) + if e.iri in (XSD.float, XSD.double): + try: + return Literal(float(x), datatype=e.iri) + except: + raise SPARQLError("Cannot interpret '%r' as float" % x) - elif e.iri == XSD.integer: - try: - return Literal(int(x), datatype=XSD.integer) - except: - raise SPARQLError("Cannot interpret '%r' as int" % x) - - elif e.iri == XSD.boolean: - # # I would argue that any number is True... - # try: - # return Literal(bool(int(x)), datatype=XSD.boolean) - # except: - if x.lower() in ("1", "true"): - return Literal(True) - if x.lower() in ("0", "false"): - return Literal(False) - raise SPARQLError("Cannot interpret '%r' as bool" % x) + elif e.iri == XSD.decimal: + if "e" in x or "E" in x: # SPARQL/XSD does not allow exponents in decimals + raise SPARQLError("Cannot interpret '%r' as decimal" % x) + try: + return Literal(Decimal(x), datatype=e.iri) + except: + raise SPARQLError("Cannot interpret '%r' as decimal" % x) + + elif e.iri == XSD.integer: + try: + return Literal(int(x), datatype=XSD.integer) + except: + raise SPARQLError("Cannot interpret '%r' as int" % x) + + elif e.iri == XSD.boolean: + # # I would argue that any number is True... + # try: + # return Literal(bool(int(x)), datatype=XSD.boolean) + # except: + if x.lower() in ("1", "true"): + return Literal(True) + if x.lower() in ("0", "false"): + return Literal(False) + raise SPARQLError("Cannot interpret '%r' as bool" % x) def UnaryNot(expr, ctx): @@ -905,6 +907,7 @@ def and_(*args): return Expr('ConditionalAndExpression', ConditionalAndExpression, expr=args[0], other=list(args[1:])) + TrueFilter = Expr('TrueFilter', lambda _1, _2: Literal(True)) diff --git a/rdflib/plugins/sparql/parser.py b/rdflib/plugins/sparql/parser.py index 62624c05f..ab99e58b5 100644 --- a/rdflib/plugins/sparql/parser.py +++ b/rdflib/plugins/sparql/parser.py @@ -42,7 +42,6 @@ def setDataType(terms): def expandTriples(terms): - """ Expand ; and , syntax for repeat predicates, subjects """ @@ -56,8 +55,8 @@ def expandTriples(terms): if t == ',': res.extend([res[-3], res[-2]]) elif t == ';': - if i+1 == len(terms) or terms[i+1] == ";" or terms[i+1] == ".": - continue # this semicolon is spurious + if i + 1 == len(terms) or terms[i + 1] == ";" or terms[i + 1] == ".": + continue # this semicolon is spurious res.append(res[0]) elif isinstance(t, list): # BlankNodePropertyList @@ -68,7 +67,7 @@ def expandTriples(terms): if len(t) > 1: res += t # is this bnode the subject of more triples? - if i + 1 < l and terms[i + 1] not in ".,;" : + if i + 1 < l and terms[i + 1] not in ".,;": res.append(t[0]) elif isinstance(t, ParseResults): res += t.asList() @@ -178,7 +177,7 @@ def expandCollection(terms): # [168] PN_PREFIX ::= PN_CHARS_BASE ((PN_CHARS|'.')* PN_CHARS)? PN_PREFIX = Regex(u'[%s](?:[%s\\.]*[%s])?' % (PN_CHARS_BASE_re, - PN_CHARS_re, PN_CHARS_re), flags=re.U) + PN_CHARS_re, PN_CHARS_re), flags=re.U) # [140] PNAME_NS ::= PN_PREFIX? ':' PNAME_NS = Optional( @@ -187,7 +186,7 @@ def expandCollection(terms): # [173] PN_LOCAL_ESC ::= '\' ( '_' | '~' | '.' | '-' | '!' | '$' | '&' | "'" | '(' | ')' | '*' | '+' | ',' | ';' | '=' | '/' | '?' | '#' | '@' | '%' ) PN_LOCAL_ESC_re = '\\\\[_~\\.\\-!$&"\'()*+,;=/?#@%]' -#PN_LOCAL_ESC = Regex(PN_LOCAL_ESC_re) # regex'd +# PN_LOCAL_ESC = Regex(PN_LOCAL_ESC_re) # regex'd #PN_LOCAL_ESC.setParseAction(lambda x: x[0][1:]) # [172] HEX ::= [0-9] | [A-F] | [a-f] @@ -195,28 +194,28 @@ def expandCollection(terms): # [171] PERCENT ::= '%' HEX HEX PERCENT_re = '%[0-9a-fA-F]{2}' -#PERCENT = Regex(PERCENT_re) # regex'd +# PERCENT = Regex(PERCENT_re) # regex'd #PERCENT.setParseAction(lambda x: unichr(int(x[0][1:], 16))) # [170] PLX ::= PERCENT | PN_LOCAL_ESC -PLX_re = '(%s|%s)'%(PN_LOCAL_ESC_re,PERCENT_re) -#PLX = PERCENT | PN_LOCAL_ESC # regex'd +PLX_re = '(%s|%s)' % (PN_LOCAL_ESC_re, PERCENT_re) +# PLX = PERCENT | PN_LOCAL_ESC # regex'd # [169] PN_LOCAL ::= (PN_CHARS_U | ':' | [0-9] | PLX ) ((PN_CHARS | '.' | ':' | PLX)* (PN_CHARS | ':' | PLX) )? PN_LOCAL = Regex(u"""([%(PN_CHARS_U)s:0-9]|%(PLX)s) (([%(PN_CHARS)s\\.:]|%(PLX)s)* - ([%(PN_CHARS)s:]|%(PLX)s) )?"""%dict(PN_CHARS_U=PN_CHARS_U_re, - PN_CHARS=PN_CHARS_re, - PLX=PLX_re), flags=re.X|re.UNICODE) + ([%(PN_CHARS)s:]|%(PLX)s) )?""" % dict(PN_CHARS_U=PN_CHARS_U_re, + PN_CHARS=PN_CHARS_re, + PLX=PLX_re), flags=re.X | re.UNICODE) + def _hexExpand(match): return unichr(int(match.group(0)[1:], 16)) -PN_LOCAL.setParseAction(lambda x: re.sub("(%s)"%PERCENT_re, _hexExpand, x[0])) - +PN_LOCAL.setParseAction(lambda x: re.sub("(%s)" % PERCENT_re, _hexExpand, x[0])) # [141] PNAME_LN ::= PNAME_NS PN_LOCAL @@ -267,7 +266,7 @@ def _hexExpand(match): # [149] INTEGER_POSITIVE ::= '+' INTEGER INTEGER_POSITIVE = Suppress('+') + INTEGER.copy().leaveWhitespace() INTEGER_POSITIVE.setParseAction(lambda x: rdflib.Literal( - "+"+x[0], datatype=rdflib.XSD.integer)) + "+" + x[0], datatype=rdflib.XSD.integer)) # [150] DECIMAL_POSITIVE ::= '+' DECIMAL DECIMAL_POSITIVE = Suppress('+') + DECIMAL.copy().leaveWhitespace() @@ -505,7 +504,7 @@ def _hexExpand(match): # [77] PropertyListNotEmpty ::= Verb ObjectList ( ';' ( Verb ObjectList )? )* PropertyListNotEmpty = Verb + ObjectList + ZeroOrMore(';' + Optional(Verb + - ObjectList)) + ObjectList)) # [76] PropertyList ::= Optional(PropertyListNotEmpty) @@ -765,7 +764,7 @@ def _hexExpand(match): # [116] AdditiveExpression ::= MultiplicativeExpression ( '+' MultiplicativeExpression | '-' MultiplicativeExpression | ( NumericLiteralPositive | NumericLiteralNegative ) ( ( '*' UnaryExpression ) | ( '/' UnaryExpression ) )* )* -### NOTE: The second part of this production is there because: +# NOTE: The second part of this production is there because: ### "In signed numbers, no white space is allowed between the sign and the number. The AdditiveExpression grammar rule allows for this by covering the two cases of an expression followed by a signed number. These produce an addition or subtraction of the unsigned number as appropriate." # Here (I think) this is not nescessary since pyparsing doesn't separate @@ -774,7 +773,7 @@ def _hexExpand(match): AdditiveExpression = Comp('AdditiveExpression', Param('expr', MultiplicativeExpression) + ZeroOrMore(ParamList('op', '+') + ParamList('other', MultiplicativeExpression) | - ParamList('op', '-') + ParamList('other', MultiplicativeExpression))).setEvalFn(op.AdditiveExpression) + ParamList('op', '-') + ParamList('other', MultiplicativeExpression))).setEvalFn(op.AdditiveExpression) # [115] NumericExpression ::= AdditiveExpression @@ -979,7 +978,7 @@ def _hexExpand(match): # [9] SelectClause ::= 'SELECT' ( 'DISTINCT' | 'REDUCED' )? ( ( Var | ( '(' Expression 'AS' Var ')' ) )+ | '*' ) SelectClause = Keyword('SELECT') + Optional(Param('modifier', Keyword('DISTINCT') | Keyword('REDUCED'))) + (OneOrMore(ParamList('projection', Comp('vars', - Param('var', Var) | (Literal('(') + Param('expr', Expression) + Keyword('AS') + Param('evar', Var) + ')')))) | '*') + Param('var', Var) | (Literal('(') + Param('expr', Expression) + Keyword('AS') + Param('evar', Var) + ')')))) | '*') # [17] WhereClause ::= 'WHERE'? GroupGraphPattern WhereClause = Optional(Keyword('WHERE')) + Param('where', GroupGraphPattern) @@ -1013,7 +1012,7 @@ def _hexExpand(match): # [29] Update ::= Prologue ( Update1 ( ';' Update )? )? Update = Forward() Update <<= (ParamList('prologue', Prologue) + Optional(ParamList('request', - Update1) + Optional(';' + Update))) + Update1) + Optional(';' + Update))) # [2] Query ::= Prologue diff --git a/rdflib/plugins/sparql/parserutils.py b/rdflib/plugins/sparql/parserutils.py index 64b51d171..0a27d70d6 100644 --- a/rdflib/plugins/sparql/parserutils.py +++ b/rdflib/plugins/sparql/parserutils.py @@ -45,7 +45,6 @@ # Comp('Sum')( Param('x')(Number) + '+' + Param('y')(Number) ) def value(ctx, val, variables=False, errors=False): - """ utility function for evaluating something... @@ -91,6 +90,7 @@ class ParamValue(object): This just keeps the name/value All cleverness is in the CompValue """ + def __init__(self, name, tokenList, isList): self.isList = isList self.name = name @@ -109,6 +109,7 @@ class Param(TokenConverter): if isList is true repeat occurrences of ParamList have their values merged in a list """ + def __init__(self, name, expr, isList=False): self.name = name self.isList = isList @@ -123,6 +124,7 @@ class ParamList(Param): """ A shortcut for a Param with isList=True """ + def __init__(self, name, expr): Param.__init__(self, name, expr, True) @@ -242,28 +244,28 @@ def setEvalFn(self, evalfn): def prettify_parsetree(t, indent='', depth=0): - out = [] - if isinstance(t, ParseResults): - for e in t.asList(): - out.append(prettify_parsetree(e, indent, depth + 1)) - for k, v in sorted(t.items()): - out.append("%s%s- %s:\n" % (indent, ' ' * depth, k)) - out.append(prettify_parsetree(v, indent, depth + 1)) - elif isinstance(t, CompValue): - out.append("%s%s> %s:\n" % (indent, ' ' * depth, t.name)) - for k, v in t.items(): - out.append("%s%s- %s:\n" % (indent, ' ' * (depth + 1), k)) - out.append(prettify_parsetree(v, indent, depth + 2)) - elif isinstance(t, dict): - for k, v in t.items(): - out.append("%s%s- %s:\n" % (indent, ' ' * (depth + 1), k)) - out.append(prettify_parsetree(v, indent, depth + 2)) - elif isinstance(t, list): - for e in t: - out.append(prettify_parsetree(e, indent, depth + 1)) - else: - out.append("%s%s- %r\n" % (indent, ' ' * depth, t)) - return "".join(out) + out = [] + if isinstance(t, ParseResults): + for e in t.asList(): + out.append(prettify_parsetree(e, indent, depth + 1)) + for k, v in sorted(t.items()): + out.append("%s%s- %s:\n" % (indent, ' ' * depth, k)) + out.append(prettify_parsetree(v, indent, depth + 1)) + elif isinstance(t, CompValue): + out.append("%s%s> %s:\n" % (indent, ' ' * depth, t.name)) + for k, v in t.items(): + out.append("%s%s- %s:\n" % (indent, ' ' * (depth + 1), k)) + out.append(prettify_parsetree(v, indent, depth + 2)) + elif isinstance(t, dict): + for k, v in t.items(): + out.append("%s%s- %s:\n" % (indent, ' ' * (depth + 1), k)) + out.append(prettify_parsetree(v, indent, depth + 2)) + elif isinstance(t, list): + for e in t: + out.append(prettify_parsetree(e, indent, depth + 1)) + else: + out.append("%s%s- %r\n" % (indent, ' ' * depth, t)) + return "".join(out) if __name__ == '__main__': diff --git a/rdflib/plugins/sparql/processor.py b/rdflib/plugins/sparql/processor.py index 5ecbb4355..857f5e03c 100644 --- a/rdflib/plugins/sparql/processor.py +++ b/rdflib/plugins/sparql/processor.py @@ -46,13 +46,14 @@ def __init__(self, res): self.askAnswer = res.get("askAnswer") self.graph = res.get("graph") + class SPARQLUpdateProcessor(UpdateProcessor): def __init__(self, graph): self.graph = graph def update(self, strOrQuery, initBindings={}, initNs={}): if isinstance(strOrQuery, string_types): - strOrQuery=translateUpdate(parseUpdate(strOrQuery), initNs=initNs) + strOrQuery = translateUpdate(parseUpdate(strOrQuery), initNs=initNs) return evalUpdate(self.graph, strOrQuery, initBindings) diff --git a/rdflib/plugins/sparql/results/graph.py b/rdflib/plugins/sparql/results/graph.py index b02aab7df..c47daa720 100644 --- a/rdflib/plugins/sparql/results/graph.py +++ b/rdflib/plugins/sparql/results/graph.py @@ -7,11 +7,12 @@ ResultException ) + class GraphResultParser(ResultParser): def parse(self, source, content_type): - res = Result('CONSTRUCT') # hmm - or describe?type_) + res = Result('CONSTRUCT') # hmm - or describe?type_) res.graph = Graph() res.graph.parse(source, format=content_type) diff --git a/rdflib/plugins/sparql/results/rdfresults.py b/rdflib/plugins/sparql/results/rdfresults.py index 946391542..088268090 100644 --- a/rdflib/plugins/sparql/results/rdfresults.py +++ b/rdflib/plugins/sparql/results/rdfresults.py @@ -21,7 +21,7 @@ def __init__(self, source, **kwargs): graph = source rs = graph.value(predicate=RDF.type, object=RS.ResultSet) - # there better be only one :) + # there better be only one :) if rs is None: type_ = 'CONSTRUCT' diff --git a/rdflib/plugins/sparql/results/tsvresults.py b/rdflib/plugins/sparql/results/tsvresults.py index fb5da07d8..6e9366f35 100644 --- a/rdflib/plugins/sparql/results/tsvresults.py +++ b/rdflib/plugins/sparql/results/tsvresults.py @@ -90,6 +90,7 @@ def convertTerm(self, t): else: return t + if __name__ == '__main__': import sys r = Result.parse(file(sys.argv[1]), format='tsv') diff --git a/rdflib/plugins/sparql/results/txtresults.py b/rdflib/plugins/sparql/results/txtresults.py index 7e91bc9e3..d381f4ee0 100644 --- a/rdflib/plugins/sparql/results/txtresults.py +++ b/rdflib/plugins/sparql/results/txtresults.py @@ -2,6 +2,7 @@ from rdflib import URIRef, BNode, Literal from rdflib.query import ResultSerializer + def _termString(t, namespace_manager): if t == None: return "-" @@ -21,23 +22,22 @@ class TXTResultSerializer(ResultSerializer): A write only QueryResult serializer for text/ascii tables """ - def serialize(self, stream, encoding, namespace_manager = None): - + def serialize(self, stream, encoding, namespace_manager=None): """ return a text table of query results """ - def c(s, w): """ center the string s in w wide string """ w -= len(s) h1 = h2 = w // 2 - if w % 2: h2 += 1 + if w % 2: + h2 += 1 return " " * h1 + s + " " * h2 - if self.result.type!='SELECT': + if self.result.type != 'SELECT': raise Exception("Can only pretty print SELECT results!") if not self.result: @@ -53,8 +53,7 @@ def c(s, w): stream.write( "|".join([c(k, maxlen[i]) for i, k in enumerate(keys)]) + "\n") - stream.write("-" * (len(maxlen)+sum(maxlen)) + "\n") + stream.write("-" * (len(maxlen) + sum(maxlen)) + "\n") for r in sorted(b): stream.write("|".join( [t + " " * (i - len(t)) for i, t in zip(maxlen, r)]) + "\n") - diff --git a/rdflib/plugins/sparql/results/xmlresults.py b/rdflib/plugins/sparql/results/xmlresults.py index ba35749cc..e1daff926 100644 --- a/rdflib/plugins/sparql/results/xmlresults.py +++ b/rdflib/plugins/sparql/results/xmlresults.py @@ -80,9 +80,6 @@ def __init__(self, source, content_type=None): self.askAnswer = boolean.text.lower().strip() == "true" - - - def parseTerm(element): """rdflib object (Literal, URIRef, BNode) for the given elementtree element""" @@ -137,6 +134,7 @@ class SPARQLXMLWriter: """ Python saxutils-based SPARQL XML Writer """ + def __init__(self, output, encoding='utf-8'): writer = XMLGenerator(output, encoding) writer.startDocument() diff --git a/rdflib/plugins/sparql/sparql.py b/rdflib/plugins/sparql/sparql.py index 146f9fada..9f6a5c172 100644 --- a/rdflib/plugins/sparql/sparql.py +++ b/rdflib/plugins/sparql/sparql.py @@ -17,7 +17,6 @@ import rdflib.plugins.sparql - class SPARQLError(Exception): def __init__(self, msg=None): Exception.__init__(self, msg) @@ -30,6 +29,7 @@ def __init__(self, msg=None): class AlreadyBound(SPARQLError): """Raised when trying to bind a variable that is already bound!""" + def __init__(self): SPARQLError.__init__(self) @@ -89,7 +89,7 @@ def __iter__(self): d = d.outer def __str__(self): - return "Bindings({"+", ".join((k, self[k]) for k in self)+"})" + return "Bindings({" + ", ".join((k, self[k]) for k in self) + "})" def __repr__(self): return text_type(self) @@ -102,6 +102,7 @@ class FrozenDict(Mapping): Taken from http://stackoverflow.com/a/2704866/81121 """ + def __init__(self, *args, **kwargs): self._d = dict(*args, **kwargs) self._hash = None @@ -230,7 +231,8 @@ class QueryContext(object): def __init__(self, graph=None, bindings=None, initBindings=None): self.initBindings = initBindings self.bindings = Bindings(d=bindings or []) - if initBindings: self.bindings.update(initBindings) + if initBindings: + self.bindings.update(initBindings) if isinstance(graph, ConjunctiveGraph): self._dataset = graph @@ -281,7 +283,7 @@ def _load(graph, source): except: raise Exception( "Could not load %s as either RDF/XML, N3 or NTriples" % ( - source)) + source)) if not rdflib.plugins.sparql.SPARQL_LOAD_GRAPHS: # we are not loading - if we already know the graph @@ -375,7 +377,6 @@ def bind(self, prefix, uri): self.namespace_manager.bind(prefix, uri, replace=True) def absolutize(self, iri): - """ Apply BASE / PREFIXes to URIs (and to datatypes in Literals) diff --git a/rdflib/plugins/sparql/update.py b/rdflib/plugins/sparql/update.py index 94cb01cd1..daf380a3e 100644 --- a/rdflib/plugins/sparql/update.py +++ b/rdflib/plugins/sparql/update.py @@ -64,6 +64,7 @@ def evalClear(ctx, u): for g in _graphAll(ctx, u.graphiri): g.remove((None, None, None)) + def evalDrop(ctx, u): """ http://www.w3.org/TR/sparql11-update/#drop @@ -276,12 +277,11 @@ def evalUpdate(graph, update, initBindings={}): for u in update: - initBindings = dict( ( Variable(k),v ) for k,v in iteritems(initBindings) ) + initBindings = dict((Variable(k), v) for k, v in iteritems(initBindings)) ctx = QueryContext(graph, initBindings=initBindings) ctx.prologue = u.prologue - try: if u.name == 'Load': evalLoad(ctx, u) diff --git a/rdflib/plugins/stores/auditable.py b/rdflib/plugins/stores/auditable.py index 61893d27c..7a3492b76 100644 --- a/rdflib/plugins/stores/auditable.py +++ b/rdflib/plugins/stores/auditable.py @@ -56,7 +56,7 @@ def add(self, triple, context, quoted=False): context = context.__class__(self.store, context.identifier) if context is not None else None ctxId = context.identifier if context is not None else None if list(self.store.triples(triple, context)): - return # triple already in store, do nothing + return # triple already in store, do nothing self.reverseOps.append((s, p, o, ctxId, 'remove')) try: self.reverseOps.remove((s, p, o, ctxId, 'add')) @@ -88,7 +88,7 @@ def remove(self, spo, context=None): self.reverseOps.append((s, p, o, ctx.identifier, 'add')) else: if not list(self.triples((subject, predicate, object_), context)): - return # triple not present in store, do nothing + return # triple not present in store, do nothing try: self.reverseOps.remove((subject, predicate, object_, ctxId, 'remove')) except ValueError: diff --git a/rdflib/plugins/stores/regexmatching.py b/rdflib/plugins/stores/regexmatching.py index a3e188f83..773dfab34 100644 --- a/rdflib/plugins/stores/regexmatching.py +++ b/rdflib/plugins/stores/regexmatching.py @@ -27,6 +27,7 @@ class REGEXTerm(text_type): perform a REGEX match (not a string comparison) using the value (pre-compiled) for checking rdf:type matches """ + def __init__(self, expr): self.compiledExpr = re.compile(expr) @@ -69,15 +70,15 @@ def remove(self, triple, context=None): if isinstance(subject, REGEXTerm) or \ isinstance(predicate, REGEXTerm) or \ isinstance(object_, REGEXTerm) or \ - (context is not None - and isinstance(context.identifier, REGEXTerm)): + (context is not None and + isinstance(context.identifier, REGEXTerm)): # One or more of the terms is a REGEX expression, so we must # replace it / them with wildcard(s)and match after we query. s = not isinstance(subject, REGEXTerm) and subject or None p = not isinstance(predicate, REGEXTerm) and predicate or None o = not isinstance(object_, REGEXTerm) and object_ or None - c = (context is not None - and not isinstance(context.identifier, REGEXTerm)) \ + c = (context is not None and + not isinstance(context.identifier, REGEXTerm)) \ and context \ or None @@ -100,15 +101,15 @@ def triples(self, triple, context=None): if isinstance(subject, REGEXTerm) or \ isinstance(predicate, REGEXTerm) or \ isinstance(object_, REGEXTerm) or \ - (context is not None - and isinstance(context.identifier, REGEXTerm)): + (context is not None and + isinstance(context.identifier, REGEXTerm)): # One or more of the terms is a REGEX expression, so we must # replace it / them with wildcard(s) and match after we query. s = not isinstance(subject, REGEXTerm) and subject or None p = not isinstance(predicate, REGEXTerm) and predicate or None o = not isinstance(object_, REGEXTerm) and object_ or None - c = (context is not None - and not isinstance(context.identifier, REGEXTerm)) \ + c = (context is not None and + not isinstance(context.identifier, REGEXTerm)) \ and context \ or None for (s1, p1, o1), cg in self.storage.triples((s, p, o), c): diff --git a/rdflib/plugins/stores/sparqlstore.py b/rdflib/plugins/stores/sparqlstore.py index 124f505ac..42e8d2a20 100644 --- a/rdflib/plugins/stores/sparqlstore.py +++ b/rdflib/plugins/stores/sparqlstore.py @@ -27,6 +27,7 @@ BNODE_IDENT_PATTERN = re.compile('(?P