Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do i pass a generator yield statement to another function. -Python
    primarykey
    data
    text
    <p>i've been reading on this site and can't seem to find the specific answer i want. i've tried reading david beasly's slides on iteration and generators but still can't quite get the answer i'm looking for though the question seems simple. i'm running a clock-based simulation (Brian for neural networking) and i have a generator that is manipulating the outputs and adding them to a running sum (in order for there to be n exponential decay for a simple low-pass filter). i then want to take the outputs of these generators, at each time-step and then use them in another function to update some of the state variables, it says that since the item is of the generator type i cannot do this. code and explanation of code is as follows:</p> <pre><code>import numpy our_range=numpy.arange(0*ms,duration+defaultclock.dt,defaultclock.dt) a=our_range c=defaultclock.t #this is a clock that is part of the program i'm running, it updates every #timestep and runs through the range given above def sum_tau(neuron): #this gives a running sum which i want to access (the alphas can be ignored as they are problem specific) for c in a: #had to express it like this (with c and a) or it wouldn't run if c ==0: x=0 elif defaultclock.still_running()==False: return else: x = x*(1-alpha) + p(neuron)*alpha print x yield x #p(neuron) just takes some of the neurons variables and gives a number b=sum_tau(DN) #this is just to specify the neuron we're working on, problem specific @network_operation def x(): b.next() </code></pre> <p>the @network_operation means that every clock timestep the function below will be executed, therefore updating the sum to it's required value. Now what i want to do here is update a value that is used for the simulation (where d is the output to another generator, not shown, but very similar to b) by typing:</p> <pre><code>ron= (d/(1-b)) </code></pre> <p>However, it says i cannot use a generator object in this way, i have used print statements to determine that that b (and d) give the outputs i want every timestep (when the simulation is run) but I cannot seem to take these outputs and do anything with them. (more specifically unsupported operand type '-' for int and generator. i tried converting it to a number with float() but obviously this doesn't work for the same reason, i feel there must be a very simple solution to my problem but i can't seem to find it. Thanks in advance.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload