Events in Python?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • redefined.horizons@gmail.com

    #1

    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

  • Diez B. Roggisch

    #2
    Re: Events in Python?

    redefined.horiz ons@gmail.com wrote:
    [color=blue]
    > 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?[/color]

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

    Diez

    Comment

    • Gregor Horvath

      #3
      Re: Events in Python?

      redefined.horiz ons@gmail.com schrieb:[color=blue]
      >
      > Does Python have a mechanism for events/event-driven programming?
      >[/color]

      Probably this is something for you:



      --
      Servus, Gregor
      Firma Gregor Horvath, Ing. - Industrieberatung & Softwareentwicklung, Wien, infor:COM, Tryton, ERP, Web, GNU/Linux

      Comment

      • Avizoa@gmail.com

        #4
        Re: Events in Python?


        redefined.horiz ons@gmail.com wrote:[color=blue]
        > 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[/color]


        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

        Comment

        • John Hunter

          #5
          Re: Events in Python?

          >>>>> "redefined" == redefined horizons <redefined.hori zons@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



          Here is an example from the web page:

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

          class Parent(HasTrait s):
          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('fathe r') # DELEGATION:
          # 'last_name' is
          # delegated to
          # father's 'last_name'

          def _age_changed(se lf, 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='fath er'))


          ############### ############### ############### #######
          # 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_t raits()

          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

          Comment

          • nikie

            #6
            Re: Events in Python?

            redefined.horiz ons@gmail.com wrote:
            [color=blue]
            > 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?[/color]

            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("Eve nt")

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

            r = Receiver()
            s = Sender(r.receiv eEvent)
            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.

            Comment

            • redefined.horizons@gmail.com

              #7
              Re: Events in Python?

              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

              Comment

              • Philippe Martin

                #8
                Re: Events in Python?

                Besides the other anwsers, you might want to check the signal module.

                Regards,

                Philippe



                redefined.horiz ons@gmail.com wrote:
                [color=blue]
                > 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[/color]

                Comment

                • Ben Sizer

                  #9
                  Re: Events in Python?

                  redefined.horiz ons@gmail.com wrote:[color=blue]
                  > 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.[/color]

                  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
                  overcomplicatio n in Python, and full-blown Observers is probably one
                  such example.

                  --
                  Ben Sizer

                  Comment

                  • Ben C

                    #10
                    Re: Events in Python?

                    On 2006-04-26, nikie <n.estner@gmx.d e> wrote:[color=blue]
                    > redefined.horiz ons@gmail.com wrote:
                    >[color=green]
                    >> 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?[/color]
                    >
                    > 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("Eve nt")
                    >
                    > class Receiver:
                    > def receiveEvent(se lf, msg):
                    > print "Received %r" % msg
                    >
                    > r = Receiver()
                    > s = Sender(r.receiv eEvent)
                    > 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.[/color]

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

                    Comment

                    Working...