473,326 Members | 2,813 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,326 software developers and data experts.

Events in Python?

Here is another non-pythonic question from the Java Developer. (I beg
for forgiveness...)

Does Python have a mechanism for events/event-driven programming?

I'm not necessarily talking about just GUIs either, I'm interested in
using events for other parts of an application as well.

If there isn't some sort of event mechanism built into Python, where
might I find some information about implementing one?

Thanks,

Scott Huey

Apr 26 '06 #1
9 2639
re****************@gmail.com wrote:
Here is another non-pythonic question from the Java Developer. (I beg
for forgiveness...)

Does Python have a mechanism for events/event-driven programming?

I'm not necessarily talking about just GUIs either, I'm interested in
using events for other parts of an application as well.

If there isn't some sort of event mechanism built into Python, where
might I find some information about implementing one?


In python 2.5, generators accept parameters. That might be a good
starting-point for an event-system.

Diez
Apr 26 '06 #2
re****************@gmail.com schrieb:

Does Python have a mechanism for events/event-driven programming?


Probably this is something for you:

http://twistedmatrix.com/trac/

--
Servus, Gregor
http://www.gregor-horvath.com
Apr 26 '06 #3

re****************@gmail.com wrote:
Here is another non-pythonic question from the Java Developer. (I beg
for forgiveness...)

Does Python have a mechanism for events/event-driven programming?

I'm not necessarily talking about just GUIs either, I'm interested in
using events for other parts of an application as well.

If there isn't some sort of event mechanism built into Python, where
might I find some information about implementing one?

Thanks,

Scott Huey

Technically this is a question more of perception than capability.

All programming languages are event driven.

Calling a function is an event, for instance.

What I suspect you mean is an event management system, which is what OO
state machines are all about.

If you actually mean something akin in function to GUI event systems,
you're really talking about message passing. In the case of message
passing you just need to build a simple event manager class that echoes
a message to all subscribed listening objects. In python that message
can be a function, object, arguments, text, etc. This is easily done
without threading, in case you're worried about that. The function of
the event manager that is used for the purpose of initiating events can
simply call a particular method of all subscribing objects held in a
list.

Simple as can be :)

~Brendan Kohler

Apr 26 '06 #4
>>>>> "redefined" == redefined horizons <re****************@gmail.com> writes:

redefined> Here is another non-pythonic question from the Java
redefined> Developer. (I beg for forgiveness...)

redefined> Does Python have a mechanism for events/event-driven
redefined> programming?

The enthought traits package has built-in support for event handling,
among other things

http://code.enthought.com/traits/

Here is an example from the web page:

from enthought.traits import Delegate, HasTraits, Int, Str, Instance
from enthought.traits.ui import View, Item

class Parent(HasTraits):
first_name = Str('') # INITIALIZATION:
last_name = Str('') # 'first_name' and
# 'last_name' are
# initialized to ''
class Child(HasTraits):
age = Int

father = Instance(Parent) # VALIDATION: 'father' must
# be a Parent instance

first_name = Str('')

last_name = Delegate('father') # DELEGATION:
# 'last_name' is
# delegated to
# father's 'last_name'

def _age_changed(self, old, new): # NOTIFICATION:
# This method is
# called when 'age'
# changes
print 'Age changed from %s to %s ' % (old, new)

traits_view = View(Item(name='first_name'), # TRAITS UI: Define
Item(name='last_name', # the default window
style='readonly'), # layout
Item(name='age'),
Item(name='father'))
################################################## ##
# Make and manipulate objects from the classes above
################################################## ##

joe = Parent()
joe.last_name = 'Johnson'

# DELEGATION in action
moe = Child()
moe.father = joe
print "Moe's last name is %s" % (moe.last_name)

# NOTIFICATION in action
moe.age = 10

#VISUALIZATION: Display the UI
moe.configure_traits()

The DELEGATION and NOTIFICATION segments in the above example yield
the following command-line output:

Moe's last name is Johnson
Age changed from 0 to 10

Apr 26 '06 #5
re****************@gmail.com wrote:
Here is another non-pythonic question from the Java Developer. (I beg
for forgiveness...)

Does Python have a mechanism for events/event-driven programming?

I'm not necessarily talking about just GUIs either, I'm interested in
using events for other parts of an application as well.

If there isn't some sort of event mechanism built into Python, where
might I find some information about implementing one?


Maybe I got your question wrong, but why not simply use something like:

class Sender:
def __init__(self, event):
self.event = event

def raiseEvent(self):
self.event("Event")

class Receiver:
def receiveEvent(self, msg):
print "Received %r" % msg

r = Receiver()
s = Sender(r.receiveEvent)
s.raiseEvent()

You can pass around functions and bound methods, I always found that's
often a really good substitute for full-blown event mechanisms.

Apr 26 '06 #6
Thank you for all of the responses. I will check out the Traits link.
It looked very interesting.

It seems like Python doesn't have a "standard" implementation of an
event or messaging system. That is really what I was curious about. I
wanted to check before I implemented something of my own.

Thanks again for the help.

Scott Huey

Apr 26 '06 #7
Besides the other anwsers, you might want to check the signal module.

Regards,

Philippe

re****************@gmail.com wrote:
Here is another non-pythonic question from the Java Developer. (I beg
for forgiveness...)

Does Python have a mechanism for events/event-driven programming?

I'm not necessarily talking about just GUIs either, I'm interested in
using events for other parts of an application as well.

If there isn't some sort of event mechanism built into Python, where
might I find some information about implementing one?

Thanks,

Scott Huey


Apr 26 '06 #8
re****************@gmail.com wrote:
It seems like Python doesn't have a "standard" implementation of an
event or messaging system. That is really what I was curious about. I
wanted to check before I implemented something of my own.


What are you comparing it to? Does Java have standard event or
messaging systems? I thought there were only such systems as part of
the GUI libraries. Perhaps you're referring to the Observer interface?
Sometimes a solution that is necessary in Java would be an
overcomplication in Python, and full-blown Observers is probably one
such example.

--
Ben Sizer

Apr 27 '06 #9
On 2006-04-26, nikie <n.******@gmx.de> wrote:
re****************@gmail.com wrote:
Here is another non-pythonic question from the Java Developer. (I beg
for forgiveness...)

Does Python have a mechanism for events/event-driven programming?

I'm not necessarily talking about just GUIs either, I'm interested in
using events for other parts of an application as well.

If there isn't some sort of event mechanism built into Python, where
might I find some information about implementing one?


Maybe I got your question wrong, but why not simply use something like:

class Sender:
def __init__(self, event):
self.event = event

def raiseEvent(self):
self.event("Event")

class Receiver:
def receiveEvent(self, msg):
print "Received %r" % msg

r = Receiver()
s = Sender(r.receiveEvent)
s.raiseEvent()

You can pass around functions and bound methods, I always found that's
often a really good substitute for full-blown event mechanisms.


Actually I'd say full-blown event mechanisms are a poor substitute for
passing around bound-methods :)
Apr 27 '06 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: HankC | last post by:
Greetings: I'm been programming Delphi for a while and am trying to get into the Python mindset. In Delphi, you can have a component class that has properties, methods, and events. Properties...
4
by: Dirk Thierbach | last post by:
I am writing an Python 2.3 extension in C, and I would like to create events (Tk-style) from an external source. I use a library that already parses the events into a C structure. This library can...
5
by: factory | last post by:
Anyone want to comment on what they think of this following bit of a simple event framework? Are there any really obvious things I should be doing with this code that stop me from getting bitten...
0
by: Mark Asbach | last post by:
Hi list, I've got a crazy problem: an application fully written C and using a third-party library that itself uses GTK to draw some GUI widgets works as expected. But if I wrap the lib with SWIG...
1
by: tim | last post by:
Someone using Python Midi Package from http://www.mxm.dk/products/public/ lately? I want to do the following : write some note events in a midi file then after doing that, put some controllers...
2
by: Dan | last post by:
I have need to create a com server that will fire events back to clients. I have looked at some of the documentation and I am new to Python so cant quite get it. I have created an IDL file of the...
4
by: Stephan Kuhagen | last post by:
Hello I'm searching for a Python Module which is able to generate GUI events on different platforms (at least X11 and Windows, MacOSX would be nice), but without being a GUI toolkit itself. So...
4
by: Gianmaria | last post by:
Hi, i'm a .net programmer and i'm learnig python, so this question can be very stupid or easy for python programmers. I've a doubt about events.... here is what: in c# for example i can write a...
2
by: Oliver Nelson | last post by:
I have MapPoint working in Python, and I'm trying to cancel events on the map, but I can't seem to make that happen. I'm responding to the events successfully in my panel object. My code is like...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.