Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > MVC in Python

Reply
Thread Tools

MVC in Python

 
 
MK
Guest
Posts: n/a
 
      08-04-2003
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


 
Reply With Quote
 
 
 
 
Troy Melhase
Guest
Posts: n/a
 
      08-04-2003
MK wrote:

> 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
 
Reply With Quote
 
 
 
 
Mike C. Fletcher
Guest
Posts: n/a
 
      08-05-2003
Troy Melhase wrote:

>MK wrote:
>
>
>>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.
>
>

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/




 
Reply With Quote
 
Tertius
Guest
Posts: n/a
 
      08-06-2003
MK wrote:
> 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


 
Reply With Quote
 
Cousin Stanley
Guest
Posts: n/a
 
      08-06-2003
I've also been looking for Python MVC examples
and ran across a simple example for WebWare ...

http://webware.colorstudy.com/twiki/...ViewController

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

--
Cousin Stanley
Human Being
Phoenix, Arizona


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
No MVC Project Template after installing ASP.NET MVC 1.0 Crazy Cat ASP .Net 1 09-03-2009 08:02 PM
WebForms X MVC? Why MVC? Give me reasons to migrate my web apps to it please. Pros x Cons! Thanks! Paulo ASP .Net 3 12-04-2008 03:00 AM
differences between Spring WebFlow,Spring MVC,and String Portlet MVC? rmn190 Java 2 01-10-2008 02:27 AM
MVC-type Framework in Python John Roth Python 4 03-04-2004 07:04 AM
does python support mvc architecture ketulp_baroda@yahoo.com Python 4 02-12-2004 07:05 PM



Advertisments