Skip to content

Commit

Permalink
Move encode and decode functions to common module.
Browse files Browse the repository at this point in the history
  • Loading branch information
fkdosilovic committed Nov 9, 2024
1 parent b0079eb commit d982f3e
Show file tree
Hide file tree
Showing 4 changed files with 15 additions and 12 deletions.
1 change: 1 addition & 0 deletions src/judge0/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ def resolve_client(
if isinstance(submissions, Submission):
submissions = [submissions]

# TODO: Move to async_execute and sync_execute.
if submissions is not None and len(submissions) == 0:
raise ValueError("Client cannot be determined from empty submissions argument.")

Expand Down
1 change: 1 addition & 0 deletions src/judge0/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ def create_submission(self, submission: Submission) -> Submission:
"wait": "false",
}

# TODO: Rename to_dict to as_body that accepts the client.
body = submission.to_dict()
# We have to resolve language_id because language_id can be Language
# enumeration.
Expand Down
9 changes: 9 additions & 0 deletions src/judge0/common.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from base64 import b64decode, b64encode
from enum import IntEnum


Expand Down Expand Up @@ -30,3 +31,11 @@ class Status(IntEnum):
RUNTIME_ERROR_OTHER = 12
INTERNAL_ERROR = 13
EXEC_FORMAT_ERROR = 14


def encode(text: str) -> str:
return b64encode(bytes(text, "utf-8")).decode()


def decode(b64_encoded_str: str) -> str:
return b64decode(b64_encoded_str.encode()).decode(errors="backslashreplace")
16 changes: 4 additions & 12 deletions src/judge0/submission.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
from base64 import b64decode, b64encode

from typing import Union

from .common import Language, Status
from .common import decode, encode, Language, Status

ENCODED_REQUEST_FIELDS = {
"source_code",
Expand Down Expand Up @@ -48,14 +46,6 @@
SKIP_FIELDS = {"language_id", "language", "status_id"}


def encode(text: str) -> str:
return b64encode(bytes(text, "utf-8")).decode()


def decode(b64_encoded_str: str) -> str:
return b64decode(b64_encoded_str.encode()).decode(errors="backslashreplace")


class Submission:
"""
Stores a representation of a Submission to/from Judge0.
Expand Down Expand Up @@ -138,6 +128,7 @@ def set_attributes(self, attributes):
else:
setattr(self, attr, value)

# TODO: Rename to as_body that accepts a Client.
def to_dict(self) -> dict:
body = {
"source_code": encode(self.source_code),
Expand All @@ -160,4 +151,5 @@ def is_done(self) -> bool:
if self.status is None:
return False
else:
return self.status["id"] not in [Status.IN_QUEUE, Status.PROCESSING]
# TODO: When status is changed to `Status`, refactor this as well.
return self.status["id"] not in (Status.IN_QUEUE, Status.PROCESSING)

0 comments on commit d982f3e

Please sign in to comment.