Skip to content

Commit 89da4c1

Browse files
Merge pull request #57 from dataiku/fix/dss70-function-mutable-args
style: make all mutable function arguments "None" and init in function body
2 parents 109afbb + a5d1975 commit 89da4c1

File tree

5 files changed

+95
-44
lines changed

5 files changed

+95
-44
lines changed

dataikuapi/dss/macro.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -33,16 +33,20 @@ def get_definition(self):
3333
return self.definition
3434

3535

36-
def run(self, params={}, admin_params={}, wait=True):
36+
def run(self, params=None, admin_params=None, wait=True):
3737
"""
3838
Run the macro from the project
3939
40-
:param params: parameters to the macro run
41-
:param admin_params: admin parameters to the macro run (if the authentication of
42-
the api client does not cover admin rights, they are ignored)
40+
:param dict params: parameters to the macro run (defaults to `{}`)
41+
:param dict admin_params: admin parameters to the macro run (if the authentication of
42+
the api client does not cover admin rights, they are ignored, defaults to `{}`)
4343
:param wait: if True, the call blocks until the run is finished
4444
:returns: a run identifier to use to abort or retrieve results
4545
"""
46+
if params is None:
47+
params = {}
48+
if admin_params is None:
49+
admin_params = {}
4650
return self.client._perform_json(
4751
"POST", "/projects/%s/runnables/%s" % (self.project_key, self.runnable_type),
4852
params={'wait':wait}, body={'params':params, 'adminParams':admin_params})['runId']
@@ -88,4 +92,3 @@ def get_result(self, run_id, as_type=None):
8892
else:
8993
return resp.raw
9094

91-

dataikuapi/dss/ml.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1322,11 +1322,11 @@ def train(self, session_name=None, session_description=None):
13221322
self.wait_train_complete()
13231323
return self.get_trained_models_ids(session_id = train_ret["sessionId"])
13241324

1325-
def ensemble(self, model_ids=[], method=None):
1325+
def ensemble(self, model_ids=None, method=None):
13261326
"""
13271327
Create an ensemble model of a set of models
13281328
1329-
:param list model_ids: A list of model identifiers
1329+
:param list model_ids: A list of model identifiers (defaults to `[]`)
13301330
:param str method: the ensembling method. One of: AVERAGE, PROBA_AVERAGE, MEDIAN, VOTE, LINEAR_MODEL, LOGISTIC_MODEL
13311331
13321332
This method waits for the ensemble train to complete. If you want to train asynchronously, use :meth:`start_ensembling` and :meth:`wait_train_complete`
@@ -1340,6 +1340,8 @@ def ensemble(self, model_ids=[], method=None):
13401340
:return: A model identifier
13411341
:rtype: string
13421342
"""
1343+
if model_ids is None:
1344+
model_ids = []
13431345
train_ret = self.start_ensembling(model_ids, method)
13441346
self.wait_train_complete()
13451347
return train_ret
@@ -1363,18 +1365,20 @@ def start_train(self, session_name=None, session_description=None):
13631365
"POST", "/projects/%s/models/lab/%s/%s/train" % (self.project_key, self.analysis_id, self.mltask_id), body=session_info)
13641366

13651367

1366-
def start_ensembling(self, model_ids=[], method=None):
1368+
def start_ensembling(self, model_ids=None, method=None):
13671369
"""
13681370
Creates asynchronously a new ensemble models of a set of models.
13691371
1370-
:param list model_ids: A list of model identifiers
1372+
:param list model_ids: A list of model identifiers (defaults to `[]`)
13711373
:param str method: the ensembling method (AVERAGE, PROBA_AVERAGE, MEDIAN, VOTE, LINEAR_MODEL, LOGISTIC_MODEL)
13721374
13731375
This returns immediately, before train is complete. To wait for train to complete, use :meth:`wait_train_complete`
13741376
13751377
:return: the model identifier of the ensemble
13761378
:rtype: string
13771379
"""
1380+
if model_ids is None:
1381+
model_ids = []
13781382
ensembling_request = {
13791383
"method" : method,
13801384
"modelsIds" : model_ids

dataikuapi/dss/project.py

Lines changed: 40 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,12 @@ def delete(self, drop_data=False):
4949
# Project export
5050
########################################################
5151

52-
def get_export_stream(self, options = {}):
52+
def get_export_stream(self, options=None):
5353
"""
5454
Return a stream of the exported project
5555
You need to close the stream after download. Failure to do so will result in the DSSClient becoming unusable.
5656
57-
:param dict options: Dictionary of export options. The following options are available:
57+
:param dict options: Dictionary of export options (defaults to `{}`). The following options are available:
5858
5959
* exportUploads (boolean): Exports the data of Uploaded datasets - default False
6060
* exportManagedFS (boolean): Exports the data of managed Filesystem datasets - default False
@@ -70,15 +70,31 @@ def get_export_stream(self, options = {}):
7070
:returns: a file-like obbject that is a stream of the export archive
7171
:rtype: file-like
7272
"""
73+
if options is None:
74+
options = {}
7375
return self.client._perform_raw(
7476
"POST", "/projects/%s/export" % self.project_key, body=options).raw
7577

76-
def export_to_file(self, path, options={}):
78+
def export_to_file(self, path, options=None):
7779
"""
7880
Export the project to a file
7981
8082
:param str path: the path of the file in which the exported project should be saved
83+
:param dict options: Dictionary of export options (defaults to `{}`). The following options are available:
84+
85+
* exportUploads (boolean): Exports the data of Uploaded datasets - default False
86+
* exportManagedFS (boolean): Exports the data of managed Filesystem datasets - default False
87+
* exportAnalysisModels (boolean): Exports the models trained in analysis - default False
88+
* exportSavedModels (boolean): Exports the models trained in saved models - default False
89+
* exportManagedFolders (boolean): Exports the data of managed folders - default False
90+
* exportAllInputDatasets (boolean): Exports the data of all input datasets - default False
91+
* exportAllDatasets (boolean): Exports the data of all datasets - default False
92+
* exportAllInputManagedFolders (boolean): Exports the data of all input managed folders - default False
93+
* exportGitRepositoy (boolean): Exports the Git repository history - default False
94+
* exportInsightsData (boolean): Exports the data of static insights - default False
8195
"""
96+
if options is None:
97+
options = {}
8298
with open(path, 'wb') as f:
8399
export_stream = self.client._perform_raw(
84100
"POST", "/projects/%s/export" % self.project_key, body=options)
@@ -98,7 +114,7 @@ def duplicate(self, target_project_key,
98114
export_saved_models=True,
99115
export_git_repository=True,
100116
export_insights_data=True,
101-
remapping={},
117+
remapping=None,
102118
target_project_folder=None):
103119
"""
104120
Duplicate the project
@@ -110,13 +126,14 @@ def duplicate(self, target_project_key,
110126
:param bool export_saved_models:
111127
:param bool export_git_repository:
112128
:param bool export_insights_data:
113-
:param dict remapping: dict of connections to be remapped for the new project
129+
:param dict remapping: dict of connections to be remapped for the new project (defaults to `{}`)
114130
:param target_project_folder: the project folder where to put the duplicated project
115131
:type target_project_folder: A :class:`dataikuapi.dss.projectfolder.DSSProjectFolder
116132
:returns: A dict containing the original and duplicated project's keys
117133
:rtype: :class:`ProjectDuplicateResult`
118134
"""
119-
135+
if remapping is None:
136+
remapping = {}
120137
obj = {
121138
"targetProjectName": target_project_name,
122139
"targetProjectKey": target_project_key,
@@ -211,7 +228,7 @@ def get_dataset(self, dataset_name):
211228
return DSSDataset(self.client, self.project_key, dataset_name)
212229

213230
def create_dataset(self, dataset_name, type,
214-
params={}, formatType=None, formatParams={}):
231+
params=None, formatType=None, formatParams=None):
215232
"""
216233
Create a new dataset in the project, and return a handle to interact with it.
217234
@@ -225,13 +242,17 @@ def create_dataset(self, dataset_name, type,
225242
226243
:param string dataset_name: the name for the new dataset
227244
:param string type: the type of the dataset
228-
:param dict params: the parameters for the type, as a JSON object
245+
:param dict params: the parameters for the type, as a JSON object (defaults to `{}`)
229246
:param string formatType: an optional format to create the dataset with (only for file-oriented datasets)
230-
:param string formatParams: the parameters to the format, as a JSON object (only for file-oriented datasets)
247+
:param dict formatParams: the parameters to the format, as a JSON object (only for file-oriented datasets, default to `{}`)
231248
232249
Returns:
233250
A :class:`dataikuapi.dss.dataset.DSSDataset` dataset handle
234251
"""
252+
if params is None:
253+
params = {}
254+
if formatParams is None:
255+
formatParams = {}
235256
obj = {
236257
"name" : dataset_name,
237258
"projectKey" : self.project_key,
@@ -676,18 +697,20 @@ def get_scenario(self, scenario_id):
676697
"""
677698
return DSSScenario(self.client, self.project_key, scenario_id)
678699

679-
def create_scenario(self, scenario_name, type, definition={'params': {}}):
700+
def create_scenario(self, scenario_name, type, definition=None):
680701
"""
681702
Create a new scenario in the project, and return a handle to interact with it
682703
683704
:param str scenario_name: The name for the new scenario. This does not need to be unique
684705
(although this is strongly recommended)
685706
:param str type: The type of the scenario. MUst be one of 'step_based' or 'custom_python'
686-
:param object definition: the JSON definition of the scenario. Use ``get_definition(with_status=False)`` on an
687-
existing ``DSSScenario`` object in order to get a sample definition object
707+
:param dict definition: the JSON definition of the scenario. Use ``get_definition(with_status=False)`` on an
708+
existing ``DSSScenario`` object in order to get a sample definition object (defaults to `{'params': {}}`)
688709
689710
:returns: a :class:`.scenario.DSSScenario` handle to interact with the newly-created scenario
690711
"""
712+
if definition is None:
713+
definition = {'params': {}}
691714
definition['type'] = type
692715
definition['name'] = scenario_name
693716
scenario_id = self.client._perform_json("POST", "/projects/%s/scenarios/" % self.project_key,
@@ -790,12 +813,14 @@ def get_tags(self):
790813
"""
791814
return self.client._perform_json("GET", "/projects/%s/tags" % self.project_key)
792815

793-
def set_tags(self, tags={}):
816+
def set_tags(self, tags=None):
794817
"""
795818
Set the tags of this project.
796-
@param obj: must be a modified version of the object returned by list_tags
819+
:param dict tags: must be a modified version of the object returned by list_tags (defaults to `{}`)
797820
"""
798-
return self.client._perform_empty("PUT", "/projects/%s/tags" % self.project_key, body = tags)
821+
if tags is None:
822+
tags = {}
823+
return self.client._perform_empty("PUT", "/projects/%s/tags" % self.project_key, body=tags)
799824

800825

801826
########################################################

dataikuapi/dss/scenario.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,12 +20,14 @@ def abort(self):
2020
return self.client._perform_json(
2121
"POST", "/projects/%s/scenarios/%s/abort" % (self.project_key, self.id))
2222

23-
def run(self, params={}):
23+
def run(self, params=None):
2424
"""
2525
Requests a run of the scenario, which will start after a few seconds.
2626
27-
:params dict params: additional parameters that will be passed to the scenario through trigger params
27+
:params dict params: additional parameters that will be passed to the scenario through trigger params (defaults to `{}`)
2828
"""
29+
if params is None:
30+
params = {}
2931
trigger_fire = self.client._perform_json(
3032
"POST", "/projects/%s/scenarios/%s/run" % (self.project_key, self.id), body=params)
3133
return DSSTriggerFire(self, trigger_fire)
@@ -47,16 +49,16 @@ def get_trigger_fire(self, trigger_id, trigger_run_id):
4749
})
4850
return DSSTriggerFire(self, trigger_fire)
4951

50-
def run_and_wait(self, params={}, no_fail=False):
52+
def run_and_wait(self, params=None, no_fail=False):
5153
"""
5254
Requests a run of the scenario, which will start after a few seconds. Wait the end of the run to complete.
5355
54-
Args:
55-
params: additional parameters that will be passed to the scenario through trigger params
56+
:param dict params: additional parameters that will be passed to the scenario through trigger params (defaults to `{}`)
5657
57-
Returns:
58-
A :class:`dataikuapi.dss.admin.DSSScenarioRun` run handle
58+
:return: A :class:`dataikuapi.dss.admin.DSSScenarioRun` run handle
5959
"""
60+
if params is None:
61+
params = {}
6062
trigger_fire = self.run(params)
6163
scenario_run = trigger_fire.wait_for_scenario_run(no_fail)
6264
waiter = DSSScenarioRunWaiter(scenario_run, trigger_fire)

0 commit comments

Comments
 (0)