-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #79 from tskir/eva-1700-trait-mapping-fixes
EVA-1700 — fixes to trait mapping pipeline
- Loading branch information
Showing
11 changed files
with
57 additions
and
76 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import logging | ||
logging.basicConfig() | ||
logger = logging.getLogger(__package__) | ||
logger.setLevel(level=logging.INFO) | ||
logger.setLevel(level=logging.DEBUG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
import logging | ||
logging.basicConfig() | ||
logger = logging.getLogger(__package__) | ||
logger.setLevel(level=logging.INFO) | ||
logger.setLevel(level=logging.DEBUG) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,37 +1,14 @@ | ||
import logging | ||
import requests | ||
from retry import retry | ||
logger = logging.getLogger(__package__) | ||
|
||
|
||
def json_request(url: str, payload: dict) -> dict: | ||
"""Makes a GET request with the specified URL and payload, attempts to parse the result as a JSON string and | ||
return it as a dictionary, on failure raises an exception.""" | ||
result = requests.get(url, data=payload) | ||
assert result.ok | ||
@retry(exceptions=(ConnectionError, requests.RequestException), logger=logger, | ||
tries=4, delay=2, backoff=1.2, jitter=(1, 3)) | ||
def json_request(url: str, payload: dict = None, method=requests.get) -> dict: | ||
"""Makes a request of a specified type (by default GET) with the specified URL and payload, attempts to parse the | ||
result as a JSON string and return it as a dictionary, on failure raises an exception.""" | ||
result = method(url, data=payload) | ||
result.raise_for_status() | ||
return result.json() | ||
|
||
|
||
def retry_helper(function, kwargs: dict, retry_count: int): | ||
""" | ||
Given a function, make a `retry_count` number of attempts to call it until it returns a value without raising | ||
an exception, and subsequently return this value. If all attempts to run the function are unsuccessful, return None. | ||
:param function: Function that could need multiple attempts to return a value | ||
:param kwargs: Dictionary with function's keyword arguments | ||
:param retry_count: Number of attempts to make | ||
:return: Returned value of the function. | ||
""" | ||
for retry_num in range(retry_count): | ||
try: | ||
return function(**kwargs) | ||
except Exception as e: | ||
logger.warning("Attempt {}: failed running function {} with kwargs {}".format(retry_num, function, kwargs)) | ||
logger.warning(e) | ||
logger.warning("Error on last attempt, skipping") | ||
return None | ||
|
||
|
||
def request_retry_helper(url: str, payload: dict = None, retry_count: int = 4): | ||
"""Makes a GET request with the specified URL and payload via `json_request`, makes several attempts and handles | ||
the exceptions via `retry_helper`.""" | ||
return retry_helper(json_request, {'url': url, 'payload': payload}, retry_count) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters