event handler advice

Roger Upole rupole at compaq.net
Tue Jul 18 17:02:13 EDT 2000


No HAHA, actually you can do exactly that.  Just construct your dictionary
so
that it maps the event integer value to the function itself.
def eventhandler1()
    ....
def eventhandler2()
    ....
def eventhandler3()
    ....
eventmap = {eventvalue1:eventhandler1, eventvalue2:eventhandler2,
eventvalue3:eventhandler3 }
eventhandler  = eventmap[someeventvalue]
eventhandler()

   HTH

       Roger Upole

Then you can
"Pete Shinners" <pete at visionart.com> wrote in message
news:8l033n$pr2$1 at la-mail4.digilink.net...
> i'm trying to write a simple event handler python class.
> so far i haven't come up with an 'elegant' solution i was
> expecting.
>
> i have a list of different messages, and they are all
> given different interger values. this list is defined
> in a C extension, for example
> msg.INIT, msg.EXIT, msg.STOP, msg.PLAY, ...
>
>
> i want to make a inheritable class that receives one
> of these messages and calls a member routine with the
> same name. my handler doesn't know the name of the
> variable, since python only sends it the INT value.
>
> so i need to find some nice way to map these message ids
> to a method name. i then need to get this name and have
> the class call it on itself.
>
> the end result being i can call
> "mymessagehandler_inst.handle(msg.PLAY)"
> and this will in turn call
> "mymessagehandler_inst.PLAY()"
>
> there's a good sized list of these messages, and in the
> future it will likely expand
>
> my guess is i need some dictionary that defines the
> messages and their routine name (no shortcut way to do
> this unless python had #define like macros :])
>
> messages = {msg.INIT:"INIT", msg.PLAY:"PLAY", ...etc}
>
> then a routine like
>
> class basehandler:
>     def handle(messageid):
>         name = messages[messageid]
>         self.name()   #HAHA, no really, how do i do this?
>
> but i'm not exactly sure how to write this. need help, thx
>
>
>







More information about the Python-list mailing list