Skip to content
/ mnd Public

Match 'n Dispatch - Dispatch to python functions by value..

License

Notifications You must be signed in to change notification settings

stuaxo/mnd

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

58 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Match 'n Dispatch

Match python args and dispatch based on their contents.

Install

$ pip install mnd

Getting started

Create a dispatcher

from mnd.dispatch import Dispatcher, handle

>>> d = Dispatcher()

Add a handler with the handy decorator

>>> @handler(d, msg="hello")
>>> def say(msg=None):
...     print "got message: ", msg

Try dispatching some events

>>> 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"

>>> @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"

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

About

Match 'n Dispatch - Dispatch to python functions by value..

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages