[Python-talk] Generator Question

bruce.labitt at autoliv.com bruce.labitt at autoliv.com
Thu Aug 13 11:58:41 EDT 2009


kent3737 at gmail.com wrote on 08/13/2009 11:34:58 AM:

> On Thu, Aug 13, 2009 at 11:18 AM, <bruce.labitt at autoliv.com> wrote:
> 
> > A slightly better version, I hope...
> > This should generate n/4 = 512 arrays that are 10000 elements in 
length?
> 
> I don't use numarray so I'm not going to evaluate that code.

For the record, it works fine :)  At least for smaller arrays that I have 
looked at.
numpy and scipy are really nice - nearly all of the function of matlab! 
Combined with matplotlib one has almost all of the capabilities of matlab. 
 I hardly use my matlab anymore because of the great utility of python!

> 
> >> The concept is fine, I can't vouch for the details! If you create the
> >> arrays this way, then the first step in the generator chain is just
> >> arrays - make_array(freqsequence, firstarray, dt, otherparams)
> >> because that is already a generator.
> >
> > Can you explain this further?
> 
> An expression such as
>   (foo(x) for x in some_sequence)
> is a generator expression. Its value is a generator object that
> calculates elements on demand.
> 
> A function such as
> def make_foos(some_sequence):
>   for x in some_sequence:
>     yield foo(x)
> 
> is a generator function - the result of calling it is also a generator 
object.
> 
> My initial code snippet included the line
>  arrays = (make_array(seed) for seed in seeds)
> 
> This assumes that make_array() is an ordinary function that returns an
> array. The variable arrays gets the generator object that will
> generate arrays on demand.
> 
> Now you have a make_arrays() function that is a generator function. So
> you don't want to call it in a generator expression, you just call it
> directly:
>   arrays = make_arrays(...)
> 
> Kent

Hmm, I think I follow this.  Will the group of generators cascade the 
same? 
Say we use the previous example.  Something like this, or would I need to 
do something different?

seeds = [ <some sequence that defines the arrays to be created> ]
score = 0
while score < threshold:
   #arrays = (make_array(seed) for seed in seeds)
    arrays = make_array(seed)    # instead of line above
    fft_results = (compute_fft(array) for array in arrays)
    powers = (compute_power(result) for result in fft_results)
    score, seeds = optimize(powers)

This is really powerful stuff...

Bruce


******************************
Neither the footer nor anything else in this E-mail is intended to or constitutes an <br>electronic signature and/or legally binding agreement in the absence of an <br>express statement or Autoliv policy and/or procedure to the contrary.<br>This E-mail and any attachments hereto are Autoliv property and may contain legally <br>privileged, confidential and/or proprietary information.<br>The recipient of this E-mail is prohibited from distributing, copying, forwarding or in any way <br>disseminating any material contained within this E-mail without prior written <br>permission from the author. If you receive this E-mail in error, please <br>immediately notify the author and delete this E-mail.  Autoliv disclaims all <br>responsibility and liability for the consequences of any person who fails to <br>abide by the terms herein. <br>
******************************


More information about the Python-talk mailing list