Skip to content

Commit 458e9f1

Browse files
committed
Encapsulate card & computation results
Avoiding raw dicts will give us more flexibility to improve the Python client in the future
1 parent 602f820 commit 458e9f1

File tree

2 files changed

+43
-10
lines changed

2 files changed

+43
-10
lines changed

dataikuapi/dss/future.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ class DSSFuture(object):
44
"""
55
A future on the DSS instance
66
"""
7-
def __init__(self, client, job_id, state=None):
7+
def __init__(self, client, job_id, state=None, result_wrapper=lambda result: result):
88
self.client = client
99
self.job_id = job_id
1010
self.state = state
1111
self.state_is_peek = True
12+
self.result_wrapper = result_wrapper
1213

1314
@classmethod
1415
def get_result_wait_if_needed(cls, client, ret):
@@ -50,7 +51,7 @@ def get_result(self):
5051
if self.state is None or not self.state.get('hasResult', False) or self.state_is_peek:
5152
self.get_state()
5253
if self.state.get('hasResult', False):
53-
return self.state.get('result', None)
54+
return self.result_wrapper(self.state.get('result', None))
5455
else:
5556
raise Exception("Result not ready")
5657

@@ -72,7 +73,7 @@ def wait_for_result(self):
7273
time.sleep(5)
7374
self.get_state()
7475
if self.state.get('hasResult', False):
75-
return self.state.get('result', None)
76+
return self.result_wrapper(self.state.get('result', None))
7677
else:
7778
raise Exception("No result")
7879

dataikuapi/dss/statistics.py

Lines changed: 39 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ def run_worksheet(self, wait=True):
4343
"""
4444
Computes the results of the whole worksheet.
4545
46-
:returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle
46+
:returns: a :class:`DSSStatisticsCardResult` if `wait` is `True`, or a :class:`~dataikuapi.dss.future.DSSFuture` handle otherwise
4747
"""
4848

4949
root_card = self.get_settings().get_raw()['rootCard']
@@ -57,7 +57,7 @@ def run_card(self, card, wait=True):
5757
5858
:param card: a card to compute
5959
:type card: :class:`DSSStatisticsCardSettings` or dict (obtained from ``DSSStatisticsCardSettings.get_raw()``)
60-
:returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle to the task of computing card's results
60+
:returns: a :class:`DSSStatisticsCardResult` if `wait` is `True`, or a :class:`~dataikuapi.dss.future.DSSFuture` handle otherwise
6161
"""
6262

6363
card = DSSStatisticsCardSettings._from_card_or_dict(self.client, card)
@@ -67,8 +67,13 @@ def run_card(self, card, wait=True):
6767
self.project_key, self.dataset_name, self.worksheet_id),
6868
body=card.get_raw()
6969
)
70-
future = DSSFuture(self.client, future_response.get(
71-
"jobId", None), future_response)
70+
71+
def wrap_card_result(result):
72+
return DSSStatisticsCardResult(result)
73+
74+
future = DSSFuture(self.client, future_response.get("jobId", None),
75+
future_response, result_wrapper=wrap_card_result)
76+
7277
return future.wait_for_result() if wait else future
7378

7479
def run_computation(self, computation, wait=True):
@@ -77,7 +82,7 @@ def run_computation(self, computation, wait=True):
7782
7883
:param computation: a card to compute
7984
:type computation: :class:`DSSStatisticsComputationSettings` or dict (obtained from ``DSSStatisticsComputationSettings.get_raw()``)
80-
:returns: a :class:`~dataikuapi.dss.future.DSSFuture` handle to the task of computing computation's results
85+
:returns: a :class:`DSSStatisticsComputationResult`, or a :class:`~dataikuapi.dss.future.DSSFuture` handle otherwise
8186
"""
8287

8388
computation = DSSStatisticsComputationSettings._from_computation_or_dict(
@@ -88,8 +93,13 @@ def run_computation(self, computation, wait=True):
8893
self.project_key, self.dataset_name, self.worksheet_id),
8994
body=computation.get_raw()
9095
)
91-
future = DSSFuture(self.client, future_response.get(
92-
"jobId", None), future_response)
96+
97+
def wrap_computation_result(result):
98+
return DSSStatisticsComputationResult(result)
99+
100+
future = DSSFuture(self.client, future_response.get("jobId", None),
101+
future_response, result_wrapper=wrap_computation_result)
102+
93103
return future.wait_for_result() if wait else future
94104

95105

@@ -152,6 +162,10 @@ def save(self):
152162

153163

154164
class DSSStatisticsCardSettings(DSSInternalDict):
165+
"""
166+
Object to manipulate the settings of a card
167+
"""
168+
155169
def __init__(self, client, card_definition):
156170
super(DSSStatisticsCardSettings, self).__init__(card_definition)
157171
self.client = client
@@ -175,7 +189,18 @@ def _from_card_or_dict(client, card_or_dict):
175189
return DSSStatisticsCardSettings(client, card_or_dict)
176190

177191

192+
class DSSStatisticsCardResult(DSSInternalDict):
193+
"""
194+
Object storing the results of a :class:`DSSStatisticsCardSettings`
195+
"""
196+
pass
197+
198+
178199
class DSSStatisticsComputationSettings(DSSInternalDict):
200+
"""
201+
Object to manipulate the settings of a computation
202+
"""
203+
179204
def __init__(self, computation_definition):
180205
super(DSSStatisticsComputationSettings,
181206
self).__init__(computation_definition)
@@ -186,3 +211,10 @@ def _from_computation_or_dict(computation_or_dict):
186211
if isinstance(computation_or_dict, DSSStatisticsComputationSettings):
187212
computation_or_dict = computation_or_dict.get_raw()
188213
return DSSStatisticsComputationSettings(computation_or_dict)
214+
215+
216+
class DSSStatisticsComputationResult(DSSInternalDict):
217+
"""
218+
Object storing the results of a :class:`DSSStatisticsComputationSettings`
219+
"""
220+
pass

0 commit comments

Comments
 (0)