Skip to content

Hermanzdosilovic/ execute #6

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 7 commits into from
Nov 12, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 1 addition & 3 deletions src/judge0/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
import os
import warnings
from typing import Optional, Union

from .api import async_execute, execute, run, sync_execute, wait
from .clients import (
Expand Down Expand Up @@ -47,7 +45,7 @@
JUDGE0_IMPLICIT_EXTRA_CE_CLIENT = None


def _get_implicit_client(flavor: Flavor) -> Optional[Client]:
def _get_implicit_client(flavor: Flavor) -> Client:
global JUDGE0_IMPLICIT_CE_CLIENT, JUDGE0_IMPLICIT_EXTRA_CE_CLIENT

# Implicit clients are already set.
Expand Down
74 changes: 41 additions & 33 deletions src/judge0/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,69 +85,77 @@ def wait(
return submissions


def async_execute(
def _execute(
*,
client: Optional[Union[Client, Flavor]] = None,
submissions: Optional[Union[Submission, list[Submission]]] = None,
source_code: Optional[str] = None,
wait_for_result: bool = False,
**kwargs,
) -> Union[Submission, list[Submission]]:
if submissions is not None and source_code is not None:
raise ValueError(
"source_code argument cannot be provided if submissions argument is provided."
"Both submissions and source_code arguments are provided. "
"Provide only one of the two."
)
if submissions is None and source_code is None:
raise ValueError("Neither source_code nor submissions argument are provided.")

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

# Check the edge cases if client is not provided.
# TODO: Since kwargs is ignored if submissions argument is provided, maybe
# use warnings if submission and kwargs are provided?

# There is no need to check for other cases since we are explicitly
# checking for submissions and source_code arguments.
if client is None:
if submissions is None:
raise ValueError(
"Client cannot be determined from None submissions argument."
)
if isinstance(submissions, list) and len(submissions) == 0:
raise ValueError(
"Client cannot be determined from the empty submissions argument."
)
raise ValueError("Client cannot be determined from empty submissions.")

client = resolve_client(client, submissions=submissions)

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

if wait_for_result:
return wait(client, submissions)
else:
return client.create_submission(submissions)
return submissions


def sync_execute(
def async_execute(
*,
client: Optional[Union[Client, Flavor]] = None,
submissions: Optional[Union[Submission, list[Submission]]] = None,
source_code: Optional[str] = None,
**kwargs,
) -> Union[Submission, list[Submission]]:
if submissions is not None and source_code is not None:
raise ValueError(
"source_code argument cannot be provided if submissions argument is provided."
)

if source_code is not None:
submissions = Submission(source_code=source_code, **kwargs)
return _execute(
client=client,
submissions=submissions,
source_code=source_code,
wait_for_result=False,
**kwargs,
)

# Check the edge cases if client is not provided.
if client is None:
if submissions is None:
raise ValueError(
"Client cannot be determined from None submissions argument."
)
if isinstance(submissions, list) and len(submissions) == 0:
raise ValueError(
"Client cannot be determined from the empty submissions argument."
)

client = resolve_client(client, submissions=submissions)
submissions = async_execute(client=client, submissions=submissions)
return wait(client, submissions)
def sync_execute(
*,
client: Optional[Union[Client, Flavor]] = None,
submissions: Optional[Union[Submission, list[Submission]]] = None,
source_code: Optional[str] = None,
**kwargs,
) -> Union[Submission, list[Submission]]:
return _execute(
client=client,
submissions=submissions,
source_code=source_code,
wait_for_result=True,
**kwargs,
)


execute = sync_execute
Expand Down
12 changes: 6 additions & 6 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,12 @@ def test_resolve_client_empty_submissions_argument(submissions):

def test_resolve_client_no_common_client_for_submissions():
cpp_submission = Submission(
source_code="", # source code is not important because in this test
source_code="", # source code is not important in this test
language_id=Language.CPP_GCC,
)

py_submission = Submission(
source_code="", # source code is not important because in this test
source_code="", # source code is not important in this test
language_id=Language.PYTHON_FOR_ML,
)

Expand All @@ -77,12 +77,12 @@ def test_resolve_client_no_common_client_for_submissions():

def test_resolve_client_common_ce_client():
cpp_submission = Submission(
source_code="", # source code is not important because in this test
source_code="", # source code is not important in this test
language_id=Language.CPP_GCC,
)

py_submission = Submission(
source_code="", # source code is not important because in this test
source_code="", # source code is not important in this test
language_id=Language.PYTHON,
)

Expand All @@ -93,12 +93,12 @@ def test_resolve_client_common_ce_client():

def test_resolve_client_common_extra_ce_client():
cpp_submission = Submission(
source_code="", # source code is not important because in this test
source_code="", # source code is not important in this test
language_id=Language.CPP_CLANG,
)

py_submission = Submission(
source_code="", # source code is not important because in this test
source_code="", # source code is not important in this test
language_id=Language.PYTHON_FOR_ML,
)

Expand Down
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