|
1 | 1 | from base64 import b64decode, b64encode
|
2 |
| -from dataclasses import dataclass |
3 |
| -from typing import Union |
| 2 | + |
4 | 3 |
|
5 | 4 | ENCODED_REQUEST_FIELDS = {
|
6 | 5 | "source_code",
|
|
44 | 43 | FIELDS = REQUEST_FIELDS | RESPONSE_FIELDS
|
45 | 44 |
|
46 | 45 |
|
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 | + |
48 | 54 | class Submission:
|
49 | 55 | """
|
50 | 56 | Stores a representation of a Submission to/from Judge0.
|
@@ -116,24 +122,30 @@ def __init__(
|
116 | 122 | self.wall_time = None
|
117 | 123 | self.memory = None
|
118 | 124 |
|
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 | + } |
121 | 137 |
|
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) |
124 | 142 |
|
125 |
| - def update_extra_request_fields(self, body): |
126 | 143 | for field in EXTRA_REQUEST_FIELDS:
|
127 | 144 | value = getattr(self, field)
|
128 | 145 | if value is not None:
|
129 | 146 | body[field] = value
|
130 | 147 |
|
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 |
137 | 149 |
|
138 | 150 |
|
139 | 151 | class SingleFileSubmission(Submission):
|
|
0 commit comments