Replies: 10 comments
-
I think @gromgull had something similar he once showed to me... I think overall there were several attempts to be able to write SPARQL queries in a more pythonic way already, but never really followed through and made it "feature complete", so I'd say contribs always welcome... Some thoughts: I'd probably still user longer names (users can shorten them via from rdflib import *
from rdflib.query_builder import QueryBuilder, FILTER, SUM
Var = Variable
tim = URIRef("http://www.w3.org/People/Berners-Lee/card#i")
qb = QueryBuilder().SELECT(
middle_aged_friend=Var('s'),
some_sum=SUM(VAR('age')),
).WHERE(
(tim, FOAF.knows, Var('s')),
(tim, FOAF.age, Var('age')),
FILTER(Literal(35) < Var('age')), # this will probably be the most challenging
FILTER(Var('age') < Literal(65)),
).LIMIT(10) I'd recommend not to use Also, And last but not least, implementing all Thinking about especially the last point, the SPARQL parser, algebra and operators come to mind... maybe parts of that can be re-used... |
Beta Was this translation helpful? Give feedback.
-
Agreed on both points 😄.
Yeah I took, the example from telescope, but am not a fan of such obscure metaprogramming either 😅. I'd also go with
Yeah, but the tradeoff seems fair, also the UPPERCASE notation makes the query stand out somewhat nicely from the rest of the code.
I would probably banish everything related to the query builder into it's own namespace, as this causes the least confusion and makes autocomplete a lot cleaner. The problem is both ways, not only would operators collide with existing methods, but exposing the internal machinery to IDE autocompletion also seems like a unclean solution. Users should be able to focus on building their queries, and every suggested completion should produce at least a valid query part (of course the whole thing might still be syntactically inconsistent but that doesn't seem solvable).
Yeah I was hoping of just generating some intermediate representation between the parser and the query->algebra compiler, as of now that does require generating pyparsing parse trees, as it seems. Thanks a lot for the feedback and time! 😊 |
Beta Was this translation helpful? Give feedback.
-
Ok I thought about this a bit more. Thoughts? |
Beta Was this translation helpful? Give feedback.
-
Did you have in mind something similar to this: https://github.com/sbg/sparqb ? The team at Seven Bridges (http://sevenbridges.com) that I'm a part of has been working on this library for a while now (still in alpha stage though) that we use internally for our projects. Among some additional features, that aren't pushed to this repo but that are developed, is to use RDFLib Terms when constructing queries. Currently, we only serialize query objects to SPARQL but additional serialization options shouldn't be hard to implement. We also use RDFLib extensively so maybe we can combine our efforts? |
Beta Was this translation helpful? Give feedback.
-
I use this in several projects : https://gist.github.com/gromgull/70e2d0500fbcb820dd99 it's not really complete enough to go into RDFLib yet - but could easily be |
Beta Was this translation helpful? Give feedback.
-
I think I would try to build something that is maybe a bit more complex but modeling the syntax of SPARQL more closely? It would be really nice if the user was guided by IDE autocompletion to write queries incrementally and correctly. It would be interesting to compare both existing libs in that regard. My main use case is didactial, it's somewhat hard to get people to learn SPARQL outside of the SemWeb community especially when it comes to embedded stuff, so making it as easy as possible would be amazing. |
Beta Was this translation helpful? Give feedback.
-
My main purpose is avoiding stupid typos, since you can construct the query from RDFLib terms, you can use a Of course it's also nice that it's now python code, so it indents nicely etc. And it's a bit more compact than SPARQL strings (but not much) |
Beta Was this translation helpful? Give feedback.
-
I have started working on this issue along with @Arshdeep25 and @saksham16085. Functions added:
Can you please help us by clarifying other expectations for the same. |
Beta Was this translation helpful? Give feedback.
-
Hi @anubhavj99 , I was looking for exactly this - awesome! I am playing around with it and a question came to my mind: Why is there a class
|
Beta Was this translation helpful? Give feedback.
-
Wow! Exactly what I was looking for. @anubhavj99 Do you know what's the status? |
Beta Was this translation helpful? Give feedback.
-
Hey,
I'd be very interested to know if people have already considered creating a query builder API?
I think it would help adoption if there was a convenient way to programatically build queries from within python in a LINQ kinda way.
Having such a DSL would not only allow for quicker generation of non prepared queries, as the entire parsing step would be omitted, it could also provide better tool support, as IDE's like pycharm would be capable of autocompleting and type checking queries.
I'd imagine something in the style of.
Which could further be simplified to the following, due to the first class nature of queries and their components.
I'd be more than happy to start working on this 😄.
Thoughts, suggestions, comments?
Beta Was this translation helpful? Give feedback.
All reactions