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

rdflib under python3 #617

Closed
pfps opened this issue Apr 9, 2016 · 8 comments
Closed

rdflib under python3 #617

pfps opened this issue Apr 9, 2016 · 8 comments
Labels
bug Something isn't working help wanted Extra attention is needed SPARQL
Milestone

Comments

@pfps
Copy link

pfps commented Apr 9, 2016

Is rdflib supposed to work in python3?

I´m getting one of the canonical non-compatability errors when I run its SPARQL implementation in python3.

Here is an edited version of what I am seeing, with some test printout included:

...
SORTING [((2, -4, True), (rdflib.term.Variable('this'), rdflib.term.URIRef('http://www.w3.org/ns/shacl#p1'), rdflib.term.Variable('value1'))), 
     ((2, -4, True), (rdflib.term.Variable('this'), Path(http://www.w3.org/ns/shacl#p2 / http://www.w3.org/ns/shacl#p2), rdflib.term.Variable('value1')))]
Traceback (most recent call last):
...
  File "/usr/lib/python3.4/site-packages/rdflib/graph.py", line 1084, in query
    query_object, initBindings, initNs, **kwargs))
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/processor.py", line 73, in query
    query = translateQuery(parsetree, base, initNs)
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 746, in translateQuery
    P, PV = translate(q[1])
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 535, in translate
    M = translateGroupGraphPattern(q.where)
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 295, in translateGroupGraphPattern
    G = Join(p1=G, p2=translateGroupOrUnionGraphPattern(p))
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 244, in translateGroupOrUnionGraphPattern
    g = translateGroupGraphPattern(g)
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 267, in translateGroupGraphPattern
    return ToMultiSet(translate(graphPattern)[0])
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 535, in translate
    M = translateGroupGraphPattern(q.where)
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 280, in translateGroupGraphPattern
    g[-1]["triples"] += triples(p.triples)
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 133, in triples
    for x in range(0, len(l), 3))
  File "/usr/lib/python3.4/site-packages/rdflib/plugins/sparql/algebra.py", line 115, in reorderTriples
    1], varsknown, varscount), x[1]) for x in l[i:])
TypeError: unorderable types: SequencePath() < URIRef()

The query includes SPARQL path expressions and these seem to be involved in
the comparison.

@joernhees
Copy link
Member

yupp, rdflib is meant to work in py3...

can you provide a few lines of code that lead to that error, so i could try to reproduce with the latest dev version?

@joernhees joernhees added bug Something isn't working SPARQL labels Apr 10, 2016
@joernhees joernhees added this to the rdflib 4.2.2 milestone Apr 10, 2016
@pfps
Copy link
Author

pfps commented Apr 11, 2016

OK. Here is a small file. The first two queries are OK but the last two fail. It appears that the culprit is trying to sort sequence paths so the solution is going to be to implement sorting for them. The error that I get is along the lines of
TypeError: unorderable types: SequencePath() < URIRef()

[I'm again having problems uploading.]

@pfps
Copy link
Author

pfps commented Apr 11, 2016

Here is the file, as I can't seem to upload.

import rdflib

g = rdflib.Graph()

print("QUERY 0")
query = """
    SELECT  ?this
    WHERE {
          ?this <http://www.w3.org/ns#p2> ?value1 .
          ?this <http://www.w3.org/ns#p2> ?value1 .
       }
  """
g.query(query)

print("QUERY 1")
query = """
    SELECT  ?this
    WHERE {
          ?this <http://www.w3.org/ns#p1>/<http://www.w3.org/ns#p2> ?value1 .
       }
  """
g.query(query)

print("QUERY 2")
query = """
    SELECT  ?this
    WHERE {
          ?this <http://www.w3.org/ns#p1> ?value1 . 
          ?this <http://www.w3.org/ns#p2>/<http://www.w3.org/ns#p2> ?value1 .
       }
  """
g.query(query)

print("QUERY 3")
query = """
    SELECT  ?this
    WHERE {
          ?this <http://www.w3.org/ns#p2>/<http://www.w3.org/ns#p2> ?value1 .
          ?this <http://www.w3.org/ns#p1>/<http://www.w3.org/ns#p2> ?value1 .
       }
  """
g.query(query)

@joernhees joernhees added the help wanted Extra attention is needed label May 10, 2016
@joernhees
Copy link
Member

as an alternative to uploading files, just copy & paste short python code snippets into a blocks like this in future (looks better and is easier to access anyhow):

```python
your code here
```

i labelled your reports and reformatted them, so they won't be forgotten... if someone is less busy and searching for some in-depth SPARQL knowledge: dive in ;)

@pjohnston-wiley
Copy link

It's just because rdflib.paths.SequencePath doesn't support sort comparisons. You can make this work in your own code by inserting the following (mickey mouse sort by string comparison):

# Egregious hack, the SequencePath object doesn't support compare, this implements the __lt__ method so that algebra.py works on sorting in SPARQL queries on e.g. rdf:List paths
def sequencePathCompareLt(self, other):
    return str(self) < str(other)

def sequencePathCompareGt(self, other):
    return str(self) < str(other)

setattr(SequencePath, '__lt__', sequencePathCompareLt)
setattr(SequencePath, '__gt__', sequencePathCompareGt)
# End egregious hack

It's not particularly meaningful other than making code work, but it could go into sequence.py as:

class SequencePath(Path):
...
  def __lt__ (self, other):
    return str(self) < str(other)
  def __gt__ (self, other):
    return str(self) > str(other)
...

I am sure there is a more intelligent sort to implement, but this will work.

@gromgull
Copy link
Member

related: #653

not sure why 57e8f71 / 25bfa5b did fix :(

@gromgull
Copy link
Member

actually - for me the code in #617 (comment) runs without error on HEAD.

@gromgull
Copy link
Member

I'm going to @pjohnston-wiley used 4.2.1 where it's broken, and it's now fixed in master :)

reopen if I'm wrong!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted Extra attention is needed SPARQL
Projects
None yet
Development

No branches or pull requests

4 participants