Skip to content

Commit f65714e

Browse files
committed
Start adding "list item" types of objects with nicer accessors than raw dicts
1 parent fda5621 commit f65714e

File tree

3 files changed

+66
-8
lines changed

3 files changed

+66
-8
lines changed

dataikuapi/dss/dataset.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,52 @@
33
from ..utils import DataikuStreamedHttpUTF8CSVReader
44
from .future import DSSFuture
55
import json, warnings
6+
from .utils import DSSTaggableObjectListItem
67
from .future import DSSFuture
78
from .metrics import ComputedMetrics
89
from .discussion import DSSObjectDiscussions
910
from .statistics import DSSStatisticsWorksheet
1011
from . import recipe
1112

13+
class DSSDatasetListItem(DSSTaggableObjectListItem):
14+
"""An item in a list of datasets. Do not instantiate this class"""
15+
def __init__(self, client, data):
16+
super(DSSDatasetListItem, self).__init__(data)
17+
self.client = client
18+
19+
def to_dataset(self):
20+
"""Gets the :class:`DSSDataset` corresponding to this dataset"""
21+
return DSSDataset(self.client, self._data["projectKey"], self._data["name"])
22+
23+
@property
24+
def name(self):
25+
return self._data["name"]
26+
@property
27+
def id(self):
28+
return self._data["name"]
29+
@property
30+
def type(self):
31+
return self._data["type"]
32+
@property
33+
def schema(self):
34+
return self._data["schema"]
35+
36+
@property
37+
def connection(self):
38+
"""Returns the connection on which this dataset is attached, or None if there is no connection for this dataset"""
39+
if not "params" in self._data:
40+
return None
41+
return self._data["params"].get("connection", None)
42+
43+
def get_column(self, column):
44+
"""
45+
Returns the schema column given a name.
46+
:param str column: Column to find
47+
:return a dict of the column settings or None if column does not exist
48+
"""
49+
matched = [col for col in self.schema["columns"] if col["name"] == column]
50+
return None if len(matched) == 0 else matched[0]
51+
1252
class DSSDataset(object):
1353
"""
1454
A dataset on the DSS instance

dataikuapi/dss/project.py

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import time, warnings, sys, os.path as osp
2-
from .dataset import DSSDataset, DSSManagedDatasetCreationHelper
2+
from .dataset import DSSDataset, DSSDatasetListItem, DSSManagedDatasetCreationHelper
33
from .recipe import DSSRecipe
44
from . import recipe
55
from .managedfolder import DSSManagedFolder
@@ -208,15 +208,22 @@ def set_permissions(self, permissions):
208208
# Datasets
209209
########################################################
210210

211-
def list_datasets(self):
211+
def list_datasets(self, rtype="listitems"):
212212
"""
213-
List the datasets in this project
214-
215-
:returns: The list of the datasets, each one as a dictionary. Each dataset dict contains at least a `name` field which is the name of the dataset
216-
:rtype: list of dicts
213+
List the datasets in this project.
214+
215+
:param str rtype: How to return the list. Supported values are "listitems" and "objects".
216+
:returns: The list of the datasets. If "rtype" is "listitems", each one as a :class:`dataset.DSSDatasetListItem`.
217+
If "rtype" is "objects", each one as a :class:`dataset.DSSDataset`
218+
:rtype: list
217219
"""
218-
return self.client._perform_json(
219-
"GET", "/projects/%s/datasets/" % self.project_key)
220+
items = self.client._perform_json("GET", "/projects/%s/datasets/" % self.project_key)
221+
if rtype == "listitems":
222+
return [DSSDatasetListItem(self.client, item) for item in items]
223+
elif rtype == "objects":
224+
return [DSSDataset(self.client, self.project_key, item["name"]) for item in items]
225+
else:
226+
raise ValueError("Unknown rtype")
220227

221228
def get_dataset(self, dataset_name):
222229
"""

dataikuapi/dss/utils.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -88,3 +88,14 @@ def from_full(ref):
8888
return AnyLoc(elts[0], elts[1])
8989
else:
9090
raise Exception("Cannot parse object id, it's not a full id")
91+
92+
93+
class DSSTaggableObjectListItem(dict):
94+
"""An item in a list of taggable objects. Do not instantiate this class"""
95+
def __init__(self, data):
96+
super(DSSTaggableObjectListItem, self).__init__(data)
97+
self._data = data
98+
99+
@property
100+
def tags(self):
101+
return self._data["tags"]

0 commit comments

Comments
 (0)