From 8dc49bdc9c430b4a258ce98d78a41d0b6c3ce0c4 Mon Sep 17 00:00:00 2001 From: Gregorio Iniesta Date: Fri, 11 Oct 2024 18:25:31 +0200 Subject: [PATCH] fixed code quality --- client/arguments.serverless | 2 +- client/qiskit_serverless/core/__init__.py | 6 ++--- client/qiskit_serverless/core/client.py | 25 ++++++++----------- .../{LocalClient.py => local_client.py} | 14 +++++++---- .../clients/{RayClient.py => ray_client.py} | 15 +++++------ ...rverlessClient.py => serverless_client.py} | 0 client/qiskit_serverless/core/job.py | 5 +--- client/tests/core/test_job.py | 4 +++ client/tests/core/test_pattern.py | 4 --- client/tests/utils.py | 7 +++--- 10 files changed, 40 insertions(+), 42 deletions(-) rename client/qiskit_serverless/core/clients/{LocalClient.py => local_client.py} (96%) rename client/qiskit_serverless/core/clients/{RayClient.py => ray_client.py} (91%) rename client/qiskit_serverless/core/clients/{ServerlessClient.py => serverless_client.py} (100%) diff --git a/client/arguments.serverless b/client/arguments.serverless index 1d21bd68a..1e01da07a 100644 --- a/client/arguments.serverless +++ b/client/arguments.serverless @@ -1 +1 @@ -{"circuit": {"__type__": "QuantumCircuit", "__value__": "eJwL9Az29gzhZmRiYYAAxtRCBu40Bg4gEyYEAkwgKSibJTmzKLk0s0TX0MwAJl9dW8gI1sLIWMiAChiRjAABZrgMB5oKdBAckZLunliSWoiimhmH6iBn5wiYamYQwVQINpsdagMTLn2RkTBtYB0MaTDnnVzG37bY9Ic9AyvUWlwODYXbC9frPTeU5VUpowNcgN3Jd+++JYIIgWNBqqWnbRgdgOz/SABmJgCi1zDN"}, "array": {"__type__": "ndarray", "__value__": "eJyb7BfqGxDJyFDGUK2eklqcXKRupaBuk2ahrqOgnpZfVFKUmBefX5SSChJ3S8wpTgWKF2ckFqQC+RpGOgqGmjoKtQpkAy4GMHB1YIACABOFG/M="}} \ No newline at end of file +{"circuit": {"__type__": "QuantumCircuit", "__value__": "eJwL9Az29gzhZmRiYYAAxtRCBu40Bg4gEyYEAkwgKSibOTmzKLk0s0TX0MwEJl9dW8gI1sLIWMiAChiRjABrh8twQGlki5AUsTsbB0e4J5akgk0EEUwgAmwBMwM71GgmTM1gEBQZCdPLDNaWBrPVrEn1gruzuAPcfkbsJjAER6Skw8wAWfMfCcDUAACxLyM+"}, "array": {"__type__": "ndarray", "__value__": "eJyb7BfqGxDJyFDGUK2eklqcXKRupaBuk2ahrqOgnpZfVFKUmBefX5SSChJ3S8wpTgWKF2ckFqQC+RpGOgqGmjoKtQpkAy4GMHB1YIACABOFG/M="}} \ No newline at end of file diff --git a/client/qiskit_serverless/core/__init__.py b/client/qiskit_serverless/core/__init__.py index 5ec773ee9..2852c01d7 100644 --- a/client/qiskit_serverless/core/__init__.py +++ b/client/qiskit_serverless/core/__init__.py @@ -49,9 +49,9 @@ from .client import BaseClient -from .clients.LocalClient import LocalClient -from .clients.RayClient import RayClient -from .clients.ServerlessClient import ServerlessClient, IBMServerlessClient +from .clients.local_client import LocalClient +from .clients.ray_client import RayClient +from .clients.serverless_client import ServerlessClient, IBMServerlessClient from .job import ( Job, diff --git a/client/qiskit_serverless/core/client.py b/client/qiskit_serverless/core/client.py index 4a2f7a08c..566ab5b24 100644 --- a/client/qiskit_serverless/core/client.py +++ b/client/qiskit_serverless/core/client.py @@ -95,7 +95,6 @@ def get_jobs(self, **kwargs) -> List[Job]: Returns: list of jobs. """ - pass @abstractmethod def get_job(self, job_id: str) -> Optional[Job]: @@ -107,9 +106,16 @@ def get_job(self, job_id: str) -> Optional[Job]: Returns: Job instance """ - pass def get_job_by_id(self, job_id: str) -> Optional[Job]: + """Returns job by job id. + + Args: + job_id: job id + + Returns: + Job instance + """ warnings.warn( "`get_job_by_id` method has been deprecated. " "And will be removed in future releases. " @@ -144,34 +150,28 @@ def run( Returns: Job """ - pass @abstractmethod def status(self, job_id: str) -> str: """Check status.""" - pass @abstractmethod def stop( self, job_id: str, service: Optional[QiskitRuntimeService] = None ) -> Union[str, bool]: """Stops job/program.""" - pass @abstractmethod def result(self, job_id: str) -> Any: """Return results.""" - pass @abstractmethod def logs(self, job_id: str) -> str: """Return logs.""" - pass @abstractmethod def filtered_logs(self, job_id: str, **kwargs) -> str: """Return filtered logs.""" - pass ######################### ####### Functions ####### @@ -180,23 +180,21 @@ def filtered_logs(self, job_id: str, **kwargs) -> str: @abstractmethod def upload(self, program: QiskitFunction) -> Optional[QiskitFunction]: """Uploads program.""" - pass @abstractmethod def get_functions(self, **kwargs) -> List[QiskitFunction]: """Returns list of available programs.""" - pass @abstractmethod def get_function( self, title: str, provider: Optional[str] = None ) -> Optional[QiskitFunction]: """Returns program based on parameters.""" - pass def get( self, title: str, provider: Optional[str] = None ) -> Optional[QiskitFunction]: + """Returns program based on parameters.""" warnings.warn( "`get` method has been deprecated. " "And will be removed in future releases. " @@ -206,6 +204,7 @@ def get( return self.get_function(title, provider=provider) def list(self, **kwargs) -> List[QiskitFunction]: + """Returns list of available programs.""" warnings.warn( "`list` method has been deprecated. " "And will be removed in future releases. " @@ -221,7 +220,6 @@ def list(self, **kwargs) -> List[QiskitFunction]: @abstractmethod def files(self) -> List[str]: """Returns list of available files produced by programs to download.""" - pass @abstractmethod def file_download( @@ -231,17 +229,14 @@ def file_download( download_location: str = "./", ): """Download file.""" - pass @abstractmethod def file_delete(self, file: str): """Deletes file uploaded or produced by the programs,""" - pass @abstractmethod def file_upload(self, file: str): """Upload file.""" - pass ###################### ####### Widget ####### diff --git a/client/qiskit_serverless/core/clients/LocalClient.py b/client/qiskit_serverless/core/clients/local_client.py similarity index 96% rename from client/qiskit_serverless/core/clients/LocalClient.py rename to client/qiskit_serverless/core/clients/local_client.py index 38c40347e..5a871f193 100644 --- a/client/qiskit_serverless/core/clients/LocalClient.py +++ b/client/qiskit_serverless/core/clients/local_client.py @@ -88,6 +88,8 @@ def run( arguments: Optional[Dict[str, Any]] = None, config: Optional[Configuration] = None, ) -> Job: + # pylint: disable=too-many-locals + title = "" if isinstance(program, QiskitFunction): title = program.title else: @@ -129,8 +131,12 @@ def run( result = results.group(1) job = Job(job_id=str(uuid4()), client=self) - entry = {"status": status, "logs": output, "result": result, "job": job} - self._jobs[job.job_id] = entry + self._jobs[job.job_id] = { + "status": status, + "logs": output, + "result": result, + "job": job, + } return job def status(self, job_id: str): @@ -189,9 +195,7 @@ def get_functions(self, **kwargs) -> List[QiskitFunction]: def get_function( self, title: str, provider: Optional[str] = None ) -> Optional[QiskitFunction]: - functions = { - function.title: function for function in self.client.get_programs() - } + functions = {function.title: function for function in self.get_functions()} return functions.get(title) ##################### diff --git a/client/qiskit_serverless/core/clients/RayClient.py b/client/qiskit_serverless/core/clients/ray_client.py similarity index 91% rename from client/qiskit_serverless/core/clients/RayClient.py rename to client/qiskit_serverless/core/clients/ray_client.py index d708001bf..29b513cef 100644 --- a/client/qiskit_serverless/core/clients/RayClient.py +++ b/client/qiskit_serverless/core/clients/ray_client.py @@ -63,7 +63,7 @@ def __init__(self, host: str): >>> ray_provider = RayClient("http://localhost:8265") """ super().__init__("ray-client", host) - self.jobSubmissionClient = JobSubmissionClient(host) + self.job_submission_client = JobSubmissionClient(host) #################### ####### JOBS ####### @@ -76,7 +76,8 @@ def get_jobs(self, **kwargs) -> List[Job]: list of jobs. """ return [ - Job(job.job_id, client=self) for job in self.jobSubmissionClient.list_jobs() + Job(job.job_id, client=self) + for job in self.job_submission_client.list_jobs() ] def get_job(self, job_id: str) -> Optional[Job]: @@ -89,7 +90,7 @@ def get_job(self, job_id: str) -> Optional[Job]: Job instance """ return Job( - self.jobSubmissionClient.get_job_info(job_id).submission_id, client=self + self.job_submission_client.get_job_info(job_id).submission_id, client=self ) def run( @@ -115,7 +116,7 @@ def run( **{ENV_JOB_ARGUMENTS: json.dumps(arguments, cls=QiskitObjectsEncoder)}, } - job_id = self.jobSubmissionClient.submit_job( + job_id = self.job_submission_client.submit_job( entrypoint=entrypoint, submission_id=f"qs_{uuid4()}", runtime_env={ @@ -128,13 +129,13 @@ def run( def status(self, job_id: str) -> str: """Check status.""" - return self.jobSubmissionClient.get_job_status(job_id).value + return self.job_submission_client.get_job_status(job_id).value def stop( self, job_id: str, service: Optional[QiskitRuntimeService] = None ) -> Union[str, bool]: """Stops job/program.""" - return self.jobSubmissionClient.stop_job(job_id) + return self.job_submission_client.stop_job(job_id) def result(self, job_id: str) -> Any: """Return results.""" @@ -142,7 +143,7 @@ def result(self, job_id: str) -> Any: def logs(self, job_id: str) -> str: """Return logs.""" - return self.jobSubmissionClient.get_job_logs(job_id) + return self.job_submission_client.get_job_logs(job_id) def filtered_logs(self, job_id: str, **kwargs) -> str: """Return filtered logs.""" diff --git a/client/qiskit_serverless/core/clients/ServerlessClient.py b/client/qiskit_serverless/core/clients/serverless_client.py similarity index 100% rename from client/qiskit_serverless/core/clients/ServerlessClient.py rename to client/qiskit_serverless/core/clients/serverless_client.py diff --git a/client/qiskit_serverless/core/job.py b/client/qiskit_serverless/core/job.py index fae42ea97..03e6f6d42 100644 --- a/client/qiskit_serverless/core/job.py +++ b/client/qiskit_serverless/core/job.py @@ -48,16 +48,13 @@ ENV_JOB_ID_GATEWAY, ENV_GATEWAY_PROVIDER_VERSION, GATEWAY_PROVIDER_VERSION_DEFAULT, - MAX_ARTIFACT_FILE_SIZE_MB, ) -from qiskit_serverless.core.function import QiskitFunction -from qiskit_serverless.exception import QiskitServerlessException from qiskit_serverless.serializers.program_serializers import ( QiskitObjectsEncoder, QiskitObjectsDecoder, ) -from qiskit_serverless.utils.json import is_jsonable, safe_json_request +from qiskit_serverless.utils.json import is_jsonable RuntimeEnv = ray.runtime_env.RuntimeEnv diff --git a/client/tests/core/test_job.py b/client/tests/core/test_job.py index 5b7bde7aa..f0edcce0d 100644 --- a/client/tests/core/test_job.py +++ b/client/tests/core/test_job.py @@ -1,4 +1,6 @@ """Tests job.""" + +# pylint: disable=too-few-public-methods import os from unittest import TestCase from unittest.mock import MagicMock, Mock, patch @@ -18,6 +20,8 @@ class ResponseMock: + """Utility class to mock request.get response with a json""" + ok = True text = "{}" diff --git a/client/tests/core/test_pattern.py b/client/tests/core/test_pattern.py index 34f19daef..96234c531 100644 --- a/client/tests/core/test_pattern.py +++ b/client/tests/core/test_pattern.py @@ -1,16 +1,12 @@ """Tests jobs.""" import os -import ray - from testcontainers.compose import DockerCompose from qiskit_serverless import RayClient, QiskitFunction from qiskit_serverless.core.job import Job from tests.utils import wait_for_ray_ready, wait_for_job_completion -import time - resources_path = os.path.join( os.path.dirname(os.path.abspath(__file__)), "../resources" ) diff --git a/client/tests/utils.py b/client/tests/utils.py index f0b5a8ac8..1fe95cc89 100644 --- a/client/tests/utils.py +++ b/client/tests/utils.py @@ -1,18 +1,19 @@ """Test utils.""" import time -from qiskit_serverless import RayClient -from qiskit_serverless.core.job import Job from ray.dashboard.modules.job.sdk import JobSubmissionClient +from qiskit_serverless.core.job import Job + def wait_for_ray_ready(connection_url: str, timeout: int = 60): + """Utility function that waits for ray to be up.""" client = None must_finish = time.time() + timeout while time.time() < must_finish and not client: try: client = JobSubmissionClient(connection_url) - except: + except ConnectionError: time.sleep(1)