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

Update checkquery.py #64

Merged
merged 4 commits into from
Feb 22, 2024
Merged
Changes from 1 commit
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
253 changes: 184 additions & 69 deletions src/scribe_data/checkquery.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,19 @@ class QueryFile:
def load(self, limit: int) -> str:
"""Load the SPARQL query from 'path' into a string.

Args:
limit (int): the maximum number of results a query should return.
Parameters
Jk40git marked this conversation as resolved.
Show resolved Hide resolved
-----------
source_value : str
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Returns:
str: the SPARQL query.
The 'target' value given the passed arguments.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We want this here to be the same as it was before, but with a different format for the Returns:. So ideally:

Returns
--------
    str : the SPARQL query.

"""
with open(self.path, encoding="utf-8") as in_stream:
return f"{in_stream.read()}\nLIMIT {limit}\n"
Expand All @@ -60,9 +68,16 @@ class QueryExecutionException(Exception):

def __init__(self, message: str, query: QueryFile) -> None:
"""
Args:
message (str): why the query failed.
query (QueryFile): the query that failed.
Parameters
-----------
source_value : str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need message and query, the arguments of the function.

The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.
"""
self.message = message
self.query = query
Expand All @@ -75,13 +90,21 @@ def __str__(self) -> str:
def ping(url: str, timeout: int) -> bool:
"""
Test if a URL is reachable.

Args:
url (str): the URL to test.
timeout (int): the maximum number of seconds to wait for a reply.

Returns:
bool: True if connectivity established. False otherwise.
Parameters
-----------
source_value : str
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need url and timeout.

The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.


Returns
--------
The 'target' value given the passed arguments.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need this to be bool : True if connectivity established. False otherwise..

"""
try:
with urllib.request.urlopen(url, timeout=timeout) as response:
Expand Down Expand Up @@ -159,12 +182,22 @@ def sparql_context(url: str) -> SPARQL.SPARQLWrapper:
Configure a SPARQL context.

A context allows the execution of SPARQL queries.

Args:
url (str): a valid URL of a SPARQL endpoint.

Returns:
SPARQLWrapper: the context.
Parameters
-----------
source_value : str
Jk40git marked this conversation as resolved.
Show resolved Hide resolved
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Returns
--------
The 'target' value given the passed arguments.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here we need SPARQLWrapper : the context. as that's the return value for this function.

"""

"""
context = SPARQL.SPARQLWrapper(url)
context.setReturnFormat(SPARQL.JSON)
Expand All @@ -179,15 +212,20 @@ def execute(
"""
Execute a SPARQL query in a given context.

Args:
query (QueryFile): the SPARQL query to run.
limit (int): the maximum number of results a query should return.
context (SPARQLWrapper): the SPARQL context.
tries (int): the maximum number of times the query should be executed
after failure.

Returns:
dict: results of the query.
Parameters
-----------
source_value : str
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Returns
--------
The 'target' value given the passed arguments.
"""

def delay_in_seconds() -> int:
Expand Down Expand Up @@ -218,11 +256,20 @@ def check_sparql_file(fpath: str) -> Path:
"""
Check meta information of SPARQL query file.

Args:
fpath (str): the file to validate.

Returns:
Path: the validated file.
Parameters
-----------
source_value : str
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Returns
--------
The 'target' value given the passed arguments.
"""
path = Path(fpath)

Expand All @@ -239,15 +286,24 @@ def check_positive_int(value: str, err_msg: str) -> int:
"""
Ensure 'value' is a positive number.

Args:
value (str): the value to be validated.
err_msg (str): used when value fails validation.

Raises:
argparse.ArgumentTypeError

Returns:
int: the validated number.
Parameters
-----------
source_value : str
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Raises
-------
ValueError : when a source_value is not supported.

Returns
--------
The 'target' value given the passed arguments.
"""
try:
number = int(value)
Expand All @@ -263,14 +319,24 @@ def check_limit(limit: str) -> int:
"""
Validate the 'limit' argument.

Args:
limit (str): the LIMIT to be validated.

Raises:
argparse.ArgumentTypeError

Returns:
int: the validated LIMIT
Parameters
-----------
source_value : str
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Raises
-------
ValueError : when a source_value is not supported.

Returns
--------
The 'target' value given the passed arguments.
"""
return check_positive_int(limit, "LIMIT must be an integer of value 1 or greater.")

Expand All @@ -279,14 +345,24 @@ def check_timeout(timeout: str) -> int:
"""
Validate the 'timeout' argument.

Args:
timeout (str): the timeout to be validated.

Raises:
argparse.ArgumentTypeError

Returns:
int: the validated timeout.
Parameters
-----------
source_value : str
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Raises
-------
ValueError : when a source_value is not supported.

Returns
--------
The 'target' value given the passed arguments.
"""
return check_positive_int(
timeout, "timeout must be an integer of value 1 or greater."
Expand All @@ -296,12 +372,20 @@ def check_timeout(timeout: str) -> int:
def main(argv=None) -> int:
"""
The main function.

Args:
argv : If set to None then argparse will use sys.argv as the arguments.

Returns:
int: the exit status - 0: success, any other value - failure.
Parameters
-----------
source_value : str
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Returns
--------
The 'target' value given the passed arguments.
"""
cli = argparse.ArgumentParser(
description=f"run SPARQL queries from the '{PROJECT_ROOT}' project",
Expand Down Expand Up @@ -424,8 +508,24 @@ def error_report(failures: list[QueryExecutionException]) -> None:
"""
Report failed queries.

Args:
failures (list[QueryExecutionException]): failed queries.
Parameters
-----------
source_value : str
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Raises
-------
ValueError : when a source_value is not supported.

Returns
--------
The 'target' value given the passed arguments.
"""
if not failures:
return
Expand All @@ -440,9 +540,24 @@ def success_report(successes: list[tuple[QueryFile, dict]], display: bool) -> No
"""
Report successful queries.

Args:
successes (list[tuple[QueryFile, dict]]): successful queries.
display (bool): should there be output?
Parameters
-----------
source_value : str
The source value to find equivalents for (e.g. 'english').
source_key : str
The source key to reference (e.g. 'language').
target_key : str
The key to target (e.g. 'iso').
error_msg : str
The message displayed when a value cannot be found.

Raises
-------
ValueError : when a source_value is not supported.

Returns
--------
The 'target' value given the passed arguments.
"""
if not (display and successes):
return
Expand Down
Loading