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

Add agent and updateEndpoint to SPARQLWrapper2 (fixes #162) #225

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions SPARQLWrapper/SmartWrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ class SPARQLWrapper2(SW):
query result is automatically set to a :class:`Bindings` instance. Makes the average query processing a bit
simpler..."""

def __init__(self, baseURI: str, defaultGraph: Optional[str] = None):
def __init__(self, baseURI: str, defaultGraph: Optional[str] = None, updateEndpoint: Optional[str] = None, agent: Optional[str] = None):
"""
Class encapsulating a full SPARQL call. In contrast to the :class:`~SPARQLWrapper.Wrapper.SPARQLWrapper`
superclass, the return format cannot be set (it is defaulted to
Expand All @@ -309,9 +309,15 @@ def __init__(self, baseURI: str, defaultGraph: Optional[str] = None):
:type baseURI: string
:param defaultGraph: URI for the default graph. Default is ``None``, can be set via an explicit call, too.
:type defaultGraph: string
:param updateEndpoint: SPARQL endpoint's URI for update operations (if it's a different one). The **default**
value is ``None``.
:type updateEndpoint: string
:param agent: The User-Agent for the HTTP request header. The **default** value is an autogenerated string
using the SPARQLWrapper version number.
:type agent: string
"""
super(SPARQLWrapper2, self).__init__(
baseURI, returnFormat=JSON, defaultGraph=defaultGraph
baseURI, returnFormat=JSON, defaultGraph=defaultGraph, updateEndpoint=updateEndpoint, agent=agent
)

def setReturnFormat(self, format: Optional[str]) -> None:
Expand Down
4 changes: 2 additions & 2 deletions SPARQLWrapper/Wrapper.py
Original file line number Diff line number Diff line change
Expand Up @@ -276,7 +276,7 @@ def __init__(
updateEndpoint: Optional[str] = None,
returnFormat: str = XML,
defaultGraph: Optional[str] = None,
agent: str = __agent__,
agent: str = None,
) -> None:
"""
Class encapsulating a full SPARQL call.
Expand All @@ -303,7 +303,7 @@ def __init__(
"""
self.endpoint = endpoint
self.updateEndpoint = updateEndpoint if updateEndpoint else endpoint
self.agent = agent
self.agent = agent if agent else __agent__
self.user: Optional[str] = None
self.passwd: Optional[str] = None
self.http_auth = BASIC
Expand Down
14 changes: 7 additions & 7 deletions SPARQLWrapper/sparql_dataframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
Query a SPARQL endpoint and return results as a Pandas dataframe.
"""
import io
from typing import TYPE_CHECKING, Any, Dict, List, Union
from typing import TYPE_CHECKING, Any, Dict, List, Optional, Union

from SPARQLWrapper.SmartWrapper import Bindings, SPARQLWrapper2, Value
from SPARQLWrapper.Wrapper import CSV, SELECT, SPARQLWrapper
Expand All @@ -16,13 +16,13 @@ class QueryException(Exception):


def get_sparql_dataframe_orig(
endpoint: str, query: Union[str, bytes]
endpoint: str, query: Union[str, bytes], agent: Optional[str] = None
) -> "pd.DataFrame":
"""copy paste from: https://github.com/lawlesst/sparql-dataframe"""
# pandas inside to avoid requiring it
import pandas as pd

sparql = SPARQLWrapper(endpoint)
sparql = SPARQLWrapper(endpoint, agent=agent)
sparql.setQuery(query)
if sparql.queryType != SELECT:
raise QueryException("Only SPARQL SELECT queries are supported.")
Expand All @@ -36,14 +36,14 @@ def get_sparql_dataframe_orig(


def get_sparql_typed_dict(
endpoint: str, query: Union[str, bytes]
endpoint: str, query: Union[str, bytes], agent: Optional[str] = None
) -> List[Dict[str, Value]]:
"""modified from: https://github.com/lawlesst/sparql-dataframe"""
# pandas inside to avoid requiring it
import pandas as pd
# rdflib in here because there is some meta stuff in the setup.py and Travis fails because rdflib is installed later
import rdflib.term
sparql = SPARQLWrapper2(endpoint)
sparql = SPARQLWrapper2(endpoint, agent=agent)
sparql.setQuery(query)
if sparql.queryType != SELECT:
raise QueryException("Only SPARQL SELECT queries are supported.")
Expand All @@ -64,11 +64,11 @@ def get_sparql_typed_dict(
return d


def get_sparql_dataframe(endpoint: str, query: Union[str, bytes]) -> "pd.DataFrame":
def get_sparql_dataframe(endpoint: str, query: Union[str, bytes], agent: Optional[str] = None) -> "pd.DataFrame":
# pandas inside to avoid requiring it
import pandas as pd

d = get_sparql_typed_dict(endpoint, query)
d = get_sparql_typed_dict(endpoint, query, agent)
# TODO: will nan fill somehow, make more strict if there is way of getting the nan types from rdflib
df = pd.DataFrame(d)
return df