From 1c5ed572eceb5f0b9b77d9c7a073718b407dfde1 Mon Sep 17 00:00:00 2001 From: Thomas Schultz Date: Fri, 28 Oct 2016 15:08:12 -0400 Subject: [PATCH] Clean up async docs and unify for future GAPIC async. --- docs/speech-usage.rst | 7 +++++-- speech/google/cloud/speech/operation.py | 5 +++-- speech/unit_tests/test_operation.py | 6 ++++-- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/docs/speech-usage.rst b/docs/speech-usage.rst index 32fc3e13a701..7ee2be7d49ec 100644 --- a/docs/speech-usage.rst +++ b/docs/speech-usage.rst @@ -69,9 +69,12 @@ See: `Speech Asynchronous Recognize`_ ... operation.poll() # API call >>> operation.complete True - >>> operation.results[0].transcript + >>> for result in operation.results: + ... print('=' * 20) + ... print(result.transcript) + ... print(result.confidence) + ==================== 'how old is the Brooklyn Bridge' - >>> operation.results[0].confidence 0.98267895 diff --git a/speech/google/cloud/speech/operation.py b/speech/google/cloud/speech/operation.py index 4ad09bd082ff..cb3ca4c61275 100644 --- a/speech/google/cloud/speech/operation.py +++ b/speech/google/cloud/speech/operation.py @@ -123,8 +123,9 @@ def _update(self, response): raw_results = response.get('response', {}).get('results', None) results = [] if raw_results: - for result in raw_results[0]['alternatives']: - results.append(Transcript.from_api_repr(result)) + for result in raw_results: + for alternative in result['alternatives']: + results.append(Transcript.from_api_repr(alternative)) if metadata: self._metadata = Metadata.from_api_repr(metadata) diff --git a/speech/unit_tests/test_operation.py b/speech/unit_tests/test_operation.py index 2ebce2c75ce1..6bf824a9d3d6 100644 --- a/speech/unit_tests/test_operation.py +++ b/speech/unit_tests/test_operation.py @@ -36,7 +36,7 @@ def test_ctor_defaults(self): def test_from_api_repr(self): from unit_tests._fixtures import OPERATION_COMPLETE_RESPONSE - from google.cloud.speech.operation import Transcript + from google.cloud.speech.transcript import Transcript from google.cloud.speech.metadata import Metadata RESPONSE = OPERATION_COMPLETE_RESPONSE @@ -46,10 +46,12 @@ def test_from_api_repr(self): self.assertEqual('123456789', operation.name) self.assertTrue(operation.complete) + self.assertEqual(len(operation.results), 1) self.assertIsInstance(operation.results[0], Transcript) self.assertEqual(operation.results[0].transcript, 'how old is the Brooklyn Bridge') - self.assertEqual(operation.results[0].confidence, 0.98267895) + self.assertEqual(operation.results[0].confidence, + 0.98267895) self.assertTrue(operation.complete) self.assertIsInstance(operation.metadata, Metadata) self.assertEqual(operation.metadata.progress_percent, 100)