Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/stuaxo/mnd
Match 'n Dispatch - Dispatch to python functions by value..
https://github.com/stuaxo/mnd
Last synced: about 1 month ago
JSON representation
Match 'n Dispatch - Dispatch to python functions by value..
- Host: GitHub
- URL: https://github.com/stuaxo/mnd
- Owner: stuaxo
- License: mit
- Created: 2015-10-03T09:37:43.000Z (about 9 years ago)
- Default Branch: master
- Last Pushed: 2017-11-01T13:36:32.000Z (about 7 years ago)
- Last Synced: 2024-09-23T14:17:47.730Z (about 2 months ago)
- Language: Python
- Size: 31.3 KB
- Stars: 2
- Watchers: 2
- Forks: 0
- Open Issues: 4
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Match 'n Dispatch
=================Match python args and dispatch based on their contents.
Install
-------```$ pip install mnd```
Getting started
---------------Create a dispatcher
```python
from mnd.dispatch import Dispatcher, handle>>> d = Dispatcher()
```Add a handler with the handy decorator
```python
>>> @handler(d, msg="hello")
>>> def say(msg=None):
... print "got message: ", msg
```Try dispatching some events
```python
>>> d.dispatch(msg="gets filtered out...")
>>> d.dispatch(msg="hello")
got message: hello
```Match attributes using rules
-----------------------------As well as equality attributes can be matched using rules including,
the full list is ```in lt le eq ne ge gt```.These are inspired by the django filter options.
Example using "in"
```python
>>> @handler(d, msg__in=["hello", "hi"])
>>> def greeting(msg=None):
... print "greetings"
>>> d.dispatch(msg="hello")
greetings
>>> d.dispatch(msg="hi")
greetings
```Example using "gt" and "le"
```python
from collections import namedtuple
Worm=namedtuple("Worm", "segments")
worm1=Worm(4)
worm2=Worm(90)
>>> @handler(d, dict(segments__le=50))
>>> def ordinary_handler(w):
... print("ordinary worm with %s segment.s" % segments)
>>> @handler(d, dict(segments__gt=50))
>>> def long_handler(w):
... print("long worm with %s segment.s" % segments)
>>> d.dispatch(worm1)
ordinary worm with 4 segments.
>>> d.dispatch(worm2)
long worm with 90 segments.
```Unit Test
---------```$ python setup.py test```
[![Build Status](https://travis-ci.org/stuaxo/mnd.svg?branch=master)](https://travis-ci.org/stuaxo/mnd)