From 7c2bfc0ee37c39b5bba77c696892f1e540d0499f Mon Sep 17 00:00:00 2001 From: Arnaud PICHERY Date: Wed, 31 Mar 2021 20:08:48 +0200 Subject: [PATCH 1/2] Jupyter notebook API / last polish --- dataikuapi/dss/jupyternotebook.py | 21 +++++++++++++++++++++ dataikuapi/dss/project.py | 10 +++++----- 2 files changed, 26 insertions(+), 5 deletions(-) diff --git a/dataikuapi/dss/jupyternotebook.py b/dataikuapi/dss/jupyternotebook.py index ee6f4e95..b778db6c 100644 --- a/dataikuapi/dss/jupyternotebook.py +++ b/dataikuapi/dss/jupyternotebook.py @@ -1,4 +1,25 @@ from .discussion import DSSObjectDiscussions +from .utils import DSSTaggableObjectListItem + +class DSSJupyterNotebookListItem(DSSTaggableObjectListItem): + """An item in a list of Jupyter notebooks. Do not instantiate this class, use :meth:`dataikuapi.dss.project.DSSProject.list_jupyter_notebooks`""" + def __init__(self, client, data): + super(DSSJupyterNotebookListItem, self).__init__(data) + self.client = client + + def to_notebook(self): + """Gets the :class:`DSSJupyterNotebook` corresponding to this notebook""" + return DSSJupyterNotebook(self.client, self._data["projectKey"], self._data["name"]) + + @property + def name(self): + return self._data["name"] + @property + def language(self): + return self._data["language"] + @property + def kernel_spec(self): + return self._data["kernelSpec"] class DSSJupyterNotebook(object): def __init__(self, client, project_key, notebook_name): diff --git a/dataikuapi/dss/project.py b/dataikuapi/dss/project.py index 6ee2bc64..b01c4467 100644 --- a/dataikuapi/dss/project.py +++ b/dataikuapi/dss/project.py @@ -1,6 +1,6 @@ import time, warnings, sys, os.path as osp from .dataset import DSSDataset, DSSDatasetListItem, DSSManagedDatasetCreationHelper -from .jupyternotebook import DSSJupyterNotebook +from .jupyternotebook import DSSJupyterNotebook, DSSJupyterNotebookListItem from .notebook import DSSNotebook from .streaming_endpoint import DSSStreamingEndpoint, DSSStreamingEndpointListItem, DSSManagedStreamingEndpointCreationHelper from .recipe import DSSRecipeListItem, DSSRecipe @@ -839,11 +839,11 @@ def list_jupyter_notebooks(self, active=False, as_type="object"): :returns: The list of the notebooks. If "as_type" is "names", each one as a string, if "as_type" is "objects", each one as a :class:`dataikuapi.dss.notebook.DSSJupyterNotebook` :rtype: list of :class:`dataikuapi.dss.notebook.DSSJupyterNotebook` or list of String """ - notebook_names = self.client._perform_json("GET", "/projects/%s/jupyter-notebooks/" % self.project_key, params={"active": active}) - if as_type == "names" or as_type == "name": - return notebook_names + notebook_items = self.client._perform_json("GET", "/projects/%s/jupyter-notebooks/" % self.project_key, params={"active": active}) + if as_type == "listitems" or as_type == "listitem": + return [DSSJupyterNotebookListItem(self.client, notebook_item) for notebook_item in notebook_items] elif as_type == "objects" or as_type == "object": - return [DSSJupyterNotebook(self.client, self.project_key, notebook_name) for notebook_name in notebook_names] + return [DSSJupyterNotebook(self.client, self.project_key, notebook_item["name"]) for notebook_item in notebook_items] else: raise ValueError("Unknown as_type") From 9da478407e07d130425fa563d1a85b6e376c62e5 Mon Sep 17 00:00:00 2001 From: Sourcery AI <> Date: Wed, 31 Mar 2021 18:09:41 +0000 Subject: [PATCH 2/2] 'Refactored by Sourcery' --- dataikuapi/dss/project.py | 37 ++++++++++++++++++------------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/dataikuapi/dss/project.py b/dataikuapi/dss/project.py index b01c4467..4339bb41 100644 --- a/dataikuapi/dss/project.py +++ b/dataikuapi/dss/project.py @@ -193,8 +193,9 @@ def duplicate(self, target_project_key, if target_project_folder is not None: obj["targetProjectFolderId"] = target_project_folder.project_folder_id - ref = self.client._perform_json("POST", "/projects/%s/duplicate/" % self.project_key, body = obj) - return ref + return self.client._perform_json( + "POST", "/projects/%s/duplicate/" % self.project_key, body=obj + ) ######################################################## # Project infos @@ -263,9 +264,9 @@ def list_datasets(self, as_type="listitems"): :rtype: list """ items = self.client._perform_json("GET", "/projects/%s/datasets/" % self.project_key) - if as_type == "listitems" or as_type == "listitem": + if as_type in ["listitems", "listitem"]: return [DSSDatasetListItem(self.client, item) for item in items] - elif as_type == "objects" or as_type == "object": + elif as_type in ["objects", "object"]: return [DSSDataset(self.client, self.project_key, item["name"]) for item in items] else: raise ValueError("Unknown as_type") @@ -403,9 +404,9 @@ def list_streaming_endpoints(self, as_type="listitems"): :rtype: list """ items = self.client._perform_json("GET", "/projects/%s/streamingendpoints/" % self.project_key) - if as_type == "listitems" or as_type == "listitem": + if as_type in ["listitems", "listitem"]: return [DSSStreamingEndpointListItem(self.client, item) for item in items] - elif as_type == "objects" or as_type == "object": + elif as_type in ["objects", "object"]: return [DSSStreamingEndpoint(self.client, self.project_key, item["id"]) for item in items] else: raise ValueError("Unknown as_type") @@ -715,7 +716,7 @@ def list_model_evaluation_stores(self, as_type=None): :rtype: list """ items = self.client._perform_json("GET", "/projects/%s/modelevaluationstores/" % self.project_key) - if as_type == "objects" or as_type == "object": + if as_type in ["objects", "object"]: return [DSSModelEvaluationStore(self.client, self.project_key, item["id"]) for item in items] else: return items @@ -840,9 +841,9 @@ def list_jupyter_notebooks(self, active=False, as_type="object"): :rtype: list of :class:`dataikuapi.dss.notebook.DSSJupyterNotebook` or list of String """ notebook_items = self.client._perform_json("GET", "/projects/%s/jupyter-notebooks/" % self.project_key, params={"active": active}) - if as_type == "listitems" or as_type == "listitem": + if as_type in ["listitems", "listitem"]: return [DSSJupyterNotebookListItem(self.client, notebook_item) for notebook_item in notebook_items] - elif as_type == "objects" or as_type == "object": + elif as_type in ["objects", "object"]: return [DSSJupyterNotebook(self.client, self.project_key, notebook_item["name"]) for notebook_item in notebook_items] else: raise ValueError("Unknown as_type") @@ -922,9 +923,9 @@ def set_variables(self, obj): @param dict obj: must be a modified version of the object returned by get_variables """ - if not "standard" in obj: + if "standard" not in obj: raise ValueError("Missing 'standard' key in argument") - if not "local" in obj: + if "local" not in obj: raise ValueError("Missing 'local' key in argument") self.client._perform_empty( @@ -1141,9 +1142,9 @@ def list_recipes(self, as_type="listitems"): :rtype: list """ items = self.client._perform_json("GET", "/projects/%s/recipes/" % self.project_key) - if as_type == "listitems" or as_type == "listitem": + if as_type in ["listitems", "listitem"]: return [DSSRecipeListItem(self.client, item) for item in items] - elif as_type == "objects" or as_type == "object": + elif as_type in ["objects", "object"]: return [DSSRecipe(self.client, self.project_key, item["name"]) for item in items] else: raise ValueError("Unknown as_type") @@ -1229,7 +1230,7 @@ def new_recipe(self, type, name=None): return recipe.SamplingRecipeCreator(name, self) elif type == "split": return recipe.SplitRecipeCreator(name, self) - elif type == "prepare" or type == "shaker": + elif type in ["prepare", "shaker"]: return recipe.PrepareRecipeCreator(name, self) elif type == "prediction_scoring": return recipe.PredictionScoringRecipeCreator(name, self) @@ -1588,11 +1589,9 @@ def add_exposed_object(self, object_type, object_id, target_project): found_eo = {"type" : object_type, "localName" : object_id, "rules" : []} self.settings["exposedObjects"]["objects"].append(found_eo) - already_exists = False - for rule in found_eo["rules"]: - if rule["targetProject"] == target_project: - already_exists = True - break + already_exists = any( + rule["targetProject"] == target_project for rule in found_eo["rules"] + ) if not already_exists: found_eo["rules"].append({"targetProject": target_project})