Skip to content

Commit fc727c1

Browse files
committed
Add to_dict for Submission serialization to dict/json. Update decoding to handle some edge cases.
1 parent e1c77df commit fc727c1

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

src/judge0/clients.py

Lines changed: 2 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -59,21 +59,11 @@ def get_statuses(self) -> list[dict]:
5959
def create_submission(self, submission: Submission):
6060
# TODO: check if client supports specified language_id
6161
params = {
62-
"base64_encoded": "true", # TODO: Make customizable later.
62+
"base64_encoded": "true",
6363
"wait": str(self.wait).lower(),
6464
}
6565

66-
body = {
67-
"source_code": submission.encode(submission.source_code),
68-
"language_id": submission.language_id,
69-
}
70-
71-
if submission.stdin:
72-
body["stdin"] = submission.encode(submission.stdin)
73-
if submission.expected_output:
74-
body["expected_output"] = submission.encode(submission.expected_output)
75-
76-
submission.update_extra_request_fields(body)
66+
body = submission.to_dict()
7767

7868
resp = requests.post(
7969
f"{self.endpoint}/submissions",

src/judge0/submission.py

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
from base64 import b64decode, b64encode
2-
from dataclasses import dataclass
3-
from typing import Union
2+
43

54
ENCODED_REQUEST_FIELDS = {
65
"source_code",
@@ -44,7 +43,14 @@
4443
FIELDS = REQUEST_FIELDS | RESPONSE_FIELDS
4544

4645

47-
@dataclass
46+
def encode(text: str) -> str:
47+
return b64encode(bytes(text, "utf-8")).decode()
48+
49+
50+
def decode(b64_encoded_str: str) -> str:
51+
return b64decode(b64_encoded_str.encode()).decode(errors="backslashreplace")
52+
53+
4854
class Submission:
4955
"""
5056
Stores a representation of a Submission to/from Judge0.
@@ -116,24 +122,30 @@ def __init__(
116122
self.wall_time = None
117123
self.memory = None
118124

119-
def encode(self, text: str) -> str:
120-
return b64encode(bytes(text, "utf-8")).decode()
125+
def set_attributes(self, attributes):
126+
for attr, value in attributes.items():
127+
if attr in ENCODED_FIELDS:
128+
setattr(self, attr, decode(value) if value else None)
129+
else:
130+
setattr(self, attr, value)
131+
132+
def to_dict(self) -> dict:
133+
body = {
134+
"source_code": encode(self.source_code),
135+
"language_id": self.language_id,
136+
}
121137

122-
def decode(self, bytes_string: str) -> str:
123-
return b64decode(bytes_string.encode()).decode()
138+
if self.stdin is not None:
139+
body["stdin"] = encode(self.stdin)
140+
if self.expected_output is not None:
141+
body["expected_output"] = encode(self.expected_output)
124142

125-
def update_extra_request_fields(self, body):
126143
for field in EXTRA_REQUEST_FIELDS:
127144
value = getattr(self, field)
128145
if value is not None:
129146
body[field] = value
130147

131-
def set_attributes(self, attributes):
132-
for attr, value in attributes.items():
133-
if attr in ENCODED_FIELDS:
134-
setattr(self, attr, self.decode(value) if value else None)
135-
else:
136-
setattr(self, attr, value)
148+
return body
137149

138150

139151
class SingleFileSubmission(Submission):

0 commit comments

Comments
 (0)