Skip to main content

A simple C#-like implementation of the Observer design pattern.

Project description

nmevent - C#-like implementation of the Observer pattern

This is a Python module :mod:`nmevent`, simple C#-like implementation of
the Observer pattern (http://en.wikipedia.org/wiki/Observer_pattern).
It's main purpose and goal is to allow developers to use events
with C#-like syntax in their Python classes.

Usage example
=============

The most straightfoward way to use :mod:`nmevent` is this:

>>> import nmevent
>>> class ExampleClass(object):
... def __init__(self):
... self.event = nmevent.Event()
...
... def _do_something(self):
... self.event(self)
...
>>> example = ExampleClass()
>>> example.event += handler

It should be noted, that event doesn't necessarily need to
be an object attribute. :class:`Event` instance is basically
just a callable object that works as a sort of "dispatch
demultiplexer".

This usage, however, isn't very C#-like. In C#, events are declared
in class scope and that's where the :class:`EventSlot` comes in.
Once you've created event using :class:`EventSlot`, you can
use the same way you use :class:`Event`, only you don't need
to specify the sender when raising the event. That's because
the event is already bound to the instance of the class it has
been declared in.

>>> from nmevent import EventSlot
>>> class ExampleClass(object):
... event = EventSlot()
...
... def _do_something(self):
... self.event()
...
>>> def handler(sender, **keywords):
... pass
...
>>> example = ExampleClass()
>>> example.event += handler

Perhaps this looks even more straightfoward than instantiating
:class:`Event` in object's constructor, but there's actually
lot more going on under hood this time.

>>> import nmevent
>>> @nmevent.with_events
... class ExampleClass(object):
... @nmevent.nmproperty
... def x(self):
... return self._x
...
>>> example = ExampleClass()
>>> example.x_changed += handler
>>> example.x = 10 # handler gets called

License
=======

Copyright (c) 2010, Jan Milík.

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope the it will be useful,
but WITHOUT ANY WARRANTY; without event the implied warranty of
MERCHANTIBILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

nmevent-0.1.1.tar.gz (9.5 kB view details)

Uploaded Source

File details

Details for the file nmevent-0.1.1.tar.gz.

File metadata

  • Download URL: nmevent-0.1.1.tar.gz
  • Upload date:
  • Size: 9.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for nmevent-0.1.1.tar.gz
Algorithm Hash digest
SHA256 1c136e822c3ae7b2fd83447f1cfcb8807f35548bed17b7d35a5c6064843752f9
MD5 4bb7b23540906ad96010651695e927a5
BLAKE2b-256 49d54d734c02db50664abf6d37d6a13f9f44564e8077a15557e5e09a141d66fc

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page