-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: Add conv_response_list utility function
- Loading branch information
Showing
8 changed files
with
167 additions
and
48 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,20 +1,22 @@ | ||
from .nbia_endpoints import NBIA_ENDPOINTS, NBIA_BASE_URLS | ||
from .nbia_endpoints import NBIA_ENDPOINTS, NBIA_BASE_URLS, ReturnType | ||
from .md5 import validateMD5 | ||
from .parsers import ( | ||
convertMillis, | ||
clean_html, | ||
convertDateFormat, | ||
parse_response, | ||
ReturnType, | ||
) | ||
|
||
from .conv_response_list import conv_response_list | ||
|
||
__all__ = [ | ||
"NBIA_ENDPOINTS", | ||
"NBIA_BASE_URLS", | ||
"validateMD5", | ||
"convertMillis", | ||
"clean_html", | ||
"convertDateFormat", | ||
"conv_response_list", | ||
"parse_response", | ||
"ReturnType", | ||
] |
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 |
---|---|---|
@@ -0,0 +1,26 @@ | ||
from typing import Any, List | ||
from .nbia_endpoints import ReturnType | ||
import pandas as pd | ||
|
||
|
||
# function that takes a list of dictionaries and returns either a list or a dataframe | ||
def conv_response_list( | ||
response_json: List[dict[Any, Any]], | ||
return_type: ReturnType, | ||
) -> List[dict[Any, Any]] | pd.DataFrame: | ||
"""Function that takes in a list of dictionaries and returns either a list or a dataframe. | ||
:param response_json: A response from the API in the form of a list of dictionaries. | ||
:type response_json: List[dict[Any, Any]] | ||
:param return_type: The desired return type for the response. | ||
:type return_type: ReturnType | ||
:return: Either a list of dictionaries or a DataFrame. | ||
:rtype: List[dict[Any, Any]] | pd.DataFrame | ||
""" | ||
|
||
assert isinstance(response_json, list), "The response JSON must be a list" | ||
|
||
if return_type == ReturnType.LIST: | ||
return response_json | ||
elif return_type == ReturnType.DATAFRAME: | ||
return pd.DataFrame(data=response_json) |
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import pytest | ||
|
||
from src.nbiatoolkit import NBIAClient | ||
from src.nbiatoolkit.utils import * | ||
import pandas as pd | ||
|
||
|
||
@pytest.fixture(scope="session") | ||
def nbia_client(): | ||
return NBIAClient() | ||
|
||
|
||
def test_getModalityValues(nbia_client: NBIAClient): | ||
modality_values = nbia_client.getModalityValues() | ||
|
||
assert isinstance(modality_values, list) | ||
assert len(modality_values) > 0 | ||
assert isinstance(modality_values[0], dict) | ||
assert len(modality_values[0].keys()) == 1 | ||
|
||
assert isinstance(modality_values[0]["Modality"], str) | ||
|
||
|
||
def test_getModalityValuesCounts(nbia_client: NBIAClient): | ||
modality_values_counts = nbia_client.getModalityValues(Counts=True) | ||
|
||
assert isinstance(modality_values_counts, list) | ||
assert len(modality_values_counts) > 0 | ||
assert isinstance(modality_values_counts[0], dict) | ||
assert len(modality_values_counts[0].keys()) == 2 |
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