Skip to content

Commit 29dc08a

Browse files
committed
Review cleanup for PR googleapis#2344.
1 parent ddee548 commit 29dc08a

File tree

8 files changed

+68
-56
lines changed

8 files changed

+68
-56
lines changed

docs/index.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,4 +237,4 @@ Cloud Storage
237237
client = storage.Client()
238238
bucket = client.get_bucket('<your-bucket-name>')
239239
blob = bucket.blob('my-test-file.txt')
240-
blob.upload_from_string('this is test content!')
240+
blob.upload_from_string('this is test content!')

docs/speech-client.rst

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,4 @@ Connection
1212
.. automodule:: google.cloud.speech.connection
1313
:members:
1414
:undoc-members:
15-
:show-inheritance:
15+
:show-inheritance:

docs/speech-usage.rst

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,17 +38,18 @@ and returns alternative text transcriptons.
3838

3939
.. code-block:: python
4040
41-
>>> alternatives = client.sync_recognize(None,"gs://my-bucket/recording.flac",
42-
... "FLAC", 16000, max_alternatives=2):
41+
>>> alternatives = client.sync_recognize(
42+
... None, 'gs://my-bucket/recording.flac',
43+
... 'FLAC', 16000, max_alternatives=2)
4344
>>> for alternative in alternatives:
4445
... print('=' * 20)
45-
... print(' transcript: %s' % (alternative["transcript"],))
46-
... print(' confidence: %s' % (alternative["confidence"],))
46+
... print('transcript: ' + alternative['transcript'])
47+
... print('confidence: ' + alternative['confidence'])
4748
====================
48-
transcript: Hello, this is a test
49-
confidence: 0.81
49+
transcript: Hello, this is a test
50+
confidence: 0.81
5051
====================
51-
transcript: Hello, this is one test
52-
confidence: 0
52+
transcript: Hello, this is one test
53+
confidence: 0
5354
54-
.. _sync_recognize: https://cloud.google.com/speech/reference/rest/v1beta1/speech/syncrecognize
55+
.. _sync_recognize: https://cloud.google.com/speech/reference/rest/v1beta1/speech/syncrecognize

google/cloud/speech/client.py

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016 Google Inc. All Rights Reserved.
1+
# Copyright 2016 Google Inc. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -70,8 +70,7 @@ class Client(client_module.Client):
7070

7171
def sync_recognize(self, content, source_uri, encoding, sample_rate,
7272
language_code=None, max_alternatives=None,
73-
profanity_filter=None,
74-
speech_context=None):
73+
profanity_filter=None, speech_context=None):
7574
"""Synchronous Speech Recognition.
7675
7776
.. _sync_recognize: https://cloud.google.com/speech/reference/\
@@ -84,10 +83,10 @@ def sync_recognize(self, content, source_uri, encoding, sample_rate,
8483
8584
:type source_uri: str
8685
:param source_uri: URI that points to a file that contains audio
87-
data bytes as specified in RecognitionConfig.
88-
Currently, only Google Cloud Storage URIs are
89-
supported, which must be specified in the following
90-
format: gs://bucket_name/object_name
86+
data bytes as specified in RecognitionConfig.
87+
Currently, only Google Cloud Storage URIs are
88+
supported, which must be specified in the following
89+
format: ``gs://bucket_name/object_name``.
9190
9291
:type encoding: str
9392
:param encoding: encoding of audio data sent in all RecognitionAudio
@@ -97,16 +96,16 @@ def sync_recognize(self, content, source_uri, encoding, sample_rate,
9796
9897
:type sample_rate: int
9998
:param sample_rate: Sample rate in Hertz of the audio data sent in all
100-
requests. Valid values are: 8000-48000.
101-
16000 is optimal. For best results, set the
102-
sampling rate of the audio source to 16000 Hz.
103-
If that's not possible, use the native sample rate
104-
of the audio source (instead of re-sampling).
99+
requests. Valid values are: 8000-48000. For best
100+
results, set the sampling rate of the audio source
101+
to 16000 Hz. If that's not possible, use the
102+
native sample rate of the audio source (instead of
103+
re-sampling).
105104
106105
:type language_code: str
107106
:param language_code: (Optional) The language of the supplied audio as
108-
BCP-47 language tag. Example: "en-GB".
109-
If omitted, defaults to "en-US".
107+
BCP-47 language tag. Example: ``'en-GB'``.
108+
If omitted, defaults to ``'en-US'``.
110109
111110
:type max_alternatives: int
112111
:param max_alternatives: (Optional) Maximum number of recognition
@@ -119,8 +118,8 @@ def sync_recognize(self, content, source_uri, encoding, sample_rate,
119118
:param profanity_filter: If True, the server will attempt to filter
120119
out profanities, replacing all but the
121120
initial character in each filtered word with
122-
asterisks, e.g. "f***". If False or omitted,
123-
profanities won't be filtered out.
121+
asterisks, e.g. ``'f***'``. If False or
122+
omitted, profanities won't be filtered out.
124123
125124
:type speech_context: list
126125
:param speech_context: A list of strings (max 50) containing words and
@@ -140,13 +139,14 @@ def sync_recognize(self, content, source_uri, encoding, sample_rate,
140139
between 0 and 1.
141140
"""
142141

143-
if (content is None) and (source_uri is None):
144-
raise ValueError('content and source_uri cannot be both equal to\
145-
None')
142+
if content is None and source_uri is None:
143+
raise ValueError('content and source_uri cannot be both '
144+
'equal to None')
145+
146+
if content is not None and source_uri is not None:
147+
raise ValueError('content and source_uri cannot be both '
148+
'different from None')
146149

147-
if (content is not None) and (source_uri is not None):
148-
raise ValueError('content and source_uri cannot be both different from\
149-
None')
150150
if encoding is None:
151151
raise ValueError('encoding cannot be None')
152152
if sample_rate is None:

scripts/verify_included_modules.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@
4444
'google.cloud.monitoring.__init__',
4545
'google.cloud.pubsub.__init__',
4646
'google.cloud.resource_manager.__init__',
47+
'google.cloud.speech.__init__',
4748
'google.cloud.storage.__init__',
4849
'google.cloud.streaming.__init__',
4950
'google.cloud.streaming.buffered_stream',
@@ -55,7 +56,6 @@
5556
'google.cloud.translate.__init__',
5657
'google.cloud.vision.__init__',
5758
'google.cloud.vision.fixtures',
58-
'google.cloud.speech.__init__',
5959
])
6060

6161

unit_tests/speech/_fixtures.py

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,13 +18,13 @@
1818
'alternatives': [
1919
{
2020
'transcript': 'hello',
21-
'confidence': 0.784919
22-
}
23-
]
24-
}
25-
]
21+
'confidence': 0.784919,
22+
},
23+
],
24+
},
25+
],
2626
}
2727

2828
SYNC_RECOGNIZE_EMPTY_RESPONSE = {
29-
'results': []
29+
'results': [],
3030
}

unit_tests/speech/test_client.py

Lines changed: 26 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016 Google Inc. All Rights Reserved.
1+
# Copyright 2016 Google Inc. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -14,12 +14,11 @@
1414

1515
import unittest
1616

17-
_AUDIO_SOURCE_URI = 'gs://sample-bucket/sample-recording.flac'
18-
1917

2018
class TestClient(unittest.TestCase):
2119
SAMPLE_RATE = 16000
2220
HINTS = ['hi']
21+
AUDIO_SOURCE_URI = 'gs://sample-bucket/sample-recording.flac'
2322

2423
def _getTargetClass(self):
2524
from google.cloud.speech.client import Client
@@ -78,9 +77,15 @@ def test_sync_recognize_content_with_optional_parameters(self):
7877
profanity_filter=True,
7978
speech_context=self.HINTS)
8079

81-
self.assertEqual(REQUEST,
82-
client.connection._requested[0]['data'])
83-
self.assertEqual(response[0]['transcript'], 'hello')
80+
self.assertEqual(len(client.connection._requested), 1)
81+
req = client.connection._requested[0]
82+
self.assertEqual(len(req), 3)
83+
self.assertEqual(req['data'], REQUEST)
84+
self.assertEqual(req['method'], 'POST')
85+
self.assertEqual(req['path'], 'syncrecognize')
86+
87+
expected = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives']
88+
self.assertEqual(response, expected)
8489

8590
def test_sync_recognize_source_uri_without_optional_parameters(self):
8691
from google.cloud.speech.client import Encoding
@@ -93,7 +98,7 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):
9398
'sampleRate': 16000,
9499
},
95100
'audio': {
96-
'uri': _AUDIO_SOURCE_URI,
101+
'uri': self.AUDIO_SOURCE_URI,
97102
}
98103
}
99104
credentials = _Credentials()
@@ -102,13 +107,19 @@ def test_sync_recognize_source_uri_without_optional_parameters(self):
102107

103108
encoding = Encoding.FLAC
104109

105-
response = client.sync_recognize(None, _AUDIO_SOURCE_URI,
110+
response = client.sync_recognize(None, self.AUDIO_SOURCE_URI,
106111
encoding,
107112
self.SAMPLE_RATE)
108113

109-
self.assertEqual(REQUEST,
110-
client.connection._requested[0]['data'])
111-
self.assertEqual(response[0]['transcript'], 'hello')
114+
self.assertEqual(len(client.connection._requested), 1)
115+
req = client.connection._requested[0]
116+
self.assertEqual(len(req), 3)
117+
self.assertEqual(req['data'], REQUEST)
118+
self.assertEqual(req['method'], 'POST')
119+
self.assertEqual(req['path'], 'syncrecognize')
120+
121+
expected = SYNC_RECOGNIZE_RESPONSE['results'][0]['alternatives']
122+
self.assertEqual(response, expected)
112123

113124
def test_sync_recognize_without_content_or_source_uri(self):
114125
from google.cloud.speech.client import Encoding
@@ -128,15 +139,15 @@ def test_sync_recognize_with_content_and_source_uri(self):
128139
client = self._makeOne(credentials=credentials)
129140

130141
with self.assertRaises(ValueError):
131-
client.sync_recognize(_AUDIO_CONTENT, _AUDIO_SOURCE_URI,
142+
client.sync_recognize(_AUDIO_CONTENT, self.AUDIO_SOURCE_URI,
132143
Encoding.FLAC, self.SAMPLE_RATE)
133144

134145
def test_sync_recognize_without_encoding(self):
135146
credentials = _Credentials()
136147
client = self._makeOne(credentials=credentials)
137148

138149
with self.assertRaises(ValueError):
139-
client.sync_recognize(None, _AUDIO_SOURCE_URI, None,
150+
client.sync_recognize(None, self.AUDIO_SOURCE_URI, None,
140151
self.SAMPLE_RATE)
141152

142153
def test_sync_recognize_without_samplerate(self):
@@ -146,7 +157,7 @@ def test_sync_recognize_without_samplerate(self):
146157
client = self._makeOne(credentials=credentials)
147158

148159
with self.assertRaises(ValueError):
149-
client.sync_recognize(None, _AUDIO_SOURCE_URI, Encoding.FLAC,
160+
client.sync_recognize(None, self.AUDIO_SOURCE_URI, Encoding.FLAC,
150161
None)
151162

152163
def test_sync_recognize_with_empty_results(self):
@@ -158,7 +169,7 @@ def test_sync_recognize_with_empty_results(self):
158169
client.connection = _Connection(SYNC_RECOGNIZE_EMPTY_RESPONSE)
159170

160171
with self.assertRaises(ValueError):
161-
client.sync_recognize(None, _AUDIO_SOURCE_URI, Encoding.FLAC,
172+
client.sync_recognize(None, self.AUDIO_SOURCE_URI, Encoding.FLAC,
162173
self.SAMPLE_RATE)
163174

164175

unit_tests/speech/test_connection.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2016 Google Inc. All Rights Reserved.
1+
# Copyright 2016 Google Inc. All rights reserved.
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.

0 commit comments

Comments
 (0)