Skip to content

Commit 71d55f1

Browse files
Hermanzdosilovic/ execute (#6)
* Refactor async_execute and sync_execute to use _execute. * Fix some comments. --------- Co-authored-by: Filip Karlo Došilović <[email protected]>
1 parent 967ed92 commit 71d55f1

File tree

3 files changed

+48
-42
lines changed

3 files changed

+48
-42
lines changed

src/judge0/__init__.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11
import os
2-
import warnings
3-
from typing import Optional, Union
42

53
from .api import async_execute, execute, run, sync_execute, wait
64
from .clients import (
@@ -47,7 +45,7 @@
4745
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT = None
4846

4947

50-
def _get_implicit_client(flavor: Flavor) -> Optional[Client]:
48+
def _get_implicit_client(flavor: Flavor) -> Client:
5149
global JUDGE0_IMPLICIT_CE_CLIENT, JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
5250

5351
# Implicit clients are already set.

src/judge0/api.py

Lines changed: 41 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -85,69 +85,77 @@ def wait(
8585
return submissions
8686

8787

88-
def async_execute(
88+
def _execute(
8989
*,
9090
client: Optional[Union[Client, Flavor]] = None,
9191
submissions: Optional[Union[Submission, list[Submission]]] = None,
9292
source_code: Optional[str] = None,
93+
wait_for_result: bool = False,
9394
**kwargs,
9495
) -> Union[Submission, list[Submission]]:
9596
if submissions is not None and source_code is not None:
9697
raise ValueError(
97-
"source_code argument cannot be provided if submissions argument is provided."
98+
"Both submissions and source_code arguments are provided. "
99+
"Provide only one of the two."
98100
)
101+
if submissions is None and source_code is None:
102+
raise ValueError("Neither source_code nor submissions argument are provided.")
99103

100104
if source_code is not None:
101105
submissions = Submission(source_code=source_code, **kwargs)
102106

103-
# Check the edge cases if client is not provided.
107+
# TODO: Since kwargs is ignored if submissions argument is provided, maybe
108+
# use warnings if submission and kwargs are provided?
109+
110+
# There is no need to check for other cases since we are explicitly
111+
# checking for submissions and source_code arguments.
104112
if client is None:
105-
if submissions is None:
106-
raise ValueError(
107-
"Client cannot be determined from None submissions argument."
108-
)
109113
if isinstance(submissions, list) and len(submissions) == 0:
110-
raise ValueError(
111-
"Client cannot be determined from the empty submissions argument."
112-
)
114+
raise ValueError("Client cannot be determined from empty submissions.")
113115

114116
client = resolve_client(client, submissions=submissions)
115117

116118
if isinstance(submissions, (list, tuple)):
117-
return client.create_submissions(submissions)
119+
submissions = client.create_submissions(submissions)
120+
else:
121+
submissions = client.create_submission(submissions)
122+
123+
if wait_for_result:
124+
return wait(client, submissions)
118125
else:
119-
return client.create_submission(submissions)
126+
return submissions
120127

121128

122-
def sync_execute(
129+
def async_execute(
123130
*,
124131
client: Optional[Union[Client, Flavor]] = None,
125132
submissions: Optional[Union[Submission, list[Submission]]] = None,
126133
source_code: Optional[str] = None,
127134
**kwargs,
128135
) -> Union[Submission, list[Submission]]:
129-
if submissions is not None and source_code is not None:
130-
raise ValueError(
131-
"source_code argument cannot be provided if submissions argument is provided."
132-
)
133-
134-
if source_code is not None:
135-
submissions = Submission(source_code=source_code, **kwargs)
136+
return _execute(
137+
client=client,
138+
submissions=submissions,
139+
source_code=source_code,
140+
wait_for_result=False,
141+
**kwargs,
142+
)
136143

137-
# Check the edge cases if client is not provided.
138-
if client is None:
139-
if submissions is None:
140-
raise ValueError(
141-
"Client cannot be determined from None submissions argument."
142-
)
143-
if isinstance(submissions, list) and len(submissions) == 0:
144-
raise ValueError(
145-
"Client cannot be determined from the empty submissions argument."
146-
)
147144

148-
client = resolve_client(client, submissions=submissions)
149-
submissions = async_execute(client=client, submissions=submissions)
150-
return wait(client, submissions)
145+
def sync_execute(
146+
*,
147+
client: Optional[Union[Client, Flavor]] = None,
148+
submissions: Optional[Union[Submission, list[Submission]]] = None,
149+
source_code: Optional[str] = None,
150+
**kwargs,
151+
) -> Union[Submission, list[Submission]]:
152+
return _execute(
153+
client=client,
154+
submissions=submissions,
155+
source_code=source_code,
156+
wait_for_result=True,
157+
**kwargs,
158+
)
151159

152160

153161
execute = sync_execute

tests/test_api.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,12 @@ def test_resolve_client_empty_submissions_argument(submissions):
6060

6161
def test_resolve_client_no_common_client_for_submissions():
6262
cpp_submission = Submission(
63-
source_code="", # source code is not important because in this test
63+
source_code="", # source code is not important in this test
6464
language_id=Language.CPP_GCC,
6565
)
6666

6767
py_submission = Submission(
68-
source_code="", # source code is not important because in this test
68+
source_code="", # source code is not important in this test
6969
language_id=Language.PYTHON_FOR_ML,
7070
)
7171

@@ -77,12 +77,12 @@ def test_resolve_client_no_common_client_for_submissions():
7777

7878
def test_resolve_client_common_ce_client():
7979
cpp_submission = Submission(
80-
source_code="", # source code is not important because in this test
80+
source_code="", # source code is not important in this test
8181
language_id=Language.CPP_GCC,
8282
)
8383

8484
py_submission = Submission(
85-
source_code="", # source code is not important because in this test
85+
source_code="", # source code is not important in this test
8686
language_id=Language.PYTHON,
8787
)
8888

@@ -93,12 +93,12 @@ def test_resolve_client_common_ce_client():
9393

9494
def test_resolve_client_common_extra_ce_client():
9595
cpp_submission = Submission(
96-
source_code="", # source code is not important because in this test
96+
source_code="", # source code is not important in this test
9797
language_id=Language.CPP_CLANG,
9898
)
9999

100100
py_submission = Submission(
101-
source_code="", # source code is not important because in this test
101+
source_code="", # source code is not important in this test
102102
language_id=Language.PYTHON_FOR_ML,
103103
)
104104

0 commit comments

Comments
 (0)