Skip to content

Commit ef6e320

Browse files
committed
Make recipe name optional and add output dataset overwriting also for visual recipes
1 parent b2fada3 commit ef6e320

File tree

3 files changed

+27
-9
lines changed

3 files changed

+27
-9
lines changed

dataikuapi/dss/dataset.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,14 @@ def set_definition(self, definition):
132132
"PUT", "/projects/%s/datasets/%s" % (self.project_key, self.dataset_name),
133133
body=definition)
134134

135+
def exists(self):
136+
"""Returns whether this dataset exists"""
137+
try:
138+
self.get_metadata()
139+
return True
140+
except Exception as e:
141+
return False
142+
135143
########################################################
136144
# Dataset metadata
137145
########################################################
@@ -571,9 +579,6 @@ def get_as_core_dataset(self):
571579
def new_code_recipe(self, type, code=None, recipe_name=None):
572580
"""Starts creation of a new code recipe taking this dataset as input"""
573581

574-
if recipe_name is None:
575-
recipe_name = "%s_recipe_from_%s" % (type, self.dataset_name)
576-
577582
if type == "python":
578583
builder = recipe.PythonRecipeCreator(recipe_name, self.project)
579584
else:
@@ -584,13 +589,17 @@ def new_code_recipe(self, type, code=None, recipe_name=None):
584589
return builder
585590

586591
def new_grouping_recipe(self, first_group_by, recipe_name=None):
587-
if recipe_name is None:
588-
recipe_name = "group_%s" % (self.dataset_name)
589592
builder = recipe.GroupingRecipeCreator(recipe_name, self.project)
590593
builder.with_input(self.dataset_name)
591594
builder.with_group_key(first_group_by)
592595
return builder
593596

597+
def new_recipe(self, type, recipe_name=None):
598+
"""Starts creation of a new recipe taking this dataset as input"""
599+
builder = self.project.new_recipe(type, recipe_name)
600+
builder.with_input(self.dataset_name)
601+
return builder
602+
594603
class DSSDatasetSettings(object):
595604
def __init__(self, dataset, settings):
596605
self.dataset = dataset

dataikuapi/dss/project.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ def create_recipe(self, recipe_proto, creation_settings):
867867
body = definition)['name']
868868
return DSSRecipe(self.client, self.project_key, recipe_name)
869869

870-
def new_recipe(self, type, name):
870+
def new_recipe(self, type, name=None):
871871
"""
872872
Initializes the creation of a new recipe. Returns a :class:`dataikuapi.dss.recipe.DSSRecipeCreator`
873873
or one of its subclasses to complete the creation of the recipe.

dataikuapi/dss/recipe.py

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -448,6 +448,9 @@ def __init__(self, type, name, project):
448448
self.creation_settings = {
449449
}
450450

451+
def set_name(self, name):
452+
self.recipe_proto["name"] = name
453+
451454
def _build_ref(self, object_id, project_key=None):
452455
if project_key is not None and project_key != self.project.project_key:
453456
return project_key + '.' + object_id
@@ -547,10 +550,10 @@ def with_existing_output(self, dataset_name, append=False):
547550
self._with_output(dataset_name, append)
548551
return self
549552

550-
def with_new_output(self, name, connection_id, typeOptionId=None, format_option_id=None, override_sql_schema=None, partitioning_option_id=None, append=False, object_type='DATASET'):
553+
def with_new_output(self, name, connection_id, typeOptionId=None, format_option_id=None, override_sql_schema=None, partitioning_option_id=None, append=False, object_type='DATASET', overwrite=False):
551554
"""
552555
Create a new dataset as output to the recipe-to-be-created. The dataset is not created immediately,
553-
but when the recipe is created (ie in the build() method)
556+
but when the recipe is created (ie in the create() method)
554557
555558
:param str name: name of the dataset or identifier of the managed folder
556559
:param str connection_id: name of the connection to create the dataset on
@@ -565,9 +568,15 @@ def with_new_output(self, name, connection_id, typeOptionId=None, format_option_
565568
:param append: whether the recipe should append or overwrite the output when running
566569
(note: not available for all dataset types)
567570
:param str object_type: DATASET or MANAGED_FOLDER
571+
:param overwrite: If the dataset being created already exists, overwrite it (and delete data)
568572
"""
569573
if object_type == 'DATASET':
570574
assert self.create_output_dataset is None
575+
576+
dataset = self.project.get_dataset(name)
577+
if overwrite and dataset.exists():
578+
dataset.delete(drop_data=True)
579+
571580
self.create_output_dataset = True
572581
self.output_dataset_settings = {'connectionId':connection_id,'typeOptionId':typeOptionId,'specificSettings':{'formatOptionId':format_option_id, 'overrideSQLSchema':override_sql_schema},'partitioningOptionId':partitioning_option_id}
573582
self._with_output(name, append)
@@ -952,7 +961,7 @@ def with_new_output_dataset(self, name, connection,
952961
Use None for not partitioning the output, "FIRST_INPUT" to copy from the first input of the recipe,
953962
"dataset:XXX" to copy from a dataset name, or "folder:XXX" to copy from a folder id
954963
:param append: whether the recipe should append or overwrite the output when running (note: not available for all dataset types)
955-
:param overwrite: If the object being created already exists, overwrite it
964+
:param overwrite: If the dataset being created already exists, overwrite it (and delete data)
956965
"""
957966

958967
ch = self.project.new_managed_dataset_creation_helper(name)

0 commit comments

Comments
 (0)