Skip to content

Commit

Permalink
Move high-level api functions from clients module to api module. Rena…
Browse files Browse the repository at this point in the history
…me _create_default_client to _create_default_ce_client.
  • Loading branch information
fkdosilovic committed Nov 4, 2024
1 parent 662b3d4 commit bfd1222
Show file tree
Hide file tree
Showing 3 changed files with 91 additions and 94 deletions.
11 changes: 4 additions & 7 deletions src/judge0/__init__.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,16 @@
import os

from .api import async_execute, execute, sync_execute, wait
from .clients import (
async_execute,
ATDJudge0CE,
ATDJudge0ExtraCE,
Client,
execute,
RapidJudge0CE,
RapidJudge0ExtraCE,
SuluJudge0CE,
SuluJudge0ExtraCE,
sync_execute,
wait,
)
from .retry import MaxRetries, MaxWaitTime, RegularPeriodRetry

from .submission import Submission

__all__ = [
Expand All @@ -36,7 +32,7 @@
]


def _create_default_client():
def _create_default_ce_client():
try:
from dotenv import load_dotenv

Expand All @@ -55,9 +51,10 @@ def _create_default_client():
elif atd_api_key is not None:
client = ATDJudge0CE(api_key=atd_api_key)
else:
# TODO: Create SuluJudge0CE with default client.
client = None

globals()["judge0_default_client"] = client


_create_default_client()
_create_default_ce_client()
87 changes: 87 additions & 0 deletions src/judge0/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
from typing import Optional, Union

from .clients import Client
from .retry import RegularPeriodRetry, RetryMechanism
from .submission import Submission


def wait(
submissions: Union[Submission, list[Submission]],
*,
client: Optional[Client] = None,
retry_mechanism: Optional[RetryMechanism] = None,
) -> Union[Submission, list[Submission]]:
if client is None:
from . import judge0_default_client

if judge0_default_client is None:
raise RuntimeError(
"Client is not set. Please explicitly set the client argument "
"or make sure that one of the implemented client's API keys "
"is set as an environment variable."
)
else:
client = judge0_default_client

if retry_mechanism is None:
retry_mechanism = RegularPeriodRetry()

if not isinstance(submissions, (list, tuple)):
submissions_to_check = {
submission.token: submission for submission in [submissions]
}
else:
submissions_to_check = {
submission.token: submission for submission in submissions
}

while len(submissions_to_check) > 0 and not retry_mechanism.is_done():
client.get_submissions(submissions_to_check.values())
for token in list(submissions_to_check):
submission = submissions_to_check[token]
if submission.is_done():
submissions_to_check.pop(token)

# Don't wait if there is no submissions to check for anymore.
if len(submissions_to_check) == 0:
break

retry_mechanism.wait()
retry_mechanism.step()

return submissions


def async_execute(
*,
client: Optional[Client] = None,
submissions: Union[Submission, list[Submission], None] = None,
) -> Union[Submission, list[Submission]]:
if client is None:
from . import judge0_default_client

if judge0_default_client is None:
raise RuntimeError(
"Client is not set. Please explicitly set the client argument "
"or make sure that one of the implemented client's API keys "
"is set as an environment variable."
)
else:
client = judge0_default_client

if isinstance(submissions, (list, tuple)):
return client.create_submissions(submissions)
else:
return client.create_submission(submissions)


def sync_execute(
*,
client: Optional[Client] = None,
submissions: Union[Submission, list[Submission], None] = None,
) -> Union[Submission, list[Submission]]:
submissions = async_execute(client=client, submissions=submissions)
return wait(client=client, submissions=submissions)


execute = sync_execute
87 changes: 0 additions & 87 deletions src/judge0/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,90 +376,3 @@ class SuluJudge0ExtraCE(Sulu):

def __init__(self, api_key):
super().__init__(self.DEFAULT_ENDPOINT, api_key=api_key)


def wait(
*,
client: Optional[Client] = None,
submissions: Union[Submission, list[Submission], None] = None,
retry_mechanism: Optional[RetryMechanism] = None,
) -> Union[Submission, list[Submission]]:
if client is None:
from . import judge0_default_client

if judge0_default_client is None:
raise RuntimeError(
"Client is not set. Please explicitly set the client argument "
"or make sure that one of the implemented client's API keys "
"is set as an environment variable."
)
else:
client = judge0_default_client

if retry_mechanism is None:
retry_mechanism = RegularPeriodRetry()

if not isinstance(submissions, (list, tuple)):
submissions_to_check = {
submission.token: submission for submission in [submissions]
}
else:
submissions_to_check = {
submission.token: submission for submission in submissions
}

while len(submissions_to_check) > 0 and not retry_mechanism.is_done():
client.get_submissions(submissions_to_check.values())
for token in list(submissions_to_check):
submission = submissions_to_check[token]
if submission.is_done():
submissions_to_check.pop(token)

# Don't wait if there is no submissions to check for anymore.
if len(submissions_to_check) == 0:
break

retry_mechanism.wait()
retry_mechanism.step()

return submissions


def async_execute(
*,
client: Optional[Client] = None,
submissions: Union[Submission, list[Submission], None] = None,
) -> Union[Submission, list[Submission]]:
if client is None:
from . import judge0_default_client

if judge0_default_client is None:
raise RuntimeError(
"Client is not set. Please explicitly set the client argument "
"or make sure that one of the implemented client's API keys "
"is set as an environment variable."
)
else:
client = judge0_default_client

if isinstance(submissions, (list, tuple)):
return client.create_submissions(submissions)
else:
return client.create_submission(submissions)


def sync_execute(
*,
client: Optional[Client] = None,
submissions: Union[Submission, list[Submission], None] = None,
) -> Union[Submission, list[Submission]]:
submissions = async_execute(client=client, submissions=submissions)
return wait(client=client, submissions=submissions)


def execute(
*,
client: Optional[Client] = None,
submissions: Union[Submission, list[Submission], None] = None,
) -> Union[Submission, list[Submission]]:
return sync_execute(client=client, submissions=submissions)

0 comments on commit bfd1222

Please sign in to comment.