Skip to content

Commit 180d48e

Browse files
Merge pull request #395 from watson-developer-cloud/pylint
Address pylint issues
2 parents 7b9de7c + 0447f90 commit 180d48e

18 files changed

Lines changed: 399 additions & 361 deletions

examples/conversation_v1.py

Lines changed: 158 additions & 137 deletions
Large diffs are not rendered by default.

examples/microphone-speech-to-text.py

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,18 @@
22
import pyaudio
33
import tempfile
44
from watson_developer_cloud import SpeechToTextV1
5-
from watson_developer_cloud.websocket import RecognizeCallback, RecognizeListener
5+
from watson_developer_cloud.websocket import RecognizeCallback
66

77
speech_to_text = SpeechToTextV1(
88
username='YOUR SERVICE USERNAME',
99
password='YOUR SERVICE PASSWORD',
1010
url='https://stream.watsonplatform.net/speech-to-text/api')
1111

12+
1213
# Example using websockets
1314
class MyRecognizeCallback(RecognizeCallback):
1415
def __init__(self):
15-
pass
16+
RecognizeCallback.__init__(self)
1617

1718
def on_transcription(self, transcript):
1819
print(transcript)
@@ -35,6 +36,7 @@ def on_transcription_complete(self):
3536
def on_hypothesis(self, hypothesis):
3637
print(hypothesis)
3738

39+
3840
mycallback = MyRecognizeCallback()
3941
tmp = tempfile.NamedTemporaryFile()
4042

@@ -45,9 +47,12 @@ def on_hypothesis(self, hypothesis):
4547
RECORD_SECONDS = 5
4648

4749
audio = pyaudio.PyAudio()
48-
stream = audio.open(format=FORMAT, channels=CHANNELS,
49-
rate=RATE, input=True,
50-
frames_per_buffer=CHUNK)
50+
stream = audio.open(
51+
format=FORMAT,
52+
channels=CHANNELS,
53+
rate=RATE,
54+
input=True,
55+
frames_per_buffer=CHUNK)
5156

5257
print('recording....')
5358
with open(tmp.name, 'w') as f:
@@ -61,4 +66,5 @@ def on_hypothesis(self, hypothesis):
6166
print('Done recording...')
6267

6368
with open(tmp.name) as f:
64-
speech_to_text.recognize_with_websocket(audio=f, recognize_callback=mycallback)
69+
speech_to_text.recognize_with_websocket(
70+
audio=f, recognize_callback=mycallback)

examples/speech_to_text_v1.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import json
33
from os.path import join, dirname
44
from watson_developer_cloud import SpeechToTextV1
5-
from watson_developer_cloud.websocket import RecognizeCallback, RecognizeListener
5+
from watson_developer_cloud.websocket import RecognizeCallback
66

77
speech_to_text = SpeechToTextV1(
88
username='YOUR SERVICE USERNAME',
@@ -27,7 +27,7 @@
2727
# Example using websockets
2828
class MyRecognizeCallback(RecognizeCallback):
2929
def __init__(self):
30-
pass
30+
RecognizeCallback.__init__(self)
3131

3232
def on_transcription(self, transcript):
3333
print(transcript)

test/integration/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
from dotenv import load_dotenv, find_dotenv
1717

1818
# load the .env file containing your environment variables for the required
19-
load_dotenv(find_dotenv())
19+
load_dotenv(find_dotenv())

test/integration/test_integration_discovery_v1.py

Lines changed: 61 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -3,79 +3,104 @@
33
from unittest import TestCase
44
import os
55
import watson_developer_cloud
6-
import json
76
import random
87

8+
99
class Discoveryv1(TestCase):
1010
def setUp(self):
1111
self.discovery = watson_developer_cloud.DiscoveryV1(
1212
version='2017-10-16',
1313
username=os.getenv('DISCOVERY_TO_TEXT_USERNAME'),
1414
password=os.getenv('DISCOVERY_TO_TEXT_PASSWORD'))
15-
self.discovery.set_default_headers({'X-Watson-Learning-Opt-Out': '1', 'X-Watson-Test': '1'})
16-
self.environment_id = 'e15f6424-f887-4f50-b4ea-68267c36fc9c' # This environment is created for integration testing
17-
self.collection_id = self.discovery.list_collections(self.environment_id)['collections'][0]['collection_id']
15+
self.discovery.set_default_headers({
16+
'X-Watson-Learning-Opt-Out': '1',
17+
'X-Watson-Test': '1'
18+
})
19+
self.environment_id = 'e15f6424-f887-4f50-b4ea-68267c36fc9c' # This environment is created for integration testing
20+
self.collection_id = self.discovery.list_collections(
21+
self.environment_id)['collections'][0]['collection_id']
1822

1923
def test_environments(self):
2024
envs = self.discovery.list_environments()
2125
assert envs is not None
22-
env = self.discovery.get_environment(envs['environments'][0]['environment_id'])
26+
env = self.discovery.get_environment(
27+
envs['environments'][0]['environment_id'])
2328
assert env is not None
24-
fields = self.discovery.list_fields(self.environment_id, self.collection_id)
29+
fields = self.discovery.list_fields(self.environment_id,
30+
self.collection_id)
2531
assert fields is not None
2632

2733
def test_configurations(self):
2834
configs = self.discovery.list_configurations(self.environment_id)
2935
assert configs is not None
30-
new_configuration_id = self.discovery.create_configuration(self.environment_id, 'test', 'creating new config for python sdk')['configuration_id']
36+
new_configuration_id = self.discovery.create_configuration(
37+
self.environment_id, 'test',
38+
'creating new config for python sdk')['configuration_id']
3139
assert new_configuration_id is not None
32-
self.discovery.get_configuration(self.environment_id, new_configuration_id)
33-
updated_config = self.discovery.update_configuration(self.environment_id, new_configuration_id, 'lala')
40+
self.discovery.get_configuration(self.environment_id,
41+
new_configuration_id)
42+
updated_config = self.discovery.update_configuration(
43+
self.environment_id, new_configuration_id, 'lala')
3444
assert updated_config['name'] == 'lala'
35-
deleted_config = self.discovery.delete_configuration(self.environment_id, new_configuration_id)
45+
deleted_config = self.discovery.delete_configuration(
46+
self.environment_id, new_configuration_id)
3647
assert deleted_config['status'] == 'deleted'
3748

3849
def test_collections_and_expansions(self):
39-
new_collection_id = self.discovery.create_collection(self.environment_id,
40-
name='Example collection for python' + random.choice('ABCDEFGHIJKLMNOPQ'),
41-
description="Integration test for python sdk")['collection_id']
50+
new_collection_id = self.discovery.create_collection(
51+
self.environment_id,
52+
name='Example collection for python' +
53+
random.choice('ABCDEFGHIJKLMNOPQ'),
54+
description="Integration test for python sdk")['collection_id']
4255
assert new_collection_id is not None
4356
self.discovery.get_collection(self.environment_id, new_collection_id)
44-
updated_collection = self.discovery.update_collection(self.environment_id, new_collection_id, name='lala')
57+
updated_collection = self.discovery.update_collection(
58+
self.environment_id, new_collection_id, name='lala')
4559
assert updated_collection['name'] == 'lala'
4660

47-
self.discovery.create_expansions(self.environment_id, new_collection_id, [{'input_terms': ['a'], 'expanded_terms': ['aa']}])
48-
expansions = self.discovery.list_expansions(self.environment_id, new_collection_id)
49-
assert len(expansions['expansions']) > 0
50-
self.discovery.delete_expansions(self.environment_id, new_collection_id)
61+
self.discovery.create_expansions(self.environment_id,
62+
new_collection_id, [{
63+
'input_terms': ['a'],
64+
'expanded_terms': ['aa']
65+
}])
66+
expansions = self.discovery.list_expansions(self.environment_id,
67+
new_collection_id)
68+
assert expansions['expansions']
69+
self.discovery.delete_expansions(self.environment_id,
70+
new_collection_id)
5171

52-
deleted_collection = self.discovery.delete_collection(self.environment_id, new_collection_id)
72+
deleted_collection = self.discovery.delete_collection(
73+
self.environment_id, new_collection_id)
5374
assert deleted_collection['status'] == 'deleted'
5475

5576
def test_documents(self):
5677
with open(os.path.join(os.path.dirname(__file__), '../../resources/simple.html'), 'r') as fileinfo:
57-
add_doc = self.discovery.add_document(environment_id=self.environment_id,
58-
collection_id=self.collection_id,
59-
file=fileinfo)
60-
add_doc['document_id'] is not None
78+
add_doc = self.discovery.add_document(
79+
environment_id=self.environment_id,
80+
collection_id=self.collection_id,
81+
file=fileinfo)
82+
assert add_doc['document_id'] is not None
6183

62-
doc_status = self.discovery.get_document_status(self.environment_id, self.collection_id, add_doc['document_id'])
84+
doc_status = self.discovery.get_document_status(
85+
self.environment_id, self.collection_id, add_doc['document_id'])
6386
assert doc_status is not None
6487

6588
with open(os.path.join(os.path.dirname(__file__), '../../resources/simple.html'), 'r') as fileinfo:
66-
update_doc = self.discovery.update_document(self.environment_id,
67-
self.collection_id,
68-
add_doc['document_id'],
69-
file=fileinfo,
70-
filename='newname.html')
89+
update_doc = self.discovery.update_document(
90+
self.environment_id,
91+
self.collection_id,
92+
add_doc['document_id'],
93+
file=fileinfo,
94+
filename='newname.html')
7195
assert update_doc is not None
72-
delete_doc = self.discovery.delete_document(self.environment_id, self.collection_id, add_doc['document_id'])
96+
delete_doc = self.discovery.delete_document(
97+
self.environment_id, self.collection_id, add_doc['document_id'])
7398
assert delete_doc['status'] == 'deleted'
7499

75-
76100
def test_queries(self):
77-
query_results = self.discovery.query(self.environment_id,
78-
self.collection_id,
79-
filter='extracted_metadata.sha1::9181d244*',
80-
return_fields='extracted_metadata.sha1')
81-
assert query_results is not None
101+
query_results = self.discovery.query(
102+
self.environment_id,
103+
self.collection_id,
104+
filter='extracted_metadata.sha1::9181d244*',
105+
return_fields='extracted_metadata.sha1')
106+
assert query_results is not None

test/integration/test_integration_speech_to_text_v1.py

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,19 @@
11
from unittest import TestCase
2-
import pytest
32
import os
43
import watson_developer_cloud
5-
from watson_developer_cloud.speech_to_text_v1 import SpeechRecognitionResults
64

75

86
class TestSpeechToTextV1(TestCase):
97
def setUp(self):
108
self.speech_to_text = watson_developer_cloud.SpeechToTextV1(
119
username=os.getenv('SPEECH_TO_TEXT_USERNAME'),
1210
password=os.getenv('SPEECH_TO_TEXT_PASSWORD'))
13-
self.speech_to_text.set_default_headers({'X-Watson-Learning-Opt-Out': '1', 'X-Watson-Test': '1'})
11+
self.speech_to_text.set_default_headers({
12+
'X-Watson-Learning-Opt-Out':
13+
'1',
14+
'X-Watson-Test':
15+
'1'
16+
})
1417
self.custom_models = self.speech_to_text.list_language_models()
1518
self.create_custom_model = self.speech_to_text.create_language_model(
1619
name="integration_test_model",
@@ -37,10 +40,7 @@ def test_create_custom_model(self):
3740
self.custom_models['customizations']) >= 1
3841

3942
def test_recognize(self):
40-
with open(
41-
os.path.join(
42-
os.path.dirname(__file__), '../../resources/speech.wav'),
43-
'rb') as audio_file:
43+
with open(os.path.join(os.path.dirname(__file__), '../../resources/speech.wav'), 'rb') as audio_file:
4444
output = self.speech_to_text.recognize(
4545
audio=audio_file, content_type='audio/l16; rate=44100')
4646
assert output['results'][0]['alternatives'][0][
@@ -70,4 +70,5 @@ def test_acoustic_model(self):
7070
self.speech_to_text.reset_acoustic_model(
7171
get_acoustic_model['customization_id'])
7272

73-
self.speech_to_text.delete_acoustic_model(get_acoustic_model['customization_id'])
73+
self.speech_to_text.delete_acoustic_model(
74+
get_acoustic_model['customization_id'])

test/integration/test_integration_text_to_speech_v1.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,17 @@
1-
import pytest
1+
22
import unittest
33
import watson_developer_cloud
4-
import os
4+
55

66
class TestIntegrationTextToSpeechV1(unittest.TestCase):
77
def setUp(self):
88
self.text_to_speech = watson_developer_cloud.TextToSpeechV1()
9-
self.text_to_speech.set_default_headers({'X-Watson-Learning-Opt-Out': '1', 'X-Watson-Test': '1'})
9+
self.text_to_speech.set_default_headers({
10+
'X-Watson-Learning-Opt-Out':
11+
'1',
12+
'X-Watson-Test':
13+
'1'
14+
})
1015
self.original_customizations = self.text_to_speech.list_voice_models()
1116
self.created_customization = self.text_to_speech.create_voice_model(
1217
name="test_integration_customization",
@@ -27,11 +32,11 @@ def test_speak(self):
2732
text="my voice is my passport",
2833
accept='audio/wav',
2934
voice='en-US_AllisonVoice')
30-
output.content is not None
35+
assert output.content is not None
3136

3237
def test_pronunciation(self):
3338
output = self.text_to_speech.get_pronunciation('hello')
34-
output['pronunciation'] is not None
39+
assert output['pronunciation'] is not None
3540

3641
def test_customizations(self):
3742
old_length = len(self.original_customizations['customizations'])
@@ -44,16 +49,9 @@ def test_custom_words(self):
4449
words = self.text_to_speech.list_words(customization_id)['words']
4550
assert len(words) == 0
4651
self.text_to_speech.add_word(
47-
customization_id,
48-
word="ACLs",
49-
translation="ackles")
50-
51-
words = [
52-
{
53-
"word": "MACLs",
54-
"translation": "mackles"
55-
}
56-
]
52+
customization_id, word="ACLs", translation="ackles")
53+
54+
words = [{"word": "MACLs", "translation": "mackles"}]
5755

5856
self.text_to_speech.add_words(customization_id, words)
5957
self.text_to_speech.delete_word(customization_id, 'ACLs')

test/integration/test_integration_visual_recognition.py

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,29 +6,46 @@
66
import json
77

88
pytestmark = pytest.mark.skip('Run These Manually, they are destructive')
9-
class IntegrationTestVisualRecognitionV3(TestCase):
109

10+
11+
class IntegrationTestVisualRecognitionV3(TestCase):
1112
def setUp(self):
12-
self.visual_recognition = watson_developer_cloud.VisualRecognitionV3('2016-05-20', api_key=os.environ.get(
13-
'YOUR API KEY'))
14-
self.visual_recognition.set_default_headers({'X-Watson-Learning-Opt-Out': '1', 'X-Watson-Test': '1'})
13+
self.visual_recognition = watson_developer_cloud.VisualRecognitionV3(
14+
'2016-05-20', api_key=os.environ.get('YOUR API KEY'))
15+
self.visual_recognition.set_default_headers({
16+
'X-Watson-Learning-Opt-Out':
17+
'1',
18+
'X-Watson-Test':
19+
'1'
20+
})
1521

1622
def test_classify(self):
1723
car_path = join(dirname(__file__), '../../resources/cars.zip')
1824
with open(car_path, 'rb') as images_file:
19-
parameters = json.dumps({'threshold': 0.1, 'classifier_ids': ['CarsvsTrucks_1479118188', 'default']})
20-
car_results = self.visual_recognition.classify(images_file=images_file,
21-
parameters=parameters)
25+
parameters = json.dumps({
26+
'threshold':
27+
0.1,
28+
'classifier_ids': ['CarsvsTrucks_1479118188', 'default']
29+
})
30+
car_results = self.visual_recognition.classify(
31+
images_file=images_file, parameters=parameters)
2232
assert car_results is not None
2333

2434
def test_detect_faces(self):
25-
output = self.visual_recognition.detect_faces(parameters=json.dumps({'url': 'https://www.ibm.com/ibm/ginni/images/ginni_bio_780x981_v4_03162016.jpg'}))
35+
output = self.visual_recognition.detect_faces(
36+
parameters=json.dumps({
37+
'url':
38+
'https://www.ibm.com/ibm/ginni/images/ginni_bio_780x981_v4_03162016.jpg'
39+
}))
2640
assert output is not None
2741

2842
def test_custom_classifier(self):
2943
with open(os.path.join(os.path.dirname(__file__), '../../resources/cars.zip'), 'rb') as cars, \
3044
open(os.path.join(os.path.dirname(__file__), '../../resources/trucks.zip'), 'rb') as trucks:
31-
classifier = self.visual_recognition.create_classifier('Cars vs Trucks', cars_positive_examples=cars, negative_examples=trucks)
45+
classifier = self.visual_recognition.create_classifier(
46+
'Cars vs Trucks',
47+
cars_positive_examples=cars,
48+
negative_examples=trucks)
3249

3350
assert classifier is not None
3451

test/unit/__init__.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414
from dotenv import load_dotenv, find_dotenv
1515

1616
# load the .env file containing your environment variables for the required
17-
load_dotenv(find_dotenv())
17+
load_dotenv(find_dotenv())

0 commit comments

Comments
 (0)