Now, displaying a grid is quite simple... The problem is: the tool must be independent from the business logic, that is my evolutionary computations. So, I was needing something that I could just "put up and working", and feeding with my own data.
Something like this must exists for sure... Probably GNUPlot will do.
Anyway I implemented (being in a pro-python period) this tool by myself. I used PySide (which I discovered at the pycon and I'll use instead of PyQt) and threads to obtain something that I could use easily to display grid data.
So, this python module, called QtVisual, works like this: a class (Window, what an original name...) is in the main thread and display the data.
Another class (DataProvider) contains the methods to handle the concurrency: this class is a QThread which will be executed to compute user's data. The user shall subclass DataProvider and reimplement the feed() method. When data is ready to be displayed, the subclass will call DataProvider's methods, that will send a Qt SIGNAL to the Window class, then the data will be copied into the main thread and displayed.
The usage of this module is pretty simple: just subclass DataProvider, implement the feed method and use the data_ready() function to set the data to be copied. An optional delay can be set to display data as animation.
Data display can be customized: the Window will use an user-provided generator function to get data from the grid (the default generator scan the whole grid as a NxM matrix). In addition, a color function can be specified to specify the fill color of the cells based on the data.
This provides some flexibility in the data representation... For example, I have create a little example that access and display data in a random way: the generator function will display 10 randomly-chosen cells.
I didn't tried it yet, but I think that this method allow to display other kind of data, for example graphs or trees... This is because using the generator, the grid-abstraction is completely hidden to the displayer. Actually, the displayer uses a index coordinate pair (x,y) to decide where to place the dot, but the generator will provide this pair.
The (undocumented and subject to changes) code, is already available at the BlueCode Lab's repository.
There are some issues, of course:
- I'm not a multi-thread programming expert. It may be possible that this code ISN'T concurrent at all :D
- The data are *deeply copied* between the threads. This means that the data_ready() may be slow if the amount of data is big... Or it may not be slow if there is some kind of concurrency control underneath or some kind of copy-on-write. But I'm not a python expert, thus I don't know about these optimizations, if any, and I'm assuming that *this is slow*.
- Undocumented.
- There are bugs and bad programming practices for sure... I don't know where, of course, but if you find any, please tell me.
Hoping it's useful for you, too!
Stay --sync
P.S. Oh, by the way, the screen capture has been done with byzanz-record and cropped with gimp.

0 commenti:
Post a Comment