Skip to content

Commit d982f3e

Browse files
committed
Move encode and decode functions to common module.
1 parent b0079eb commit d982f3e

File tree

4 files changed

+15
-12
lines changed

4 files changed

+15
-12
lines changed

src/judge0/api.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ def resolve_client(
2828
if isinstance(submissions, Submission):
2929
submissions = [submissions]
3030

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

src/judge0/clients.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ def create_submission(self, submission: Submission) -> Submission:
8787
"wait": "false",
8888
}
8989

90+
# TODO: Rename to_dict to as_body that accepts the client.
9091
body = submission.to_dict()
9192
# We have to resolve language_id because language_id can be Language
9293
# enumeration.

src/judge0/common.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
from base64 import b64decode, b64encode
12
from enum import IntEnum
23

34

@@ -30,3 +31,11 @@ class Status(IntEnum):
3031
RUNTIME_ERROR_OTHER = 12
3132
INTERNAL_ERROR = 13
3233
EXEC_FORMAT_ERROR = 14
34+
35+
36+
def encode(text: str) -> str:
37+
return b64encode(bytes(text, "utf-8")).decode()
38+
39+
40+
def decode(b64_encoded_str: str) -> str:
41+
return b64decode(b64_encoded_str.encode()).decode(errors="backslashreplace")

src/judge0/submission.py

Lines changed: 4 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
from base64 import b64decode, b64encode
2-
31
from typing import Union
42

5-
from .common import Language, Status
3+
from .common import decode, encode, Language, Status
64

75
ENCODED_REQUEST_FIELDS = {
86
"source_code",
@@ -48,14 +46,6 @@
4846
SKIP_FIELDS = {"language_id", "language", "status_id"}
4947

5048

51-
def encode(text: str) -> str:
52-
return b64encode(bytes(text, "utf-8")).decode()
53-
54-
55-
def decode(b64_encoded_str: str) -> str:
56-
return b64decode(b64_encoded_str.encode()).decode(errors="backslashreplace")
57-
58-
5949
class Submission:
6050
"""
6151
Stores a representation of a Submission to/from Judge0.
@@ -138,6 +128,7 @@ def set_attributes(self, attributes):
138128
else:
139129
setattr(self, attr, value)
140130

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

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy