Skip to content

Commit 83d0657

Browse files
author
Mike Kistler
committed
Generated SDK updates to use convert_model helper
1 parent a035380 commit 83d0657

5 files changed

Lines changed: 109 additions & 98 deletions

File tree

watson_developer_cloud/conversation_v1.py

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -114,23 +114,13 @@ def create_workspace(self,
114114
:rtype: dict
115115
"""
116116
if intents is not None:
117-
intents = [
118-
x._to_dict() if hasattr(x, "_to_dict") else x for x in intents
119-
]
117+
intents = [self._convert_model(x) for x in intents]
120118
if entities is not None:
121-
entities = [
122-
x._to_dict() if hasattr(x, "_to_dict") else x for x in entities
123-
]
119+
entities = [self._convert_model(x) for x in entities]
124120
if dialog_nodes is not None:
125-
dialog_nodes = [
126-
x._to_dict() if hasattr(x, "_to_dict") else x
127-
for x in dialog_nodes
128-
]
121+
dialog_nodes = [self._convert_model(x) for x in dialog_nodes]
129122
if counterexamples is not None:
130-
counterexamples = [
131-
x._to_dict() if hasattr(x, "_to_dict") else x
132-
for x in counterexamples
133-
]
123+
counterexamples = [self._convert_model(x) for x in counterexamples]
134124
params = {'version': self.version}
135125
data = {
136126
'name': name,
@@ -252,23 +242,13 @@ def update_workspace(self,
252242
if workspace_id is None:
253243
raise ValueError('workspace_id must be provided')
254244
if intents is not None:
255-
intents = [
256-
x._to_dict() if hasattr(x, "_to_dict") else x for x in intents
257-
]
245+
intents = [self._convert_model(x) for x in intents]
258246
if entities is not None:
259-
entities = [
260-
x._to_dict() if hasattr(x, "_to_dict") else x for x in entities
261-
]
247+
entities = [self._convert_model(x) for x in entities]
262248
if dialog_nodes is not None:
263-
dialog_nodes = [
264-
x._to_dict() if hasattr(x, "_to_dict") else x
265-
for x in dialog_nodes
266-
]
249+
dialog_nodes = [self._convert_model(x) for x in dialog_nodes]
267250
if counterexamples is not None:
268-
counterexamples = [
269-
x._to_dict() if hasattr(x, "_to_dict") else x
270-
for x in counterexamples
271-
]
251+
counterexamples = [self._convert_model(x) for x in counterexamples]
272252
params = {'version': self.version}
273253
data = {
274254
'name': name,
@@ -317,21 +297,15 @@ def message(self,
317297
if workspace_id is None:
318298
raise ValueError('workspace_id must be provided')
319299
if input is not None:
320-
input = input._to_dict() if hasattr(input, '_to_dict') else input
300+
input = self._convert_model(input)
321301
if context is not None:
322-
context = context._to_dict() if hasattr(context,
323-
'_to_dict') else context
302+
context = self._convert_model(context)
324303
if entities is not None:
325-
entities = [
326-
x._to_dict() if hasattr(x, "_to_dict") else x for x in entities
327-
]
304+
entities = [self._convert_model(x) for x in entities]
328305
if intents is not None:
329-
intents = [
330-
x._to_dict() if hasattr(x, "_to_dict") else x for x in intents
331-
]
306+
intents = [self._convert_model(x) for x in intents]
332307
if output is not None:
333-
output = output._to_dict() if hasattr(output,
334-
'_to_dict') else output
308+
output = self._convert_model(output)
335309
params = {'version': self.version}
336310
data = {
337311
'input': input,
@@ -375,9 +349,7 @@ def create_intent(self,
375349
if intent is None:
376350
raise ValueError('intent must be provided')
377351
if examples is not None:
378-
examples = [
379-
x._to_dict() if hasattr(x, "_to_dict") else x for x in examples
380-
]
352+
examples = [self._convert_model(x) for x in examples]
381353
params = {'version': self.version}
382354
data = {
383355
'intent': intent,
@@ -501,10 +473,7 @@ def update_intent(self,
501473
if intent is None:
502474
raise ValueError('intent must be provided')
503475
if new_examples is not None:
504-
new_examples = [
505-
x._to_dict() if hasattr(x, "_to_dict") else x
506-
for x in new_examples
507-
]
476+
new_examples = [self._convert_model(x) for x in new_examples]
508477
params = {'version': self.version}
509478
data = {
510479
'intent': new_intent,
@@ -705,9 +674,7 @@ def create_entity(self,
705674
if entity is None:
706675
raise ValueError('entity must be provided')
707676
if values is not None:
708-
values = [
709-
x._to_dict() if hasattr(x, "_to_dict") else x for x in values
710-
]
677+
values = [self._convert_model(x) for x in values]
711678
params = {'version': self.version}
712679
data = {
713680
'entity': entity,
@@ -836,10 +803,7 @@ def update_entity(self,
836803
if entity is None:
837804
raise ValueError('entity must be provided')
838805
if new_values is not None:
839-
new_values = [
840-
x._to_dict() if hasattr(x, "_to_dict") else x
841-
for x in new_values
842-
]
806+
new_values = [self._convert_model(x) for x in new_values]
843807
params = {'version': self.version}
844808
data = {
845809
'entity': new_entity,
@@ -1276,12 +1240,9 @@ def create_dialog_node(self,
12761240
if dialog_node is None:
12771241
raise ValueError('dialog_node must be provided')
12781242
if next_step is not None:
1279-
next_step = next_step._to_dict() if hasattr(
1280-
next_step, '_to_dict') else next_step
1243+
next_step = self._convert_model(next_step)
12811244
if actions is not None:
1282-
actions = [
1283-
x._to_dict() if hasattr(x, "_to_dict") else x for x in actions
1284-
]
1245+
actions = [self._convert_model(x) for x in actions]
12851246
params = {'version': self.version}
12861247
data = {
12871248
'dialog_node': dialog_node,
@@ -1437,13 +1398,9 @@ def update_dialog_node(self,
14371398
if new_dialog_node is None:
14381399
raise ValueError('new_dialog_node must be provided')
14391400
if new_next_step is not None:
1440-
new_next_step = new_next_step._to_dict() if hasattr(
1441-
new_next_step, '_to_dict') else new_next_step
1401+
new_next_step = self._convert_model(new_next_step)
14421402
if new_actions is not None:
1443-
new_actions = [
1444-
x._to_dict() if hasattr(x, "_to_dict") else x
1445-
for x in new_actions
1446-
]
1403+
new_actions = [self._convert_model(x) for x in new_actions]
14471404
params = {'version': self.version}
14481405
data = {
14491406
'dialog_node': new_dialog_node,

watson_developer_cloud/discovery_v1.py

Lines changed: 80 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -258,18 +258,11 @@ def create_configuration(self,
258258
if name is None:
259259
raise ValueError('name must be provided')
260260
if conversions is not None:
261-
conversions = conversions._to_dict() if hasattr(
262-
conversions, '_to_dict') else conversions
261+
conversions = self._convert_model(conversions)
263262
if enrichments is not None:
264-
enrichments = [
265-
x._to_dict() if hasattr(x, "_to_dict") else x
266-
for x in enrichments
267-
]
263+
enrichments = [self._convert_model(x) for x in enrichments]
268264
if normalizations is not None:
269-
normalizations = [
270-
x._to_dict() if hasattr(x, "_to_dict") else x
271-
for x in normalizations
272-
]
265+
normalizations = [self._convert_model(x) for x in normalizations]
273266
params = {'version': self.version}
274267
data = {
275268
'name': name,
@@ -394,18 +387,11 @@ def update_configuration(self,
394387
if name is None:
395388
raise ValueError('name must be provided')
396389
if conversions is not None:
397-
conversions = conversions._to_dict() if hasattr(
398-
conversions, '_to_dict') else conversions
390+
conversions = self._convert_model(conversions)
399391
if enrichments is not None:
400-
enrichments = [
401-
x._to_dict() if hasattr(x, "_to_dict") else x
402-
for x in enrichments
403-
]
392+
enrichments = [self._convert_model(x) for x in enrichments]
404393
if normalizations is not None:
405-
normalizations = [
406-
x._to_dict() if hasattr(x, "_to_dict") else x
407-
for x in normalizations
408-
]
394+
normalizations = [self._convert_model(x) for x in normalizations]
409395
params = {'version': self.version}
410396
data = {
411397
'name': name,
@@ -1195,9 +1181,7 @@ def add_training_data(self,
11951181
if collection_id is None:
11961182
raise ValueError('collection_id must be provided')
11971183
if examples is not None:
1198-
examples = [
1199-
x._to_dict() if hasattr(x, "_to_dict") else x for x in examples
1200-
]
1184+
examples = [self._convert_model(x) for x in examples]
12011185
params = {'version': self.version}
12021186
data = {
12031187
'natural_language_query': natural_language_query,
@@ -1406,6 +1390,32 @@ def list_training_data(self, environment_id, collection_id):
14061390
accept_json=True)
14071391
return response
14081392

1393+
def list_training_examples(self, environment_id, collection_id, query_id):
1394+
"""
1395+
List all examples for this training data query.
1396+
1397+
:param str environment_id: The ID of the environment.
1398+
:param str collection_id: The ID of the collection.
1399+
:param str query_id: The ID of the query used for training.
1400+
:return: A `dict` containing the `TrainingExampleList` response.
1401+
:rtype: dict
1402+
"""
1403+
if environment_id is None:
1404+
raise ValueError('environment_id must be provided')
1405+
if collection_id is None:
1406+
raise ValueError('collection_id must be provided')
1407+
if query_id is None:
1408+
raise ValueError('query_id must be provided')
1409+
params = {'version': self.version}
1410+
response = self.request(
1411+
method='GET',
1412+
url=
1413+
'/v1/environments/{0}/collections/{1}/training_data/{2}/examples'.
1414+
format(environment_id, collection_id, query_id),
1415+
params=params,
1416+
accept_json=True)
1417+
return response
1418+
14091419
def update_training_example(self,
14101420
environment_id,
14111421
collection_id,
@@ -4458,6 +4468,53 @@ def __ne__(self, other):
44584468
return not self == other
44594469

44604470

4471+
class TrainingExampleList(object):
4472+
"""
4473+
TrainingExampleList.
4474+
4475+
:attr list[TrainingExample] examples: (optional)
4476+
"""
4477+
4478+
def __init__(self, examples=None):
4479+
"""
4480+
Initialize a TrainingExampleList object.
4481+
4482+
:param list[TrainingExample] examples: (optional)
4483+
"""
4484+
self.examples = examples
4485+
4486+
@classmethod
4487+
def _from_dict(cls, _dict):
4488+
"""Initialize a TrainingExampleList object from a json dictionary."""
4489+
args = {}
4490+
if 'examples' in _dict:
4491+
args['examples'] = [
4492+
TrainingExample._from_dict(x) for x in _dict['examples']
4493+
]
4494+
return cls(**args)
4495+
4496+
def _to_dict(self):
4497+
"""Return a json dictionary representing this model."""
4498+
_dict = {}
4499+
if hasattr(self, 'examples') and self.examples is not None:
4500+
_dict['examples'] = [x._to_dict() for x in self.examples]
4501+
return _dict
4502+
4503+
def __str__(self):
4504+
"""Return a `str` version of this TrainingExampleList object."""
4505+
return json.dumps(self._to_dict(), indent=2)
4506+
4507+
def __eq__(self, other):
4508+
"""Return `true` when self and other are equal, false otherwise."""
4509+
if not isinstance(other, self.__class__):
4510+
return False
4511+
return self.__dict__ == other.__dict__
4512+
4513+
def __ne__(self, other):
4514+
"""Return `true` when self and other are not equal, false otherwise."""
4515+
return not self == other
4516+
4517+
44614518
class TrainingQuery(object):
44624519
"""
44634520
TrainingQuery.

watson_developer_cloud/natural_language_classifier_v1.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@
1414
# See the License for the specific language governing permissions and
1515
# limitations under the License.
1616
"""
17-
The The IBM Watson Natural Language Classifier service uses machine learning algorithms to
18-
return the top matching predefined classes for short text input. You create and train a
19-
classifier to connect predefined classes to example texts so that the service can apply
20-
those classes to new inputs.
17+
IBM Watson Natural Language Classifier uses machine learning algorithms to return the top
18+
matching predefined classes for short text input. You create and train a classifier to
19+
connect predefined classes to example texts so that the service can apply those classes to
20+
new inputs.
2121
"""
2222

2323
from __future__ import absolute_import
@@ -117,11 +117,11 @@ def create_classifier(self,
117117
raise ValueError('training_data must be provided')
118118
if not metadata_filename and hasattr(metadata, 'name'):
119119
metadata_filename = metadata.name
120-
mime_type = 'application/octet-stream'
120+
mime_type = 'application/json'
121121
metadata_tuple = (metadata_filename, metadata, mime_type)
122122
if not training_data_filename and hasattr(training_data, 'name'):
123123
training_data_filename = training_data.name
124-
mime_type = 'application/octet-stream'
124+
mime_type = 'text/csv'
125125
training_data_tuple = (training_data_filename, training_data, mime_type)
126126
response = self.request(
127127
method='POST',

watson_developer_cloud/natural_language_understanding_v1.py

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,8 +155,7 @@ def analyze(self,
155155
"""
156156
if features is None:
157157
raise ValueError('features must be provided')
158-
features = features._to_dict() if hasattr(features,
159-
'_to_dict') else features
158+
features = self._convert_model(features)
160159
params = {'version': self.version}
161160
data = {
162161
'features': features,

watson_developer_cloud/tone_analyzer_v3.py

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -197,9 +197,7 @@ def tone_chat(self, utterances, accept_language=None):
197197
"""
198198
if utterances is None:
199199
raise ValueError('utterances must be provided')
200-
utterances = [
201-
x._to_dict() if hasattr(x, "_to_dict") else x for x in utterances
202-
]
200+
utterances = [self._convert_model(x) for x in utterances]
203201
headers = {'Accept-Language': accept_language}
204202
params = {'version': self.version}
205203
data = {'utterances': utterances}

0 commit comments

Comments
 (0)