MVC in Python

M

MK

In Java, I simply register many listeners with a data model.
Whenever the model changes, all the listeners are notified
and they change their GUI elements accordingly and
automatically.

How to achieve this feat in Python? Which techniques
do you guys use?

Say you have one data structure and two tree controls
in wxPython, displaying two perspectives of that same
data structure. How would you proceed, that is, implement
an equivalent, MVC-ish mechanism, in this case?
My first knee-jerk reaction would be: implement two functions,
update_tree_1() and update_tree_2() which "intelligently"
update only the critical parts of tree GUI control and
which are being called by the data structure whenever
it (data structure, that is) changes.

MK,
coming from a patterns-ridden Java world
 
T

Troy Melhase

MK said:
How to achieve this feat in Python? Which techniques
do you guys use?

Hi MK:

If memory serves (and it rarely does), you have two options. The first
would be custom wx events, and the second would be the evtmgr module in the
wxPython.lib package. Both approaches have examples in the wx demo
application. The evtmgr demo is absolutely dazzling in its simplicity, and
I would suggest you start your search there.

Good luck,
troy
 
M

Mike C. Fletcher

Troy said:
MK wrote:



Hi MK:

If memory serves (and it rarely does), you have two options. The first
would be custom wx events, and the second would be the evtmgr module in the
wxPython.lib package. Both approaches have examples in the wx demo
application. The evtmgr demo is absolutely dazzling in its simplicity, and
I would suggest you start your search there.
Another option: a number of people also use the
multi-consumer-multi-producer "dispatcher" engine. There's a sourceforge
project for the latest version here:

http://sourceforge.net/projects/pydispatcher/

I've used it in both OpenGLContext and ConflictSolver/wxPython
Properties Distribution (and helped a little with putting together the
above project).

* It's *not* GUI-lib specific, but it works nicely with GUI
libraries (and was, IIRC originally created for use with a GUI
library), so your model objects don't have dependencies on the GUI
code at all, but can readily generate events to which the GUI can
subscribe.
* There's no requirement that your objects be aware of the
event-routing infrastructure.
* Registering interest doesn't (generally) affect object lifetimes
(that is, dispatcher uses weak references by default (and has
special code to allow safe weak references to methods of objects),
so that when objects go away their associated routing tables are
also cleaned up).

The basic pattern is to register interest in certain classes of messages
(which are just any hashable value, and may be a special "Any" message
to recieve all messages) from a specific sender, any sender, or a
special "anonymous" sender. That interest is registered by passing in a
callable object (a "receiver"). You then send messages, specifying the
sender (which can be "Anonymous") and the message. The system finds all
receivers which have registered interest that matches the sender +
message combination, figures out what part of the arguments to the
message are applicable to the particular receiver, and collects the
results of delivering the message to each receiver.

All-in-all, it's a good system for normal application event routing, but
to be clear, it's not:

* an IPC mechanism... this is an in-process mechanism (though IIRC
someone has done some experimenting with doing RPC calls with it)
* asynchronous (message delivery occurs at the moment you call "send")
* protected against event looping (you have to do that in your own
code), i.e. it won't detect that method q is sending an event to
itself
* guaranteed (this isn't an enterprise application routing
infrastructure with guaranteed message delivery, input and output
queues, logging and the like, it's a lightweight framework for
desktop application development)
* highly scalable (you don't want to load 100s of thousands of
highly dynamic (i.e. disappearing and reappearing rapidly, and/or
constantly changing their routing parameters) nodes, (though the
version above does include significant improvements beyond the
original Python Cookbook version in this regard)).

That said, it's right up the MVC for desktop applications alley, so I'd
suggest taking a look.

Enjoy yourself,
Mike

_______________________________________

Mike C. Fletcher
Designer, VR Plumber, Coder
http://members.rogers.com/mcfletch/
 
T

Tertius

MK said:
In Java, I simply register many listeners with a data model.
Whenever the model changes, all the listeners are notified
and they change their GUI elements accordingly and
automatically.

How to achieve this feat in Python? Which techniques
do you guys use?

Say you have one data structure and two tree controls
in wxPython, displaying two perspectives of that same
data structure. How would you proceed, that is, implement
an equivalent, MVC-ish mechanism, in this case?
My first knee-jerk reaction would be: implement two functions,
update_tree_1() and update_tree_2() which "intelligently"
update only the critical parts of tree GUI control and
which are being called by the data structure whenever
it (data structure, that is) changes.

MK,
coming from a patterns-ridden Java world

Check out ..\wxPython\lib\pubsub.py
 
C

Cousin Stanley

I've also been looking for Python MVC examples
and ran across a simple example for WebWare ...

http://webware.colorstudy.com/twiki/bin/view/Webware/ModelViewController

The model and controller are plain-vanilla Python
and the view module contains the WebWare interface ...

Since I have NO clue about WebWare,
I haven't tried to convert it to anything else ...

Perhaps someone that is more familar with WebWare programming
could provide a conversion to something like Tkinter ...
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top