Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

# NotificationCenter

This package implements a class similar to NSNotificationCenter, a variant of the Observer design pattern, from Objective-C and Swift in Python. To install, (soon you will be able to) just use

pip install notificationcenter

It consists of one singleton class, NotificationCenter, that is used to add observers and to post notifications. To add an observer, you need to do the following:

```python
from notificationcenter import *

# Get the NotifiationCenter
notificationcenter = NotificationCenter()
# Define the function you want to perform when you get the notification.
# It should have parameters of (sender, notification name, notification info)
# And return nothing (It can, but it will be thrown away)
def foo(sender, notification_name, info):
# Do something
# Add an observer
observer = notificationcenter.add_observer(with_block=foo,
for_name="bar")
```

Now, if we post a notification to the NotificationCenter with the name "bar", as follows, the above function foo will be executed.

```python
notificationcenter = NotificationCenter()
notificationcenter.post_notification(sender=None,
with_name="bar")
```

When you are no longer using the observer, you should remove it from the NotificationCenter so that the block is no longer executed. It is done as follows:

```python
notificationcenter.remove_oberver(observer)
```

Detailed documentation is shown below, and the source code is commented.

## API Reference

### add\_observer(with\_block, for\_name, for\_sender=None)

Adds an observer to listen for certain types of notifications.

#### Parameters:

* with\_block: Some function that takes arguments of a sender (any object), notification name (string), and notification info (any object). Any values with\_block returns will be ignored.
* The function that will be executed when a notification with this name/sender is observed

* for\_name: String
* The name of the notification for which the observer is registered.

* for\_sender: Any object
* If this is set, then only notifications from this object are recieved. Else, any notifications with the correct name will be recieved.

#### Returns:

* tuple (String, Any Object, Some Function)
* This is a tuple of (for\_name, for\_sender, with\_block). It is not meant to be used directly except in the remove\_observer method.

### remove\_observer(observer)

Removes the given observer from the list of active obserers.

#### Parameters:

* observer: tuple (String, Any Object, Some Function)
* This is the tuple that is returned by add\_observer.

### post\_notification(sender, with\_name, with\_info=None)

Posts a notification with a given name and (optionally) some additional information.

#### Parameters:

* sender: Any Object
* The sender of this notification

* with\_name: String
* The name of the notification

* with\_info: Any Object
* Any additional information for the notification

Metadata

Release files for notificationcenter 1.0.0b2

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for notificationcenter 1.0.0b2
File Size Uploaded
notificationcenter-1.0.0b2.tar.gz 20.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for notificationcenter 1.0.0b2
File Interpreter ABI Platform
notificationcenter-1.0.0b2-py3-none-any.whl Python 3 none any Details

Total release size: 26.4 kB

Release files / notificationcenter-1.0.0b2.tar.gz

Download URL notificationcenter-1.0.0b2.tar.gz
Size 20.0 kB
Tags Source
SHA-256 checksum
How to use checksums
481e9c9c379e0f86d861c60ed6ee5558f46f7de06ce0edd4cf7bdba54687f7a2
BLAKE2b-256 checksum
How to use checksums
7563995c2c49bef3e44a33f890e2e74381edf99f98b178eb4e619dcdeb642476
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release files / notificationcenter-1.0.0b2-py3-none-any.whl

Download URL notificationcenter-1.0.0b2-py3-none-any.whl
Size 6.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f911d87bf45d723bd4996f98237ac5ddb424ab17dd32a18f375e05e9e87a6406
BLAKE2b-256 checksum
How to use checksums
a9faccd3c4f8f4c9516a12e1da643b67d8c3aad8226ccbe2f4ccf95d14b40377
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No

Release history Release notifications | RSS feed

This release

1.0.0b2 This release

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page