MVC in Python

Discussion in 'Python' started by MK, Aug 4, 2003.

  1. MK

    MK Guest

    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
     
    MK, Aug 4, 2003
    #1
    1. Advertising

  2. MK

    Troy Melhase Guest

    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
     
    Troy Melhase, Aug 4, 2003
    #2
    1. Advertising

  3. 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/
     
    Mike C. Fletcher, Aug 5, 2003
    #3
  4. MK

    Tertius Guest

    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
     
    Tertius, Aug 6, 2003
    #4
  5. 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 ...

    --
    Cousin Stanley
    Human Being
    Phoenix, Arizona
     
    Cousin Stanley, Aug 7, 2003
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    4
    Views:
    328
    Yermat
    Feb 12, 2004
  2. John Roth

    MVC-type Framework in Python

    John Roth, Mar 3, 2004, in forum: Python
    Replies:
    4
    Views:
    485
    F. GEIGER
    Mar 4, 2004
  3. rmn190
    Replies:
    2
    Views:
    2,424
    Arne Vajhøj
    Jan 10, 2008
  4. Paulo
    Replies:
    3
    Views:
    500
    Peter Bromberg [C# MVP]
    Dec 4, 2008
  5. Crazy Cat
    Replies:
    1
    Views:
    712
    schepp
    Sep 3, 2009
Loading...

Share This Page