[Tutor] Request for guidance about a python syntax
Dear Tutors, I am new to python. I am trying to understand ( use) the python implementation of goertzel implementation provided on http://www.black-aura.com/blog/2011/12/10/python-implementation-of-the-goertzel-algorithm-dtmf-decoding/ . This python program uses unpack_from() function to unpack string into array of ints, with the following statement: frames = struct.unpack_from(*%dH % nframes * **nchannels*, frames) I could know the use of unpack_from, but I could not understand the fmt part, i.e *%dH % nframes * nchannels*. Can you pls help me know, what is the purpose of two % signs in this statement? Thanks for your help in advance. Best Regards, banes ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Request for guidance about a python syntax
On Mon, Dec 19, 2011 at 10:54 AM, Ganesh Borse bganes...@gmail.com wrote: I could know the use of unpack_from, but I could not understand the fmt part, i.e *%dH % nframes * nchannels*. Can you pls help me know, what is the purpose of two % signs in this statement? That's python's string formatting. See http://docs.python.org/library/stdtypes.html#string-formatting-operations for details. Basically, it's building the format for the call to unpack_from on the fly. The %dH portion is calling for a single decimal value to become part of the string (that's the %d portion). That value comes from multiplying nframes by nchannels. So, for example, if nframes = 10 and nchannels = 24, then the string becomes 240H. That, in turn, tells the program to unpack a sequence of 240 unsigned short values from the buffer. -- Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Request for guidance about a python syntax
Jerry Hill wrote: On Mon, Dec 19, 2011 at 10:54 AM, Ganesh Borse bganes...@gmail.com wrote: I could know the use of unpack_from, but I could not understand the fmt part, i.e *%dH % nframes * nchannels*. Can you pls help me know, what is the purpose of two % signs in this statement? That's python's string formatting. See http://docs.python.org/library/stdtypes.html#string-formatting-operations for details. Basically, it's building the format for the call to unpack_from on the fly. The %dH portion is calling for a single decimal value to become part of the string (that's the %d portion). That value comes from multiplying nframes by nchannels. So, for example, if nframes = 10 and nchannels = 24, then the string becomes 240H. That, in turn, tells the program to unpack a sequence of 240 unsigned short values from the buffer. Close, but % and * have the same operator precedence. Therefore the expression %dH % nframes * nchannels is evaluated as (%dH % nframes) * nchannels So nframes = 2 nchannels = 3 %dH % nframes '2H' %dH % nframes * nchannels '2H2H2H' The original script still works because multiplying a string by an integer repeats the string, and the format 2H2H2H is equivalent to 6H, but %dH % (nframes * nchannels) is probably a bit more efficient, especially for large values of nchannels. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Request for guidance about a python syntax
On Mon, Dec 19, 2011 at 12:10 PM, Peter Otten __pete...@web.de wrote: Close, but % and * have the same operator precedence. Therefore the expression %dH % nframes * nchannels is evaluated as (%dH % nframes) * nchannels Thanks Peter, that's exactly correct. Maybe this will teach me not to post things without actually trying them in the interactive interpreter. -- Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Request for guidance about a python syntax
Hi, That's amazing. Thanks for sharing this information. Regards On Tue, Dec 20, 2011 at 2:31 AM, Jerry Hill malaclyp...@gmail.com wrote: On Mon, Dec 19, 2011 at 12:10 PM, Peter Otten __pete...@web.de wrote: Close, but % and * have the same operator precedence. Therefore the expression %dH % nframes * nchannels is evaluated as (%dH % nframes) * nchannels Thanks Peter, that's exactly correct. Maybe this will teach me not to post things without actually trying them in the interactive interpreter. -- Jerry ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor