Generator functions and user interfaces

Bruno Desthuilliers bdesth.quelquechose at
Wed Sep 10 17:35:08 CEST 2008

psaffrey at a écrit :
> I'm trying to implement an interactive graph visualisation tool using
> matplotlib.
> I want to use a spring layout, where nodes repulse each other and
> edges act as springs to pull connected nodes together. Usually, this
> algorithm runs through a number of iterations of attraction/repulsion
> to allow the nodes to converge to suitable positions. However, rather
> than running all these iterations to lay out the graph and then
> rendering it, I want to animate the graph as it is "springing" into
> place, and then allow the user to drag nodes around and have the graph
> redraw on the fly.
> My idea for doing this was to use a generator function, where I yield
> the position of the nodes after each iteration and then call draw() on
> the position yielded. Does this seem like a sensible approach? 

I don't have much experience with this kind of algorithms, but AFAICT, 
it seems sensible to me, yes. But don't take me on words...

> The
> problem is that the node positions that are being operated on by the
> generator function may be altered by user input - dragging the nodes -
> and I'm not sure if this will break the way that the new positions are
> yielded. How do I use a generator function that might stop (when the
> nodes stop moving) but then need to restart again (once the user moves
> the nodes)?

Starting with Python 2.5, there's a way to pass values back to generators:

Now, not having played much with these new features so far, I'm afraid I 
can't help more, nor even be strictly positive about this being what you 

Any generator guru around ?

More information about the Python-list mailing list