Skip to content

Commit b7964b7

Browse files
committed
Add resolving of implicit client. Add an option to choose implicit client using flavor enum.
1 parent 3c67ef0 commit b7964b7

File tree

5 files changed

+108
-48
lines changed

5 files changed

+108
-48
lines changed

examples/0001_hello_world.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
language_id=100,
66
)
77

8-
judge0.execute(submissions=submission)
8+
# Run submission on CE flavor of judge0.
9+
judge0.run(submissions=submission)
910

1011
print(submission.stdout)

examples/0002_hello_world.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import judge0
2+
from judge0 import Flavor
3+
4+
submission = judge0.Submission(
5+
source_code="print('Hello Judge0')",
6+
language_id=25,
7+
)
8+
9+
# Instead of relying on the CE flavor of judge0, we can use EXTRA_CE.
10+
judge0.run(client=Flavor.EXTRA_CE, submissions=submission)
11+
12+
print(submission.stdout)

src/judge0/__init__.py

Lines changed: 65 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,42 +1,87 @@
11
import os
2+
import warnings
3+
from typing import Optional, Union
24

3-
from .api import async_execute, execute, sync_execute, wait
4-
from .clients import CE, Client, EXTRA_CE
5+
from .api import async_execute, execute, run, sync_execute, wait
6+
from .clients import (
7+
ATD,
8+
ATDJudge0CE,
9+
ATDJudge0ExtraCE,
10+
Client,
11+
Rapid,
12+
RapidJudge0CE,
13+
RapidJudge0ExtraCE,
14+
Sulu,
15+
SuluJudge0CE,
16+
SuluJudge0ExtraCE,
17+
)
18+
from .common import Flavor, Status
519
from .retry import MaxRetries, MaxWaitTime, RegularPeriodRetry
620
from .submission import Submission
721

822
__all__ = [
9-
Client,
10-
*CE,
11-
*EXTRA_CE,
12-
Submission,
13-
RegularPeriodRetry,
14-
MaxRetries,
15-
MaxWaitTime,
16-
async_execute,
17-
sync_execute,
18-
execute,
19-
wait,
23+
"ATD",
24+
"ATDJudge0CE",
25+
"ATDJudge0ExtraCE",
26+
"Client",
27+
"Rapid",
28+
"RapidJudge0CE",
29+
"RapidJudge0ExtraCE",
30+
"Sulu",
31+
"SuluJudge0CE",
32+
"SuluJudge0ExtraCE",
33+
"Submission",
34+
"RegularPeriodRetry",
35+
"MaxRetries",
36+
"MaxWaitTime",
37+
"async_execute",
38+
"sync_execute",
39+
"execute",
40+
"wait",
41+
"run",
42+
"Status",
2043
]
2144

2245

23-
def _create_default_ce_client():
46+
def _create_default_client(
47+
flavor: Flavor,
48+
preview_client_class: Union[SuluJudge0CE, SuluJudge0ExtraCE],
49+
) -> Optional[Client]:
50+
from .clients import CE, EXTRA_CE
51+
2452
try:
2553
from dotenv import load_dotenv
2654

2755
load_dotenv()
2856
except: # noqa: E722
2957
pass
3058

59+
if flavor == Flavor.CE:
60+
client_classes = CE
61+
else:
62+
client_classes = EXTRA_CE
63+
3164
client = None
32-
for client_class in CE:
65+
for client_class in client_classes:
3366
api_key = os.getenv(client_class.API_KEY_ENV)
3467
if api_key is not None:
35-
client = client_class(api_key)
36-
37-
# TODO: If client is none, instantiate the default client.
68+
# It is possible for a client to be subscribed to one flavor with
69+
# API key.
70+
try:
71+
client = client_class(api_key)
72+
break
73+
except Exception as e:
74+
warnings.warn(f"Failed to initialize client: {e}")
75+
else:
76+
try:
77+
client = preview_client_class("")
78+
except Exception as e:
79+
warnings.warn(f"Failed to initialize preview client: {e}")
3880

39-
globals()["judge0_default_client"] = client
81+
return client
4082

4183

42-
_create_default_ce_client()
84+
JUDGE0_IMPLICIT_CE_CLIENT = _create_default_client(Flavor.CE, SuluJudge0CE)
85+
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT = _create_default_client(
86+
Flavor.EXTRA_CE, SuluJudge0ExtraCE
87+
)

src/judge0/api.py

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,33 @@
11
from typing import Optional, Union
22

33
from .clients import Client
4+
from .common import Flavor
45
from .retry import RegularPeriodRetry, RetryMechanism
56
from .submission import Submission
67

78

9+
def _resolve_client(client: Union[Client, Flavor]) -> Client:
10+
if not isinstance(client, Flavor):
11+
return client
12+
13+
if client == Flavor.CE:
14+
from . import JUDGE0_IMPLICIT_CE_CLIENT
15+
16+
client = JUDGE0_IMPLICIT_CE_CLIENT
17+
else:
18+
from . import JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
19+
20+
client = JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
21+
22+
return client
23+
24+
825
def wait(
26+
client: Client,
927
submissions: Union[Submission, list[Submission]],
1028
*,
11-
client: Optional[Client] = None,
1229
retry_mechanism: Optional[RetryMechanism] = None,
1330
) -> Union[Submission, list[Submission]]:
14-
if client is None:
15-
from . import judge0_default_client
16-
17-
if judge0_default_client is None:
18-
raise RuntimeError(
19-
"Client is not set. Please explicitly set the client argument "
20-
"or make sure that one of the implemented client's API keys "
21-
"is set as an environment variable."
22-
)
23-
else:
24-
client = judge0_default_client
25-
2631
if retry_mechanism is None:
2732
retry_mechanism = RegularPeriodRetry()
2833

@@ -54,20 +59,10 @@ def wait(
5459

5560
def async_execute(
5661
*,
57-
client: Optional[Client] = None,
62+
client: Union[Client, Flavor] = Flavor.CE,
5863
submissions: Union[Submission, list[Submission], None] = None,
5964
) -> Union[Submission, list[Submission]]:
60-
if client is None:
61-
from . import judge0_default_client
62-
63-
if judge0_default_client is None:
64-
raise RuntimeError(
65-
"Client is not set. Please explicitly set the client argument "
66-
"or make sure that one of the implemented client's API keys "
67-
"is set as an environment variable."
68-
)
69-
else:
70-
client = judge0_default_client
65+
client = _resolve_client(client)
7166

7267
if isinstance(submissions, (list, tuple)):
7368
return client.create_submissions(submissions)
@@ -77,11 +72,13 @@ def async_execute(
7772

7873
def sync_execute(
7974
*,
80-
client: Optional[Client] = None,
75+
client: Union[Client, Flavor] = Flavor.CE,
8176
submissions: Union[Submission, list[Submission], None] = None,
8277
) -> Union[Submission, list[Submission]]:
78+
client = _resolve_client(client)
8379
submissions = async_execute(client=client, submissions=submissions)
84-
return wait(client=client, submissions=submissions)
80+
return wait(client, submissions)
8581

8682

8783
execute = sync_execute
84+
run = execute

src/judge0/common.py

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,11 @@
11
from enum import IntEnum
22

33

4+
class Flavor(IntEnum):
5+
CE = 0
6+
EXTRA_CE = 1
7+
8+
49
class Status(IntEnum):
510
IN_QUEUE = 1
611
PROCESSING = 2

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