event handler advice

Pete Shinners pete at visionart.com
Mon Jul 17 18:49:02 EDT 2000


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