A lightweight library with an implementation of Pub-Sub.
Project description
# Domain Events
[![Build Status](https://travis-ci.org/agroptima/domain-events.svg)](https://travis-ci.org/agroptima/domain-events)
[![License GPLv3](https://img.shields.io/badge/license-GPLv3-red.svg)](https://opensource.org/licenses/GPL-3.0)
![Python versions](https://img.shields.io/badge/python-3.x-blue.svg)
A lightweight library with an implementation of Pub-Sub.
## Install
```
$ pipenv install domain-events
```
or
```
$ pip install domain-events
```
## Usage
In order to have the pub-sub working in your application, you'll need to write your own subscribers and to subscribe them to the publisher.
`domain-events` provides an interface for subscribers that needs to be implemented by your own subscribers in the first place. Two methods are mandatory: `handle(self, event)` and `_events_subscribed_to(self)`. Following is an example of how to do it.
```python
from domain_events import Subscriber
class ExampleSubscriber(Subscriber):
def handle(self, event):
# Logic to handle the occurring event
def _events_subscribed_to(self):
""" Return the tuple of events it is subscribed to. """
return (AnEvent, AnotherEvent)
```
Once the subscriber is implemented, you'll need to subscribe it to the event publisher.
```python
from domain_events import Publisher
Publisher().subscribe(ExampleSubscriber())
```
Now, in order to publish an event, just call the `publish` method on the `Publisher` with one of your domain events.
```python
from domain_events import Event, Publisher
class AnEvent(Event):
pass
Publisher().publish(AnEvent())
```
The event will be passed on to the proper subscribers.
[![Build Status](https://travis-ci.org/agroptima/domain-events.svg)](https://travis-ci.org/agroptima/domain-events)
[![License GPLv3](https://img.shields.io/badge/license-GPLv3-red.svg)](https://opensource.org/licenses/GPL-3.0)
![Python versions](https://img.shields.io/badge/python-3.x-blue.svg)
A lightweight library with an implementation of Pub-Sub.
## Install
```
$ pipenv install domain-events
```
or
```
$ pip install domain-events
```
## Usage
In order to have the pub-sub working in your application, you'll need to write your own subscribers and to subscribe them to the publisher.
`domain-events` provides an interface for subscribers that needs to be implemented by your own subscribers in the first place. Two methods are mandatory: `handle(self, event)` and `_events_subscribed_to(self)`. Following is an example of how to do it.
```python
from domain_events import Subscriber
class ExampleSubscriber(Subscriber):
def handle(self, event):
# Logic to handle the occurring event
def _events_subscribed_to(self):
""" Return the tuple of events it is subscribed to. """
return (AnEvent, AnotherEvent)
```
Once the subscriber is implemented, you'll need to subscribe it to the event publisher.
```python
from domain_events import Publisher
Publisher().subscribe(ExampleSubscriber())
```
Now, in order to publish an event, just call the `publish` method on the `Publisher` with one of your domain events.
```python
from domain_events import Event, Publisher
class AnEvent(Event):
pass
Publisher().publish(AnEvent())
```
The event will be passed on to the proper subscribers.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
domain-events-2.0.tar.gz
(2.8 kB
view details)
Built Distribution
File details
Details for the file domain-events-2.0.tar.gz
.
File metadata
- Download URL: domain-events-2.0.tar.gz
- Upload date:
- Size: 2.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0858fd2d13b4c8fe4d5902bb2eaf0377dfa9e3e3b3e0b57b7283a7360407f091 |
|
MD5 | f05afcd2add140d417968a6c0cb274f9 |
|
BLAKE2b-256 | ee72d47b0d054a43340e1035716c4407f212656e60042cef5c74c6d3a9ad19a7 |
File details
Details for the file domain_events-2.0-py3-none-any.whl
.
File metadata
- Download URL: domain_events-2.0-py3-none-any.whl
- Upload date:
- Size: 15.9 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: Python-urllib/3.5
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 83a6f1d491e541364d8c739c5aec9159aa8efa4a664977719a217866cda349c6 |
|
MD5 | 054c7889bbd772d20168769b7dc26d07 |
|
BLAKE2b-256 | f75b66a225721a795664df76eee085db4066d86a618a0c2f33d705b9a91316ae |