Skip to content

Commit 967ed92

Browse files
Load implicit and preview clients only when needed. (judge0#1)
* Load implicit and preview clients only when needed. * Add comments. Make api_key for Sulu clients positional and keyword argument with default value. * Run formatter and minor refactor of api module. --------- Co-authored-by: Filip Karlo Došilović <filipk.dosilovic@gmail.com>
1 parent 3d31a59 commit 967ed92

File tree

3 files changed

+49
-52
lines changed

3 files changed

+49
-52
lines changed

src/judge0/__init__.py

Lines changed: 28 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -43,11 +43,19 @@
4343
"wait",
4444
]
4545

46+
JUDGE0_IMPLICIT_CE_CLIENT = None
47+
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT = None
48+
49+
50+
def _get_implicit_client(flavor: Flavor) -> Optional[Client]:
51+
global JUDGE0_IMPLICIT_CE_CLIENT, JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
52+
53+
# Implicit clients are already set.
54+
if flavor == Flavor.CE and JUDGE0_IMPLICIT_CE_CLIENT is not None:
55+
return JUDGE0_IMPLICIT_CE_CLIENT
56+
if flavor == Flavor.EXTRA_CE and JUDGE0_IMPLICIT_EXTRA_CE_CLIENT is not None:
57+
return JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
4658

47-
def _create_default_client(
48-
flavor: Flavor,
49-
preview_client_class: Union[SuluJudge0CE, SuluJudge0ExtraCE],
50-
) -> Optional[Client]:
5159
from .clients import CE, EXTRA_CE
5260

5361
try:
@@ -62,31 +70,31 @@ def _create_default_client(
6270
else:
6371
client_classes = EXTRA_CE
6472

73+
# Try to find one of the predefined keys JUDGE0_{SULU,RAPID,ATD}_API_KEY
74+
# in environment variables.
6575
client = None
6676
for client_class in client_classes:
6777
api_key = os.getenv(client_class.API_KEY_ENV)
6878
if api_key is not None:
69-
# It is possible for a client to be subscribed to one flavor with
70-
# API key.
71-
try:
72-
client = client_class(api_key)
73-
break
74-
except Exception as e:
75-
warnings.warn(f"Failed to initialize client: {e}")
79+
client = client_class(api_key)
80+
break
81+
82+
# If we didn't find any of the possible predefined keys, initialize
83+
# the preview Sulu client based on the flavor.
84+
if client is None:
85+
if flavor == Flavor.CE:
86+
client = SuluJudge0CE()
87+
else:
88+
client = SuluJudge0ExtraCE()
89+
90+
if flavor == Flavor.CE:
91+
JUDGE0_IMPLICIT_CE_CLIENT = client
7692
else:
77-
try:
78-
client = preview_client_class("")
79-
except Exception as e:
80-
warnings.warn(f"Failed to initialize preview client: {e}")
93+
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT = client
8194

8295
return client
8396

8497

85-
JUDGE0_IMPLICIT_CE_CLIENT = _create_default_client(Flavor.CE, SuluJudge0CE)
86-
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT = _create_default_client(
87-
Flavor.EXTRA_CE, SuluJudge0ExtraCE
88-
)
89-
9098
CE = Flavor.CE
9199
EXTRA_CE = Flavor.EXTRA_CE
92100

src/judge0/api.py

Lines changed: 12 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -10,44 +10,30 @@ def resolve_client(
1010
client: Optional[Union[Client, Flavor]] = None,
1111
submissions: Optional[Union[Submission, list[Submission]]] = None,
1212
) -> Union[Client, None]:
13-
from . import JUDGE0_IMPLICIT_CE_CLIENT, JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
14-
1513
# User explicitly passed a client.
1614
if isinstance(client, Client):
1715
return client
1816

17+
from . import _get_implicit_client
18+
1919
# User explicitly choose the flavor of the client.
2020
if isinstance(client, Flavor):
21-
if client == Flavor.CE:
22-
return JUDGE0_IMPLICIT_CE_CLIENT
23-
else:
24-
return JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
21+
return _get_implicit_client(flavor=client)
2522

2623
# client is None and we have to determine a flavor of the client from the
2724
# submissions and the languages.
2825
if isinstance(submissions, Submission):
2926
submissions = [submissions]
3027

3128
# Check which client supports all languages from the provided submissions.
32-
languages = [submission.language_id for submission in submissions]
33-
34-
if JUDGE0_IMPLICIT_CE_CLIENT is not None:
35-
if all(
36-
(
37-
JUDGE0_IMPLICIT_CE_CLIENT.is_language_supported(lang)
38-
for lang in languages
39-
)
40-
):
41-
return JUDGE0_IMPLICIT_CE_CLIENT
29+
languages = (submission.language_id for submission in submissions)
4230

43-
if JUDGE0_IMPLICIT_EXTRA_CE_CLIENT is not None:
44-
if all(
45-
(
46-
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT.is_language_supported(lang)
47-
for lang in languages
48-
)
31+
for flavor in Flavor:
32+
client = _get_implicit_client(flavor)
33+
if client is not None and all(
34+
(client.is_language_supported(lang) for lang in languages)
4935
):
50-
return JUDGE0_IMPLICIT_EXTRA_CE_CLIENT
36+
return client
5137

5238
raise RuntimeError(
5339
"Failed to resolve the client from submissions argument. "
@@ -65,13 +51,13 @@ def wait(
6551
if retry_mechanism is None:
6652
retry_mechanism = RegularPeriodRetry()
6753

68-
if not isinstance(submissions, (list, tuple)):
54+
if isinstance(submissions, (list, tuple)):
6955
submissions_to_check = {
70-
submission.token: submission for submission in [submissions]
56+
submission.token: submission for submission in submissions
7157
}
7258
else:
7359
submissions_to_check = {
74-
submission.token: submission for submission in submissions
60+
submission.token: submission for submission in [submissions]
7561
}
7662

7763
while len(submissions_to_check) > 0 and not retry_mechanism.is_done():

src/judge0/clients.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -402,25 +402,28 @@ def __init__(self, api_key):
402402
class Sulu(Client):
403403
API_KEY_ENV = "JUDGE0_SULU_API_KEY"
404404

405-
def __init__(self, endpoint, api_key):
405+
def __init__(self, endpoint, api_key=None):
406406
self.api_key = api_key
407-
super().__init__(endpoint, {"Authorization": f"Bearer {api_key}"})
407+
super().__init__(
408+
endpoint,
409+
{"Authorization": f"Bearer {api_key}"} if api_key else None,
410+
)
408411

409412

410413
class SuluJudge0CE(Sulu):
411414
DEFAULT_ENDPOINT: str = "https://judge0-ce.p.sulu.sh"
412415
HOME_URL: str = "https://sparkhub.sulu.sh/apis/judge0/judge0-ce/readme"
413416

414-
def __init__(self, api_key):
415-
super().__init__(self.DEFAULT_ENDPOINT, api_key=api_key)
417+
def __init__(self, api_key=None):
418+
super().__init__(self.DEFAULT_ENDPOINT, api_key)
416419

417420

418421
class SuluJudge0ExtraCE(Sulu):
419422
DEFAULT_ENDPOINT: str = "https://judge0-extra-ce.p.sulu.sh"
420423
HOME_URL: str = "https://sparkhub.sulu.sh/apis/judge0/judge0-extra-ce/readme"
421424

422-
def __init__(self, api_key):
423-
super().__init__(self.DEFAULT_ENDPOINT, api_key=api_key)
425+
def __init__(self, api_key=None):
426+
super().__init__(self.DEFAULT_ENDPOINT, api_key)
424427

425428

426429
CE = [RapidJudge0CE, SuluJudge0CE, ATDJudge0CE]

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