0

I'm trying to understand google speech to text sample, it takes data from micro and convert the audio to text

Whole source code is here https://github.com/GoogleCloudPlatform/python-docs-samples/blob/master/speech/microphone/transcribe_streaming_mic.py#L181

generator will yield some data

  def generator(self):
        while not self.closed:
            #.....
            yield b''.join(data)
audio_generator = stream.generator()
requests = (types.StreamingRecognizeRequest(audio_content=content)
                    for content in audio_generator)
responses = client.streaming_recognize(streaming_config, requests)
listen_print_loop(responses)

I only know little about python, cannot understand above code, seems generator will not block if some data is yield.

What does the for .. in line mean?

If the audio data comes for ever the for loop will run forever? and when will client.streaming_recognize gets run?

===UPDATE

OK, now I know generator..

but still does not understand for in line, does it mean:

for content in audio_generator:
    requests = types.StreamingRecognizeRequest(audio_content=content)

?

vego
  • 545
  • 3
  • 11
  • Most of your question is answered by [the duplicate](https://stackoverflow.com/q/1756096/364696). The rest seems to be stumbling about caused by not understanding generators/generator expressions, so learning about them should answer your other questions. – ShadowRanger Oct 16 '19 at 00:56
  • @ShadowRanger thanks, how about the for in line? – vego Oct 16 '19 at 01:02
  • It's a generator expression. Think lazy list comprehension. This is also covered by the duplicate. – ShadowRanger Oct 16 '19 at 01:09
  • @ShadowRanger Is `requests` a tuple? because it's inside a parentheses. so every yield will call `types.StreamingRecognizeRequest` and return a value, so requests is a tuple? – vego Oct 16 '19 at 01:12
  • No. Read the top answer to the duplicate, it describes *exactly* what that does. You're guessing without reading. For the record, `tuple`s are not defined by the presence of parentheses (`x = 1, 2` makes a `tuple`) outside of the empty `tuple` (which does need them), you just need the parentheses in some cases to force proper grouping. – ShadowRanger Oct 16 '19 at 01:14
  • I have read. I understand that for will call next() automatically. so `content` will be the yield data. I still cannot understand `for in` line here(a function before `for in`), – vego Oct 16 '19 at 01:18
  • I see. requests is also a generator?? and client.streaming_recognize use generator as argument? – vego Oct 16 '19 at 01:28
  • [membership operator, `in`](https://docs.python.org/3/reference/expressions.html#membership-test-operations) – Trenton McKinney Oct 16 '19 at 02:53

0 Answers0