473,788 Members | 2,867 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Generator functions and user interfaces

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? 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)?

I'm quite an experienced Python programmer but I've never taken the
trouble to get my head around generator functions, so any guidance
welcome!

Peter
Sep 10 '08 #1
2 1157
On Sep 10, 10:35*am, Bruno Desthuilliers
<bdesth.quelque ch...@free.quel quepart.frwrote :
psaff...@google mail.com 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:http://docs.python.org/whatsnew/pep-342.html

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
need.

Any generator guru around ?
Yield can return values. The syntax you're looking for is:

def generator_fun( ):
a= []
while 1:
b= yield( a )
a.append( b )

g= generator_fun( )
g.next( )
g.send( 3 )
g.send( 4 )
g.send( 5 )

/Output:
>>g.next( )
[]
>>g.send( 3 )
[3]
>>g.send( 4 )
[3, 4]
>>g.send( 5 )
[3, 4, 5]

'g' is inactive in between 'send' calls.
Sep 10 '08 #2


ps******@google mail.com wrote:
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?
To me, no. The reasons for using one do not apply here. What you
describe is much like a game with auto updates plus user intervention.
I would write an update function and add it to the gui mainloop (if
running: update()) along with the draw function to be tied into the gui
redraw (those parts I know little about). I would allow use of the
spacebar instead of or in addition to a button to toggle 'running' on
and off. That is standard in games that allow users to stop the action.

tjr

Sep 10 '08 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

18
8740
by: vrillusions | last post by:
I've been using functions since I first started using php, but I've been hearing more and more about classes, which I never really looked at that much. I understand the whole OO programming aspect, but are there any advantages to having classes as opposed to just functions? I've heard that the classes are better because they only load the functions they need to, where with an include file of functions, the whole page gets loaded, even if...
72
4416
by: Raymond Hettinger | last post by:
Peter Norvig's creative thinking triggered renewed interest in PEP 289. That led to a number of contributors helping to re-work the pep details into a form that has been well received on the python-dev list: http://www.python.org/peps/pep-0289.html In brief, the PEP proposes a list comprehension style syntax for creating fast, memory efficient generator expressions on the fly: sum(x*x for x in roots)
9
2690
by: Francis Avila | last post by:
A little annoyed one day that I couldn't use the statefulness of generators as "resumable functions", I came across Hettinger's PEP 288 (http://www.python.org/peps/pep-0288.html, still listed as open, even though it's at least a year old and Guido doesn't seem very hot on the idea). I'm not too sure of its ideas on raising exceptions in generators from outside (although it looks like it might be convenient in some cases), but being able...
0
2352
by: Anthony Baxter | last post by:
To go along with the 2.4a3 release, here's an updated version of the decorator PEP. It describes the state of decorators as they are in 2.4a3. PEP: 318 Title: Decorators for Functions and Methods Version: $Revision: 1.34 $ Last-Modified: $Date: 2004/09/03 09:32:50 $ Author: Kevin D. Smith, Jim Jewett, Skip Montanaro, Anthony Baxter
45
3048
by: Joh | last post by:
hello, i'm trying to understand how i could build following consecutive sets from a root one using generator : l = would like to produce : , , , ,
4
6656
by: Jonathan Burd | last post by:
Greetings everyone, Here is a random string generator I wrote for an application and I'm wondering about the thread-safety of this function. I was told using static and global variables cause potential problems for thread-safety. So far, I'm only confused. I need a proper explanation for the concept so I can understand how to write thread-safe functions in the future. My apologies for posting a long routine.
8
1763
by: Paperback Writer | last post by:
Hi Friends, I have a table with 30 fields and i'd like to know if there is some code generator that get those fields and transform to properties ??? -- Please, check my theSpoke: http://www.thespoke.net/MyBlog/dgroh/MyBlog.aspx
41
2538
by: Petr Jakes | last post by:
Hello, I am trying to study/understand OOP principles using Python. I have found following code http://tinyurl.com/a4zkn about FSM (finite state machine) on this list, which looks quite useful for my purposes. As this code was posted long time ago (November 1998) I would like to ask if the principles used in this code are still valid in the "modern" Python and if/how it can be improved (revrited) using futures of current version of...
3
1910
by: Dieter Maurer | last post by:
I met the following surprising behaviour .... for i in range(3): .... def gen1(): .... yield i .... yield i, gen1() .... .... 0 0 1 1
0
9656
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10366
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10173
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
9967
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8993
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6750
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
4070
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3674
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2894
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.