Skip to content

Commit

Permalink
#406: refactor TopcoderLongContestProblem.download_individual_results…
Browse files Browse the repository at this point in the history
…_feed
  • Loading branch information
kmyk committed Mar 30, 2019
1 parent 1b68404 commit 6e50c51
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions onlinejudge/service/topcoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -306,26 +306,30 @@ def download_individual_results_feed(self, cr: int, session: Optional[requests.S
resp = utils.request('GET', url, session=session)

# parse
def get_text_at(node: xml.etree.ElementTree.Element, i: int) -> str:
text = list(node)[i].text
if text is None:
raise ValueError
return text

root = xml.etree.ElementTree.fromstring(resp.content.decode(resp.encoding))
assert len(list(root)) == 5
round_id = int(list(root)[0].text or '') # NOTE: `or ''` is for the typechecker
coder_id = int(list(root)[1].text or '')
handle = list(root)[2].text
assert handle is not None
round_id = int(get_text_at(root, 0)) # NOTE: `or ''` is for the typechecker
coder_id = int(get_text_at(root, 1))
handle = get_text_at(root, 2)
submissions = [] # type: List[TopcoderLongContestProblemIndividualResultsFeedSubmission]
for submission in list(root)[3]:
number = int(list(submission)[0].text or '')
score = float(list(submission)[1].text or '')
language = list(submission)[2].text or ''
time = list(submission)[3].text
assert time is not None
number = int(get_text_at(submission, 0))
score = float(get_text_at(submission, 1))
language = get_text_at(submission, 2)
time = get_text_at(submission, 3)
submissions += [TopcoderLongContestProblemIndividualResultsFeedSubmission(number, score, language, time)]
testcases = [] # type: List[TopcoderLongContestProblemIndividualResultsFeedTestCase]
for testcase in list(root)[4]:
test_case_id = int(list(testcase)[0].text or '')
score = float(list(testcase)[1].text or '')
processing_time = int(list(testcase)[2].text or '')
fatal_error_ind = int(list(testcase)[3].text or '')
test_case_id = int(get_text_at(testcase, 0))
score = float(get_text_at(testcase, 1))
processing_time = int(get_text_at(testcase, 2))
fatal_error_ind = int(get_text_at(testcase, 3))
testcases += [TopcoderLongContestProblemIndividualResultsFeedTestCase(test_case_id, score, processing_time, fatal_error_ind)]
return TopcoderLongContestProblemIndividualResultsFeed(round_id, coder_id, handle, submissions, testcases)

Expand Down

0 comments on commit 6e50c51

Please sign in to comment.