RDFLib / sparqlwrapper

A wrapper for a remote SPARQL endpoint
https://sparqlwrapper.readthedocs.io/
Other
513 stars 121 forks source link

Parameters in SPARQL URI #144

Closed hsolbrig closed 3 years ago

hsolbrig commented 4 years ago

We need to parameterize our URI:

https://graph.fhircat.org/repositories/fhirontology?infer=false

This currently doesn't work because Wrapper.py#L698, Wrapper.py#L711 and Wrapper.py#L719 unconditionally append a '?' to the URI. Propose changing

uri + "?" + self._getRequestEncodedParameters() to uri + ("&" if "?" in uri else "?") + self._getRequestEncodedParameters()

in all 3 places

dayures commented 4 years ago

Did you try the method addParameter?

sparql = SPARQLWrapper("https://graph.fhircat.org/repositories/fhirontology")
sparql.addParameter("infer","false")
hsolbrig commented 4 years ago

Ah - I should have followed the getRequestEncodedParameters further.

This will work in the near term, but it would still be handy if you could slip this change in at a convenient point -- the reason being is that my users often copy a base URL from a SPARQL client. In the near term, I'll tell them that they need to remove the parameters and pass them as as separate command line arguments. In the longer term, we either need the fix above or going to add code to parse the parameters off of the URL and re-insert them.

Just idle musing -- URL parameters are not unlike currying, eh?

Demirrr commented 4 years ago

Hello, I seem to have similar problem. I would like to have query = 'PREFIX dcat: http://www.w3.org/ns/dcat# PREFIX dct: http://purl.org/dc/terms/ SELECT (COUNT(distinct ?s) AS ?num)WHERE{ GRAPH ?g { ?s a dcat:Dataset . ?s dct:description ?o . FILTER isLiteral(?o) FILTER contains(STR(?o), "{keyword}")}}'.format(keyword='Berlin')

However, I could not manage to do so. Is it currently possible to create such queries ? Cheers

dayures commented 4 years ago

@Demirrr coud you share the whole code that you are using?

Demirrr commented 4 years ago
for k in ['Berlin', 'Paderborn']:
    query = 'PREFIX dcat: http://www.w3.org/ns/dcat#' \
            'PREFIX dct: http://purl.org/dc/terms/' \
            'SELECT (COUNT(distinct ?s) AS ?num)WHERE{' \
            'GRAPH ?g { ?s a dcat:Dataset . ?s dct:description ?o . ' \
            'FILTER isLiteral(?o) FILTER contains(STR(?o), {keyword})}}'.format(keyword=k)
    sparql.setQuery(query)
    sparql.setReturnFormat(JSON)
dayures commented 3 years ago

Thanks for contributing to this issue. As it has been more than 90 days since the last activity, we are automatically closing the issue. This is often because the request was already solved in some way and it just wasn't updated or it's no longer applicable. If that's not the case, please do feel free to either reopen this issue or open a new one. We'll gladly take a look again!