Skip to main content

Observer pattern for functions and bound methods

Project description

observed allows you to to sign up functions or methods to "observe" other
functions or methods:

from observed import event

class Foo(object):
def __init__(self, name):
self.name = name

@event
def bar(self, arg):
print("Object %s invoked bar with arg='%s'"%(self.name,arg))


def callback(arg):
print("callback was invoked with arg='%s'"%(arg,))

a = Foo('a')
b = Foo('b')
# Sign up b.bar and callback to "observe" a.bar
a.bar.addObserver(b.bar)
a.bar.addObserver(callback)
# Now when we call a.bar, b.bar will be invoked with the same arguments
a.bar('baz')

>>> Object a invoked bar with arg='baz'
>>> Object b invoked bar with arg='baz'
>>> callback was invoked with arg='baz'

This example is included in ./observed/example.py.

You can also ask that the observed object pass itself as the first argument
whenever it calls observers:

from observed import event

@event
def observed_func():
print("observed_func: I was called")

def observer_func(observed):
print("observer_func: %s called me"%(observed.__name__,))

observed_func.addObserver(observer_func, identifyObserved=True)
observed_func()

>>> observed_func: I was called
>>> observer_func: observed_func called me

Notable features include:

1. Being an observer does not keep an object alive. In other words, the
observer code does not keep any strong references to objects which
have signed up as observers.
2. The @event decorator can be used with unhashable types, and can be
used on an arbitrary number of methods in each class.
3. Tests included :)


Installation
------------


pip install observed

or

Unpack the source distribution.
Navigate to the root directory of the unpacked distribution.
At command prompt:
python setup.py install


News
----

See the file NEWS for the user-visible changes from previous releases.


License
------

observed is free (as in beer) software. See the LICENSE file.


Downloading
-----------

observed can be obtained from the python package index

https://pypi.python.org/pypi/observed

or via git

https://github.com/DanielSank/observed.git


Documentation
-------------

Basic usage is illustrated at the top of this file. Further examples are
given in ./observed/example.py

The source code is documented. Docstrings are required in contributions.


Development
-----------

observed development is hosted on github. The current working repository
is given in the Downloading section above.


Bug Reporting
-------------

Please submit bug tickets on the github tracking system

https://github.com/DanielSank/observed/issues

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

observed-0.2.4.zip (7.8 kB view details)

Uploaded Source

File details

Details for the file observed-0.2.4.zip.

File metadata

  • Download URL: observed-0.2.4.zip
  • Upload date:
  • Size: 7.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No

File hashes

Hashes for observed-0.2.4.zip
Algorithm Hash digest
SHA256 f3c819a9e00b151156e2b3363b58e2a6b9a2a04dc5cf926707587a504466b4ba
MD5 0dd57b505604c82dbc451003263513ea
BLAKE2b-256 e4082afd9ac17bd0b6d64d150d8171bccc48a9498d1d8507a8b9d8acffd43efe

See more details on using hashes here.

Supported by

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