Skip to content

Commit

Permalink
Add type annotations for fields. Add single field (str) as option to …
Browse files Browse the repository at this point in the history
…fields parameter.
  • Loading branch information
fkdosilovic committed Nov 3, 2024
1 parent b27b46e commit 428ac30
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 11 deletions.
62 changes: 52 additions & 10 deletions src/judge0/clients.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from typing import Iterable, Union

import requests

from .submission import Submission
Expand Down Expand Up @@ -76,15 +78,25 @@ def create_submission(self, submission: Submission) -> None:

submission.set_attributes(resp.json())

def get_submission(self, submission: Submission, *, fields=None) -> None:
def get_submission(
self,
submission: Submission,
*,
fields: Union[str, Iterable[str], None] = None,
) -> None:
"""Check the submission status."""

params = {
"base64_encoded": "true",
}

if isinstance(fields, str):
fields = [fields]

if fields is not None:
params["fields"] = ",".join(fields)
else:
params["fields"] = "*"

resp = requests.get(
f"{self.endpoint}/submissions/{submission.token}",
Expand Down Expand Up @@ -117,15 +129,25 @@ def create_submissions(self, submissions: list[Submission]) -> None:
for submission, attrs in zip(submissions, resp.json()):
submission.set_attributes(attrs)

def get_submissions(self, submissions: list[Submission], *, fields=None) -> None:
def get_submissions(
self,
submissions: list[Submission],
*,
fields: Union[str, Iterable[str], None] = None,
) -> None:
params = {
"base64_encoded": "true",
}

if isinstance(fields, str):
fields = [fields]

if fields is not None:
params["fields"] = ",".join(fields)
else:
params["fields"] = "*"

tokens = ",".join((submission.token for submission in submissions))
tokens = ",".join(submission.token for submission in submissions)
params["tokens"] = tokens

resp = requests.get(
Expand Down Expand Up @@ -199,15 +221,25 @@ def create_submission(self, submission: Submission) -> None:
self._update_endpoint_header(self.DEFAULT_CREATE_SUBMISSION_ENDPOINT)
return super().create_submission(submission)

def get_submission(self, submission: Submission, *, fields=None) -> None:
def get_submission(
self,
submission: Submission,
*,
fields: Union[str, Iterable[str], None] = None,
) -> None:
self._update_endpoint_header(self.DEFAULT_GET_SUBMISSION_ENDPOINT)
return super().get_submission(submission, fields=fields)

def create_submissions(self, submissions: list[Submission]) -> None:
self._update_endpoint_header(self.DEFAULT_CREATE_SUBMISSIONS_ENDPOINT)
return super().create_submissions(submissions)

def get_submissions(self, submissions: list[Submission], *, fields=None) -> None:
def get_submissions(
self,
submissions: list[Submission],
*,
fields: Union[str, Iterable[str], None] = None,
) -> None:
self._update_endpoint_header(self.DEFAULT_GET_SUBMISSIONS_ENDPOINT)
return super().get_submissions(submissions, fields=fields)

Expand Down Expand Up @@ -257,17 +289,27 @@ def create_submission(self, submission: Submission):
self._update_endpoint_header(self.DEFAULT_CREATE_SUBMISSION_ENDPOINT)
return super().create_submission(submission)

def get_submission(self, submission: Submission, *, fields=None):
def get_submission(
self,
submission: Submission,
*,
fields: Union[str, Iterable[str], None] = None,
):
self._update_endpoint_header(self.DEFAULT_GET_SUBMISSION_ENDPOINT)
return super().get_submission(submission, fields=fields)

def create_submissions(self, submission: list[Submission]) -> None:
def create_submissions(self, submissions: list[Submission]) -> None:
self._update_endpoint_header(self.DEFAULT_CREATE_SUBMISSIONS_ENDPOINT)
return super().create_submissions(submission)
return super().create_submissions(submissions)

def get_submissions(self, submission: list[Submission], *, fields=None) -> None:
def get_submissions(
self,
submissions: list[Submission],
*,
fields: Union[str, Iterable[str], None] = None,
) -> None:
self._update_endpoint_header(self.DEFAULT_GET_SUBMISSIONS_ENDPOINT)
return super().get_submissions(submission, fields=fields)
return super().get_submissions(submissions, fields=fields)


class Rapid(Client):
Expand Down
2 changes: 1 addition & 1 deletion src/judge0/submission.py
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ def __init__(
self.status = None
self.created_at = None
self.finished_at = None
self.token = None
self.token = ""
self.time = None
self.wall_time = None
self.memory = None
Expand Down

0 comments on commit 428ac30

Please sign in to comment.