-
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.
- Loading branch information
Showing
3 changed files
with
68 additions
and
30 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
from typing import List | ||
import logging | ||
import os | ||
|
||
MAX_NB_COLS = 4 | ||
MAX_DISPLAYED_LINES = 5 | ||
|
||
|
||
def parse_api_result(result_filepath: str) -> List[str]: | ||
if not os.path.isfile(result_filepath): | ||
raise IOError(f"could not find the file {result_filepath}") | ||
|
||
# Open the file and read its content. | ||
with open(result_filepath) as f: | ||
content = f.read().splitlines() | ||
|
||
to_display = [] | ||
for line in content[:MAX_DISPLAYED_LINES]: | ||
cols = line.split("\t") | ||
logging.info(cols) | ||
if len(cols) != MAX_NB_COLS: | ||
raise ValueError(f"the file contains {len(cols)} instead of {MAX_NB_COLS}") | ||
line_direction = cols[1] | ||
if len(line_direction) > 8: | ||
line_direction = cols[1][:3] + ".." + cols[1][-3:] | ||
to_append = ( | ||
cols[0] | ||
+ " " | ||
+ line_direction | ||
+ " " | ||
+ cols[2] | ||
+ ("" if cols[3] == "0" else "+" + cols[3]) | ||
) | ||
logging.info(f"appending {to_append}") | ||
to_display.append(to_append) | ||
return to_display |
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 @@ | ||
import unittest | ||
import os | ||
from lib import parse_api_result | ||
|
||
TEST_RESULT_FILENAME = "api_result_test.tsv" | ||
|
||
|
||
class TestParse(unittest.TestCase): | ||
def test_parse_result(self): | ||
test_result_filepath = os.path.join( | ||
os.path.dirname(os.path.dirname(os.path.realpath(__file__))), | ||
"api_fetcher", | ||
TEST_RESULT_FILENAME, | ||
) | ||
expected = [ | ||
"6 Gen..age 10:46+1", | ||
"3 Gra..tti 10:46+1", | ||
"9 Ver..urs 10:46", | ||
"6 Ver..age 10:47", | ||
"10 Gen..ive 10:47", | ||
] | ||
self.assertEqual(parse_api_result(test_result_filepath), expected) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |