From d982f3e4770f554bb148aa20f63e408ddd333a88 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Filip=20Karlo=20Do=C5=A1ilovi=C4=87?= Date: Sat, 9 Nov 2024 16:42:26 +0100 Subject: [PATCH] Move encode and decode functions to common module. --- src/judge0/api.py | 1 + src/judge0/clients.py | 1 + src/judge0/common.py | 9 +++++++++ src/judge0/submission.py | 16 ++++------------ 4 files changed, 15 insertions(+), 12 deletions(-) diff --git a/src/judge0/api.py b/src/judge0/api.py index 4a0e868..ed75192 100644 --- a/src/judge0/api.py +++ b/src/judge0/api.py @@ -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.") diff --git a/src/judge0/clients.py b/src/judge0/clients.py index 35cd25f..a9f25ce 100644 --- a/src/judge0/clients.py +++ b/src/judge0/clients.py @@ -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. diff --git a/src/judge0/common.py b/src/judge0/common.py index edd2eaa..13e568f 100644 --- a/src/judge0/common.py +++ b/src/judge0/common.py @@ -1,3 +1,4 @@ +from base64 import b64decode, b64encode from enum import IntEnum @@ -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") diff --git a/src/judge0/submission.py b/src/judge0/submission.py index 19092aa..229d42f 100644 --- a/src/judge0/submission.py +++ b/src/judge0/submission.py @@ -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", @@ -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. @@ -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), @@ -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)