"Pushing" Data to Modules

classic Classic list List threaded Threaded
24 messages Options
12
Reply | Threaded
Open this post in threaded view
|

"Pushing" Data to Modules

Rich Shepard
   I now know how to use a generic 'config.py' module to make variables
visible to all the modules in my application. Each module can "pull" the
values it needs from this universally-visible module, and that's working
well.

   However, is there a way of "pushing" data to the various modules?
Specifically, each module is a wx.Notebook() page with widgets for data
belonging to different parts of the overall model. Within each notebook page
I successfully enter new or changed values of the widgets and store those in
the model database when the "Save" button is pressed. My difficulties are
with loading each of those widgets with data retrieved from an existing
model database when it's opened.

   One module, the one with general descriptive information on the model, has
the "Open" button and bound method. It works as far as displaying retrieved
data on its own notebook page, but I have not figured out how to force the
data onto the other pages since they don't have the equivalent of the "Open"
button's method.

   Each module sets up the appropriate widgets, but does not have a bound
method to fill the display. I could create such a method and use it to call
the data retrieved by the dbMethods.py module, but to what is it bound? Is
there a way to propagate the OnOpenDB() call to all modules?

Puzzled,

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: "Pushing" Data to Modules

Josiah Carlson

Rich Shepard <[hidden email]> wrote:
>    I now know how to use a generic 'config.py' module to make variables
> visible to all the modules in my application. Each module can "pull" the
> values it needs from this universally-visible module, and that's working
> well.
>
>    However, is there a way of "pushing" data to the various modules?
[snip]

wx.lib.pubsub

 - Josiah



Reply | Threaded
Open this post in threaded view
|

Re: "Pushing" Data to Modules

Rich Shepard
On Fri, 1 Dec 2006, Josiah Carlson wrote:

> wx.lib.pubsub

   Thank you.

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: "Pushing" Data to Modules

Rich Shepard
In reply to this post by Josiah Carlson
On Fri, 1 Dec 2006, Josiah Carlson wrote:

> wx.lib.pubsub

   Re-reading the MVC section in the wPIA book, I see that wx.lib.pubsub is
the appropriate class for me. The API docs are rather sparse, and I did not
find more detailed descriptions in my Google search.

   The event I want to "publish" now from the module with the database
methods is that a database connection has been established to an existing
file. The subscribers are in different modules, each of which holds the
widgets and bound methods for a notebook page.

   Does anyone have -- or have a reference to -- an application that uses
this library in multiple modules/classes? It would make my learning and
understanding much easier if I could look at a working example more complex
than the simple one (for the AbstractModel() class) in the book.

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: "Pushing" Data to Modules

Donn Ingle
Rich, I have also recently being trying to grok the whole WVC/WVP thing and
I ended-up making my own "thing" because I couldn't really find good
examples and info relating to the pubsub stuff.

Attached, is my shout() ----> Ear ()  code, FWIW. I included instructions on
use in the file.

Perhaps my (inevitable) mistakes and misconceptions will help you (and me by
flame :) ) learn about pusub.

/d



remotecontrol.py (4K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Rich Shepard
On Sat, 2 Dec 2006, Donn Ingle wrote:

> Rich, I have also recently being trying to grok the whole WVC/WVP thing
> and I ended-up making my own "thing" because I couldn't really find good
> examples and info relating to the pubsub stuff.

Donn,

   I have no problems with the concept(s); I've used that model in C for
years, even though it didn't have a name. I'd always have the UI code in one
.c file, the middleware in another .c file, and the database back end (if
one was needed) in yet another file. To me, that was intuitive and the only
way to go. However, python and the object-oriented model seems to make the
whole process much more complex, at least for those of us who didn't start
coding with it.

> Attached, is my shout() ----> Ear ()  code, FWIW. I included instructions
> on use in the file.

   Thank you.

> Perhaps my (inevitable) mistakes and misconceptions will help you (and me
> by flame :) ) learn about pusub.

   Well, I'm not about to flame anyone over anything.

   I am having difficulties reconciling your names (shout and ear) with the
publish and subscribe idea. It's strictly a terminology mis-match, and I
know that you can clear it up for me.

   In wx.lib.pubsub, a module/class that creates an event (for example, in my
application, having the DBtools() class open the embedded sqlite3 database
file) would "publish" the news to the entire application. Other
modules/classes could "subscribe" to read messages of interest to it (with
the same example, loading database values in the widgets of each notebook
page).

   When I started reading your remotecontrol.py, I thought that shout() was
equivalent to publish(), and that ear() was equivalent to subscribe(). As I
read further, it seemed that I had it backwards. Now, I'm not at all sure
what's happening. Nor, why the messages are numbered rather than using
strings; e.g., "file open."

   Seems to me that the biggest problem with our use of pubsub is the lack of
an actual implementation to examine. Reading the API docs and trying to
learn just how to implement the wx.lib.pubsub() and Publisher() classes is
like reading a man page to learn a tool. Man pages and API docs are great
syntax reminders when you know what you're doing, and why you're doing it,
but they don't teach the context.

   In the vast universe of working wxPython apps, there must be at least one
that uses these classes so we can learn how to use them for ourselves. No?

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Donn Ingle
Rich Shepard wrote:
>    I have no problems with the concept(s); I've used that model in C for
> years, even though it didn't have a name.

Oops. Sorry about that. I am totally new to it - I'm a spaghetti coder!

> I read further, it seemed that I had it backwards. Now, I'm not at all
> sure what's happening. Nor, why the messages are numbered rather than
> using strings; e.g., "file open."

Well, I reckon you're way beyond my little attempt. At the end, I include
some samples given to me (on this list) recently. Perhaps those will help
you more.

I used "ear" to indicate that it was going to listen and "shout" to indicate
that one wants something heard. To my mind they make sense :) So, yeah you
got them backwards.

I used descriptive variable names rather than strings because when I get
python errors, it shows the variables involved and I can see which 'topic'
is involved in the car smash. With strings, you don't get to see
what 'topic' was involved. Well, that's my story!
 
>    In the vast universe of working wxPython apps, there must be at least
>    one
> that uses these classes so we can learn how to use them for ourselves. No?

This is what I have been pointed to:
1.
http://lists.wxwidgets.org/cgi-bin/ezmlm-cgi?11:mss:48197:200602:okmajbebilmpeafpdlco
2. See attached samples by Werner F. Bruhin
He said "The post Dan pointed out looks very good.
There is also quit a bit of test code in the
pubsub.py module.  For what it is worth I attach the
very crude test stuff (run pubsubApp, press the
button and close it, output goes to standard out)
I put together for myself to try to understand it."
3. There are two good articels on the wiki, neither
mention pubsub, but they may help you. I found them incomplete
as far as concept goes.
http://wiki.wxpython.org/index.cgi/ModelViewController 
http://wiki.wxpython.org/index.cgi/ModelViewPresenter


Good luck,
Donn.

mypublisher.py (1K) Download Attachment
pubsubApp.py (656 bytes) Download Attachment
pubsubtest.py (1K) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Werner F. Bruhin
In reply to this post by Rich Shepard
Hi Rich and Donn,

Not that I am expert on this but here is a revised version of what I
used to get a hang of pubsub.

- mypublisher.py - setup constants and instantiate the pubsub stuff
- pubsubApp.py - Boa app module
- pubsubtest.py - a wx.Frame with a statusbar and a button, subscribes
to statustext messages, when button is pressed it does some publishing
stuff, and calls up a dialog
- dialog1.py - simple dialog with a button, when pressed sends a message
to statusbar

Maybe something along these lines (with better doc strings then I do)
should be on the wiki to help people going with pubsub.

Hope this helps
Werner

pubsubtest.py (2K) Download Attachment
Dialog1.py (1K) Download Attachment
mypublisher.py (1K) Download Attachment
pubsubApp.py (706 bytes) Download Attachment
Reply | Threaded
Open this post in threaded view
|

Re: Re: Re: "Pushing" Data to Modules

Rich Shepard
In reply to this post by Donn Ingle
On Sat, 2 Dec 2006, Donn Ingle wrote:

> Oops. Sorry about that. I am totally new to it - I'm a spaghetti coder!

   Ah, I gave that up when I moved from FORTRAN to C in the mid-1980s.

> Well, I reckon you're way beyond my little attempt. At the end, I include
> some samples given to me (on this list) recently. Perhaps those will help
> you more.

   Thanks, Donn.

> I used "ear" to indicate that it was going to listen and "shout" to indicate
> that one wants something heard. To my mind they make sense :)

   Makes sense to me, too. What confused me is which module was shouting and
which was trying to listen. :-)

> This is what I have been pointed to:
> 1.
> http://lists.wxwidgets.org/cgi-bin/ezmlm-cgi?11:mss:48197:200602:okmajbebilmpeafpdlco

> 2. See attached samples by Werner F. Bruhin He said "The post Dan pointed
> out looks very good. There is also quit a bit of test code in the
> pubsub.py module.  For what it is worth I attach the very crude test stuff
> (run pubsubApp, press the button and close it, output goes to standard
> out) I put together for myself to try to understand it."

> 3. There are two good articels on the wiki, neither mention pubsub, but
> they may help you. I found them incomplete as far as concept goes.
> http://wiki.wxpython.org/index.cgi/ModelViewController
> http://wiki.wxpython.org/index.cgi/ModelViewPresenter

Thanks very much,

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Rich Shepard
In reply to this post by Werner F. Bruhin
On Sat, 2 Dec 2006, Werner F. Bruhin wrote:

> Not that I am expert on this but here is a revised version of what I used to
> get a hang of pubsub.
>
> - mypublisher.py - setup constants and instantiate the pubsub stuff
> - pubsubApp.py - Boa app module
> - pubsubtest.py - a wx.Frame with a statusbar and a button, subscribes to
> statustext messages, when button is pressed it does some publishing stuff,
> and calls up a dialog
> - dialog1.py - simple dialog with a button, when pressed sends a message to
> statusbar

   Thanks very much, Werner.

> Maybe something along these lines (with better doc strings then I do)
> should be on the wiki to help people going with pubsub.

   There's obviously interest and need in all this. Perhaps cooperatively we
can both gain better understanding and document it in the wiki. This topic
looks like one of those hidden gems that turn out to be useful in a broad
range of applications ... perhaps most.

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Donn Ingle
>    There's obviously interest and need in all this. Perhaps cooperatively
>    we
> can both gain better understanding and document it in the wiki. This topic
> looks like one of those hidden gems that turn out to be useful in a broad
> range of applications ... perhaps most.

I'll certainly keep an eye on the wiki and contribute where I can ( I like
to make diagrams to simplify a lot of words! ), but I don't have clue #1
about this whole topic, so I can't start anything.

Please, if you are in the zone, hit the wiki, but Keep It Simple Stupid!!

:D

/d



Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Rich Shepard
In reply to this post by Werner F. Bruhin
On Sat, 2 Dec 2006, Werner F. Bruhin wrote:

> Not that I am expert on this but here is a revised version of what I used to
> get a hang of pubsub.

Werner,

   Looking at what you sent, and the wx.lib.pubsub API doc page is getting me
closer, and with less code than you used. The error I'm getting now is:

   File
"/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/lib/pubsub.py",
line 670, in validate
     raise TypeError, 'Listener '+`listener+' lacking arguments!'
TypeError: Listener <bound method modPolicy.setWidgets of
<policyPage.modPolicy; proxy of C++ wxPanel instance at
_18544408_p_wxPanel>> lacking arguments!

so it appears that by 'importing wx.lib.pubsub as Publisher' I've exposed
what's neede. Now it's a matter of structuring the arguments properly.
Unfortunately, the API doc page uses ellipses in the parentheses rather than
clue sticks for beginning users like me.

   In config.py (the application-visible variables) I have added:
  projOpen = 1 # publisher message that a project file's opened

   Next, in DBmethods.DBtools().openDB() I have:
  self.pubData = self.appData.projName
  self.Publisher().sendMessage(projOpen, self.pubData)
which are probably mal-formed. I'm not sure what to put in the arguments to
self.Publisher().sendMessage().

   Finally, in modPolicy().__init__() I have the line generating the error:

  self.polSub = Publisher().subscribe(self.setWidgets, self.appData.projOpen)

   I tried to follow the model in your example of having the
Publisher().subscribe parameters of the function to call and the message
that triggers calling that function. Obviously, I missed.

   The trick to using wx.lib.pubsub seems to be understanding what to put in
the parentheses of the subscribe and sendMessage methods.

Thanks,

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Werner F. Bruhin
Hi Rich,

Rich Shepard wrote:

> On Sat, 2 Dec 2006, Werner F. Bruhin wrote:
>
>> Not that I am expert on this but here is a revised version of what I
>> used to get a hang of pubsub.
>
> Werner,
>
>   Looking at what you sent, and the wx.lib.pubsub API doc page is
> getting me
> closer, and with less code than you used. The error I'm getting now is:
>
>   File
> "/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/lib/pubsub.py",
> line 670, in validate
>     raise TypeError, 'Listener '+`listener+' lacking arguments!'
> TypeError: Listener <bound method modPolicy.setWidgets of
> <policyPage.modPolicy; proxy of C++ wxPanel instance at
> _18544408_p_wxPanel>> lacking arguments!
What is the "def" for policyPage.modPolicy?  See below for the
NeedChoiceRefresh stuff.
>
> so it appears that by 'importing wx.lib.pubsub as Publisher' I've exposed
> what's neede. Now it's a matter of structuring the arguments properly.
Yes, I think the module mypublisher.py is only needed if one wants to
use variables instead of strings.
> Unfortunately, the API doc page uses ellipses in the parentheses
> rather than
> clue sticks for beginning users like me.
Any Python object is accepted, i.e. ('something', 'whatever') a tuple or
[] a list or {} a dict or whatever other object.

>
>   In config.py (the application-visible variables) I have added:
>     projOpen = 1 # publisher message that a project file's opened
>
>   Next, in DBmethods.DBtools().openDB() I have:
>     self.pubData = self.appData.projName
>     self.Publisher().sendMessage(projOpen, self.pubData)
> which are probably mal-formed. I'm not sure what to put in the
> arguments to
> self.Publisher().sendMessage().
>
>   Finally, in modPolicy().__init__() I have the line generating the
> error:
>
>     self.polSub = Publisher().subscribe(self.setWidgets,
> self.appData.projOpen)
>
>   I tried to follow the model in your example of having the
> Publisher().subscribe parameters of the function to call and the message
> that triggers calling that function. Obviously, I missed.
Does your code look something like:
    def setWidgets(self, msg):
       msg.topic
       msg.data
>
>
>   The trick to using wx.lib.pubsub seems to be understanding what to
> put in
> the parentheses of the subscribe and sendMessage methods.
The way I understand it (wxPython 2.6.3, don't know if it changed in 2.7).

    # to subscribe
    mypub.publisher.subscribe(functionToCall, mypub.statusText)

Above could also be:
    # to subscribe
    mypub.publisher.subscribe(functionToCall, 'statusText')

The "functionToCall" is what ever method/function you want to call and
it accepts the "data", e.g.:

If you look a mypublisher.py it has:
def MyPrint(msg):
    print '****************'
    print 'for testing only'
    print msg.topic
    print msg.data
    print '****************'

Above "MyPrint" is the functionToCall and "msg" contains the data and it
has an attribute "topic" (e.g. 'statusText' and an attribute "data"
(e.g. the string to be shown in the statustext) which can be any valid
Python object (you define what it is and it can be different for each
topic).

See you also MySetStatusText in pubsubtest.py:
    def MySetStatusText(self, msg):
        self.statusBar1.SetStatusText(msg.data, 0)

Now the notify stuff would be:
    # to notify
    mypub.publisher.sendMessage(mypub.statusText, 'Text for status line')

Or:
    # to notify
    mypub.publisher.sendMessage('statusText', 'Text for status line')

Something more complex could be the stuff I use in my application for
comboboxes, e.g. in the dialog which maintains the entries for a
combobox I do something along the following.

E.g. in the dialog which  maintains wine regions I do this when closing
the dialog.

        mypub.publisher.sendMessage(mypub.comboBoxRefresh, 'region_ls')

In my generic combobox module I subscribe to both the refresh for a
particular table (first one below) and a generic refresh (second one
below) of all comboboxes.

            mypub.publisher.subscribe(self.NeedChoiceRefresh,
(mypub.comboBoxRefresh, self.dbTable))
            mypub.publisher.subscribe(self.NeedChoiceRefresh,
mypub.comboBoxRefresh)

Note that the first line above is the more specific one, i.e. if this is
the instance for a "region_ls" type combobox the function
"NeedChoiceRefresh" is called, or it is called if the publisher does not
specify a table name, i.e.

        mypub.publisher.sendMessage(mypub.comboBoxRefresh)

Here is what NeedChoiceRefresh looks like:

def NeedChoiceRefresh(self, msg):
        self.initDone = False
        try:
            self.LoadChoices()
        except wx.PyDeadObjectError:
            pass

"initDone" is needed by LoadChoices - don't worry about it, the except
was needed in case the combobox is being destroyed.

Hope this helps and does not cause more confusion.

Werner



Reply | Threaded
Open this post in threaded view
|

Re: "Pushing" Data to Modules

Robin Dunn
In reply to this post by Rich Shepard
Rich Shepard wrote:

> On Fri, 1 Dec 2006, Josiah Carlson wrote:
>
>> wx.lib.pubsub
>
>   Re-reading the MVC section in the wPIA book, I see that wx.lib.pubsub is
> the appropriate class for me. The API docs are rather sparse, and I did not
> find more detailed descriptions in my Google search.
>
>   The event I want to "publish" now from the module with the database
> methods is that a database connection has been established to an existing
> file. The subscribers are in different modules, each of which holds the
> widgets and bound methods for a notebook page.
>
>   Does anyone have -- or have a reference to -- an application that uses
> this library in multiple modules/classes? It would make my learning and
> understanding much easier if I could look at a working example more complex
> than the simple one (for the AbstractModel() class) in the book.

With pubsub it works exactly the same way in a multi-module app as it
does with a single module app.  You simply have one part of the
application that subscribes to receive messages of certain topics, and
another part of the application that publishes messages with those
topics.  It doesn't matter if they are in the same module or even the
same class, or not.


--
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!



Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Robin Dunn
In reply to this post by Rich Shepard
Rich Shepard wrote:

> Seems to me that the biggest problem with our use of pubsub is the
> lack of an actual implementation to examine. Reading the API docs and
>  trying to learn just how to implement the wx.lib.pubsub() and
> Publisher() classes is like reading a man page to learn a tool. Man
> pages and API docs are great syntax reminders when you know what
> you're doing, and why you're doing it, but they don't teach the
> context.

There is some test code in the pubsub module itself.  See the test()
function at the end.


> The trick to using wx.lib.pubsub seems to be understanding what to
> put in the parentheses of the subscribe and sendMessage methods.

http://wxpython.org/docs/api/wx.lib.pubsub.PublisherClass-class.html

Basically, it's

        Publisher().subscribe(callable, topic)

and

        Publisher().sendMessage(topic, data=None)

Where topic is a string or a tuple of strings that defines what the type
of the message is.  The topic can be hierarchical with a topic and
sub-topics, allowing you to have listeners that can respond generically
to a collection of topics, or listeners that can only respond to
specific topics.

--
Robin Dunn
Software Craftsman
http://wxPython.org  Java give you jitters?  Relax with wxPython!



Reply | Threaded
Open this post in threaded view
|

Re: "Pushing" Data to Modules

Rich Shepard
In reply to this post by Robin Dunn
On Sun, 3 Dec 2006, Robin Dunn wrote:

> With pubsub it works exactly the same way in a multi-module app as it does
> with a single module app.  You simply have one part of the application
> that subscribes to receive messages of certain topics, and another part of
> the application that publishes messages with those topics.  It doesn't
> matter if they are in the same module or even the same class, or not.

   Thank you, Robin. With further reading I realized this and am now working
on learning how to correctly structure the arguments to .subscribe() and
.sendMessage().

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Rich Shepard
In reply to this post by Robin Dunn
On Sun, 3 Dec 2006, Robin Dunn wrote:

> Basically, it's
> Publisher().subscribe(callable, topic)
> and
> Publisher().sendMessage(topic, data=None)

   Well, darn! That's what I thought I have.

   In the OpenDB() function I have

  Publisher().sendMessage(self.appData.projOpen, data=None)

and in the class modPolicy()'s __init__() method I have

  Publisher().subscribe(self.setWidgets, self.appData.projOpen)

with setWidgets() a function that places values in the treeCtrl.

   But the error is still that the subscribe method has no arguments:

   File "/data1/eikos/policyPage.py", line 124, in __init__
     Publisher().subscribe(self.setWidgets, self.appData.projOpen)
   File
"/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/lib/pubsub.py",
line 638, in subscribe
     self.validate(listener)
   File
"/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/lib/pubsub.py",
line 670, in validate
     raise TypeError, 'Listener '+ istener+' lacking arguments!'
TypeError: Listener <bound method modPolicy.setWidgets of
<policyPage.modPolicy; proxy of C++ wxPanel instance at
_00534408_p_wxPanel>> lacking arguments!

   Should the Publisher().subscribe() be located somewhere other than in the
init method?

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules

Werner F. Bruhin
Hi Rich,

Rich Shepard wrote:

> On Sun, 3 Dec 2006, Robin Dunn wrote:
>
>> Basically, it's
>>     Publisher().subscribe(callable, topic)
>> and
>>     Publisher().sendMessage(topic, data=None)
>
>   Well, darn! That's what I thought I have.
>
>   In the OpenDB() function I have
>
>     Publisher().sendMessage(self.appData.projOpen, data=None)
>
> and in the class modPolicy()'s __init__() method I have
>
>     Publisher().subscribe(self.setWidgets, self.appData.projOpen)
>
> with setWidgets() a function that places values in the treeCtrl.
>
>   But the error is still that the subscribe method has no arguments:
>
>   File "/data1/eikos/policyPage.py", line 124, in __init__
>     Publisher().subscribe(self.setWidgets, self.appData.projOpen)
>   File
> "/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/lib/pubsub.py",
> line 638, in subscribe
>     self.validate(listener)
>   File
> "/usr/lib/python2.4/site-packages/wx-2.6-gtk2-unicode/wx/lib/pubsub.py",
> line 670, in validate
>     raise TypeError, 'Listener '+ istener+' lacking arguments!'
> TypeError: Listener <bound method modPolicy.setWidgets of
> <policyPage.modPolicy; proxy of C++ wxPanel instance at
> _00534408_p_wxPanel>> lacking arguments!
>
>   Should the Publisher().subscribe() be located somewhere other than
> in the
> init method?

I think the problem is with your "self.setWidgets" definition.

In the sample code if I change:

def MyPrint(msg):

to
def MyPrint():

I get the following exception:
Traceback (most recent call last):
  File "pubsubApp.py", line 6, in ?
    import pubsubtest
  File "C:\Dev\BoaTest04\pubsub\pubsubtest.py", line 4, in ?
    import mypublisher as mypub
  File "C:\Dev\BoaTest04\pubsub\mypublisher.py", line 36, in ?
    InitMyPublisher()
  File "C:\Dev\BoaTest04\pubsub\mypublisher.py", line 27, in InitMyPublisher
    publisher.subscribe(MyPrint, '')
  File "C:\Python24\lib\site-packages\wx-2.6-msw-ansi\wx\lib\pubsub.py",
line 677, in subscribe
    self.validate(listener)
  File "C:\Python24\lib\site-packages\wx-2.6-msw-ansi\wx\lib\pubsub.py",
line 709, in validate
    raise TypeError, 'Listener '+`listener`+' lacking arguments!'
TypeError: Listener <function MyPrint at 0x009ED1B0> lacking arguments!

Werner
>
> Rich
>




Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules -- SOLVED!

Rich Shepard
On Mon, 4 Dec 2006, Werner F. Bruhin wrote:

> Hi Rich,

Good afternoon (your time), Werner:

> In the sample code if I change:
>
> def MyPrint(msg):
> to
> def MyPrint():

   This threw me for a moment, because I had
  def setWidgets(msg):
But, and this is embarrassing while being a solid lesson, what I need is
  def setWidgets(self, msg):

   And now the program loads. I expect that when I put content in this method
it will work as desired.

   Thanks to Donn, Robin, and you what was so daunting when I first looked at
the wx.lib.pubsub API page turns out to be no more complex than using any
other wx. class. The Wiki page seems to be a bit more obtuse than it needs
to be, too.

   I can summarize what I did in three steps:

   1) In the application-global config.py's configData() class, I entered the
one message I currently need ('projOpen = 1'); more messages can be entered
as necessary in other parts of the model.

   2) In the module/class that needs to tell the rest of the application that
a project database file has been opened (i.e., a connection and cursor
established), I import the class
  from wx.lib.pubsub import Publisher
and have this line:
  Publisher().sendMessage('projOpen', data=None)

   3) In each of the modules/classes that need to set retrieved data into the
appropriate widgets for display, the class is also imported
  from wx.lib.pubsub import Publisher
and I have this command at the end of the __init__ method:
  Publisher().subscribe(self.setWidgets, 'projOpen')

   The called method is then defined by:
  def setWidgets(self, msg):

   All of this is much simpler than it seems. Of course, this is not an
implimentation of the MVC/MVP, but a solution to the problem of inter-module
communication (IMC?).

Rich

--
Richard B. Shepard, Ph.D.               |    The Environmental Permitting
Applied Ecosystem Services, Inc.(TM)    |            Accelerator
<http://www.appl-ecosys.com>     Voice: 503-667-4517      Fax: 503-667-8863


Reply | Threaded
Open this post in threaded view
|

Re: Re: "Pushing" Data to Modules -- SOLVED!

Werner F. Bruhin
Hi Rich,

Rich Shepard wrote:
> On Mon, 4 Dec 2006, Werner F. Bruhin wrote:
>
>> Hi Rich,
>
> Good afternoon (your time), Werner:
Good morning to you then :-) .

>
>> In the sample code if I change:
>>
>> def MyPrint(msg):
>> to
>> def MyPrint():
>
>   This threw me for a moment, because I had
>     def setWidgets(msg):
> But, and this is embarrassing while being a solid lesson, what I need is
>     def setWidgets(self, msg):
>
>   And now the program loads. I expect that when I put content in this
> method
> it will work as desired.
>
>   Thanks to Donn, Robin, and you what was so daunting when I first
> looked at
> the wx.lib.pubsub API page turns out to be no more complex than using any
> other wx. class. The Wiki page seems to be a bit more obtuse than it
> needs
> to be, too.
>
>   I can summarize what I did in three steps:
>
>   1) In the application-global config.py's configData() class, I
> entered the
> one message I currently need ('projOpen = 1'); more messages can be
> entered
> as necessary in other parts of the model.
>
>   2) In the module/class that needs to tell the rest of the
> application that
> a project database file has been opened (i.e., a connection and cursor
> established), I import the class
>     from wx.lib.pubsub import Publisher
> and have this line:
>     Publisher().sendMessage('projOpen', data=None)
I think this should be:
    Publisher().sendMessage(configData.projOpen, data=None)

Or use strings everywhere and forget to predefine them in your configdata.
>
>   3) In each of the modules/classes that need to set retrieved data
> into the
> appropriate widgets for display, the class is also imported
>     from wx.lib.pubsub import Publisher
> and I have this command at the end of the __init__ method:
>     Publisher().subscribe(self.setWidgets, 'projOpen')
Again this would be:
    Publisher().subscribe(self.setWidgets, configData.projOpen)

>
>   The called method is then defined by:
>     def setWidgets(self, msg):
>
>   All of this is much simpler than it seems. Of course, this is not an
> implimentation of the MVC/MVP, but a solution to the problem of
> inter-module
> communication (IMC?).
>
> Rich
>
See you
Werner




12