Skip to content

Commit a3eed7d

Browse files
committed
Rename Submission.to_dict to as_body. Fix encode and decode.
1 parent a95d1cc commit a3eed7d

File tree

3 files changed

+22
-14
lines changed

3 files changed

+22
-14
lines changed

src/judge0/clients.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,7 @@ def create_submission(self, submission: Submission) -> Submission:
9292
"wait": "false",
9393
}
9494

95-
# TODO: Rename to_dict to as_body that accepts the client.
96-
body = submission.to_dict(self)
95+
body = submission.as_body(self)
9796

9897
resp = requests.post(
9998
f"{self.endpoint}/submissions",
@@ -147,7 +146,7 @@ def create_submissions(self, submissions: list[Submission]) -> list[Submission]:
147146
f"{submission.language}!"
148147
)
149148

150-
submissions_body = [submission.to_dict(self) for submission in submissions]
149+
submissions_body = [submission.as_body(self) for submission in submissions]
151150

152151
resp = requests.post(
153152
f"{self.endpoint}/submissions/batch",

src/judge0/common.py

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,20 @@
11
from base64 import b64decode, b64encode
2+
from typing import Union
23

34

4-
def encode(text: str) -> str:
5-
return b64encode(bytes(text, "utf-8")).decode()
5+
def encode(content: Union[bytes, str]) -> str:
6+
if isinstance(content, bytes):
7+
return b64encode(content).decode()
8+
if isinstance(content, str):
9+
return b64encode(content.encode()).decode()
10+
raise ValueError(f"Unsupported type. Expected bytes or str, got {type(content)}!")
611

712

8-
def decode(b64_encoded_str: str) -> str:
9-
return b64decode(b64_encoded_str.encode()).decode(errors="backslashreplace")
13+
def decode(content: Union[bytes, str]) -> str:
14+
if isinstance(content, bytes):
15+
return b64decode(content.decode(errors="backslashreplace")).decode(
16+
errors="backslashreplace"
17+
)
18+
if isinstance(content, str):
19+
return b64decode(content.encode()).decode(errors="backslashreplace")
20+
raise ValueError(f"Unsupported type. Expected bytes or str, got {type(content)}!")

src/judge0/submission.py

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -144,18 +144,16 @@ def set_attributes(self, attributes):
144144

145145
setattr(self, attr, value)
146146

147-
# TODO: Rename to as_body that accepts a Client.
148-
def to_dict(self, client: "Client") -> dict:
147+
def as_body(self, client: "Client") -> dict:
149148
body = {
150149
"source_code": encode(self.source_code),
151150
"language_id": client.get_language_id(self.language),
152151
}
153152

154-
# TODO: Iterate over ENCODED_REQUEST_FIELDS.
155-
if self.stdin is not None:
156-
body["stdin"] = encode(self.stdin)
157-
if self.expected_output is not None:
158-
body["expected_output"] = encode(self.expected_output)
153+
for field in ENCODED_REQUEST_FIELDS:
154+
value = getattr(self, field)
155+
if value is not None:
156+
body[field] = encode(value)
159157

160158
for field in EXTRA_REQUEST_FIELDS:
161159
value = getattr(self, field)

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